micro:bit Remote Code Updates

In this activity, you will follow along as a chatbot is guided through writing two application scripts: one for a sender micro:bit and for receiver micro:bit modules.  The sender application allows you to type a script into the serial monitor. Then, after typing run, the sender radio-broadcasts the script to any available micro:bit receivers.  The receiver application receives and executes any scripts they receive over the radio.   You will also test the scripts and consider beneficial and malicious ways in which it could be used.

Application Development Conversation

  • Read through the conversation below, to see how the chatbot was prompted.  The ... lines indicate where the script iterations were omitted. Final scripts will be on the next page.

User

Write two scripts for two micro:bit modules.  The first module's script should allow the user to input a string in the terminal.  It should then send that string over the radio to the second micro:bit.  The second micro:bit should receive that string over its radio, and execute it as Python statements.

ChatGPT

Certainly! To achieve this communication between two micro:bits using radio communication, you can use the radio module that comes with the micro:bit MicroPython library. Below are the scripts for the two micro:bit modules.

 

User

Let's up the buffer in both scripts to 250 characters.

ChatGPT

Certainly! To increase the buffer size for both scripts, you can modify the scripts as follows:

In these modified scripts, I've added display.scroll messages to provide prompts and indicate when a message has been sent or received. These messages will scroll on the micro:bit's LED display. Make sure to upload these updated scripts to your micro:bits.

 

User

Adjust this to be entirely terminal based.

ChatGPT

If you want the interaction to be entirely text-based in the terminal without using the micro:bit LED display, you can modify the scripts accordingly. Below are the adjusted scripts:

In this modification, the receiver script accumulates the lines of the script until it receives the command "run." When "run" is received, it executes the accumulated script line by line using the exec function. After execution, it clears the script lines to prepare for the next set of input. This way, you can enter multi-line scripts and execute them by sending the "run" command.

 

User

Could both scripts be adjusted to support indentation?

ChatGPT

Certainly! To support indentation in Python scripts, you can adjust the receiver script to properly handle indentation. Here are the modified scripts:

See Module 1: Sender and Module 2: Receiver in the Scripts section.

….

In this modification, the receiver script now properly adjusts the indentation by comparing the leading spaces of each line with the current indentation level. It appends lines to the script accordingly, taking into account the indentation.