Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • Cybersecurity with AI and micro:bit

Cybersecurity with AI and micro:bit

Sender and Receiver Scripts

This example uses one sender and at least one receiver micro:bit.  Both micro:bit modules can be connected to a single computer, or to separate computers in the same room.

  • Start with just two micro:bit modules.
  • Open two separate instances of the micro:bit Python Editor.  
    • If both micro:bit modules are connected to the same computer, they can be two different tabs in the same browser. 
    • If they are connected to different computers, you will need one micro:bit Python Editor running on each computer.
  • If you are in a classroom, pair up with another student and adjust the radio.config(channel=7) statements in both scripts.  Make sure that each pair of students is using a unique channel number, from 0 to 83.
  • Connect both micro:bit modules to USB ports.
  • With the first micro:bit Python Editor, click Send to micro:bit to flash the Module 1: Sender script to one micro:bit module.
  • Click Show serial.
  • With the second micro:bit Python Editor, Click Send to micro:bit to flash the Module 2: Receiver script into one or more micro:bit modules.
  • Click Show serial.

 

Module 1: Sender

# Module 1: Sender

import radio

def get_input_string():
    print("Enter text:")
    return input()

radio.on()
radio.config(channel=7)   # Set the radio channel, make sure it matches with the receiver
radio.config(length=250)  # Set the buffer size to 250 characters
while True:
    user_input = get_input_string()
    radio.send(user_input)
    print("Sent:", user_input)

 

Module 2: Receiver

# Module 2: Receiver

import radio

def execute_python_code(script_lines):
    try:
        script = "\n".join(script_lines)
        exec(script)
    except Exception as e:
        print("Error:", e)

radio.on() 
radio.config(channel=7)   # Set the radio channel, make sure it matches with the sender
radio.config(length=250)  # Set the buffer size to 250 characters

script_lines = []  # List to store the lines of the script
indent_level = 0   # Track the current indentation level

while True:
    incoming_message = radio.receive()
    if incoming_message is not None:
        print("Received:", incoming_message)

        if incoming_message == "run":
            execute_python_code(script_lines)
            script_lines = []  # Clear the script lines after running
            indent_level = 0   # Reset the indentation level
        else:
            # Adjust indentation based on leading spaces
            line_indent = len(incoming_message) - len(incoming_message.lstrip())
            if line_indent > indent_level:
                script_lines.append(incoming_message[indent_level:])
            else:
                script_lines.append(incoming_message)

 


Printer-friendly version
micro:bit Remote Code Updates
Prev
Test the Scripts
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2025 Parallax Learn • Built with GeneratePress