In this activity, you will first try an application of one-way (unidirectional) texting between terminals through a micro:bit-to-micro:bit radio link. Then, you will then expand it for two-way (bidirectional) texting.
Here is the block diagram for unidirectional texting. You will type text into the terminal connected to the sender micro:bit. The sender micro:bit's script will input those messages from the terminal and broadcast them with its radio. The receiver micro:bit will receive those messages, and print them to the terminal it’s connected to.
This activity is similar to the previous one since you’ll need to have two micro:bit modules running at the same time. But now, they both have to be connected to USB ports and terminal apps at the same time. So, you will either need to use two computers, each with a micro:bit connected to its USB, or use one computer with two micro:bit modules connected to two USB different ports and two browsers, each with a terminal app.
Time to choose your hardware setup!
If you accidentally connect both browsers to the same micro:bit, you might need to unplug/replug their UB cables and repeat the browser connecting steps.
It is time to decide which micro:bit you want to be the sender and which one you want to be the receiver. You will start with the browser connected to the receiver micro:bit
Now, you will use the browser connected to the sender micro:bit.
# text_terminal_to_radio from microbit import * import radio radio.on() radio.config(channel = 7) sleep(1000) print("micro:bit radio sender") while True: message = input("Send: ") radio.send(message)
Sender terminal
Receiver terminal
With unidirectional RF text working, the next step is bidirectional texting. Each micro:bit transceiver (transmitter/receiver) will take input that you type into its terminal and send it through the radio. Each micro:bit will also receive radio messages and print them to the terminal.
IMPORTANT: The Send: prompt will not appear until you click inside the terminal and start typing.
# terminal_chat_through_microbits # learn.parallax.com/cyberbot # Copyright Parallax Inc 2020 under MIT license from microbit import * import radio radio.on() radio.config(channel=7) sleep(1000) print("micro:bit transceiver A") print("Type messages, press enter to send.") print("Received messages will also be displayed.") while True: if uart.any(): tx = input("Send: ") radio.send(tx) message = radio.receive() if message is not None: print("Receive: ", message)
Transceiver A
Transceiver B
The main loop in each program repeatedly and rapidly checks to find out if any characters were typed into the keyboard or received by the radio. If uart.any() is zero, meaning nothing was typed into the terminal, the script moves on to check for messages from the radio. If that’s none too, the while True loop just repeats, rapidly, without doing anything else.
while True: if uart.any(): tx = input("Send: ") radio.send(tx) message = radio.receive() if message is not None: print("Receive: ", message)
If uart.any() is something other than zero, meaning you have typed characters into the terminal, tx = input("Send: ") makes the Send: prompt appear. It then stores the characters you type into the tx variable as a string, up until you press the Enter key. Then, radio.send(tx) transmits that string over the radio for another micro:bit to receive and forward to its terminal.
Remember that message = radio.receive() either stores a string or None in message. If message has a string, then print("Receive: ", message) displays it.
UART — it stands for universal asynchronous receiver transmitter. UART communication is one of several common communication protocols that devices use to exchange data.
Buffers — Incoming messages to the UART are stored in a special string called a buffer that is set aside for keeping characters until you need them. The radio object also has a buffer.
TX & RX — The abbreviation tx is short for transmit. If you see tx in scripts, you might also see rx, which is shorthand for receive. For example, the message variable could be named rx instead.
Dropped Packets — In its current form, the application drops packets sometimes. In other words, not every message you type will be received and displayed by the other micro:bit.