Imagine sharing something personal, like maybe your mood or reaction to something, with a friend. The catch is, everybody else in the class gets to read your text! That’s certainly possible if you share your data unencrypted.
This activity uses one micro:bit to broadcast microbit.Image.HAPPY/SAD/ANGRY images. They might be only intended for one micro:bit receiver, but without encryption, any other micro:bit receiver in range can access the data too. Other people might monitor channels with their micro:bit modules and after gathering information through sniffing, then use it for purposes you would not want or intend.
After this activity, you will add encryption, and examine the difference in what the “sniffing” micro:bit is able to detect.
If you only have two micro:bit modules you can alternate the one between intended receiver and sniffer roles. You will load the script for the intended receiver and perform any tests. Then, load the script for the sniffer, and perform tests for that role.
See Texting with Terminals [2] if you need a reminder on how to set up the serial connection to the micro:bit modules.
# radio_send_images.py from microbit import * import radio radio.on() radio.config(channel=7) sleep(1000) while True: packet = "HAPPY" print("Send:", packet) radio.send(packet) sleep(2500) packet = "SAD" print("Send:", packet) radio.send(packet) sleep(2500) packet = "ANGRY" print("Send:", packet) radio.send(packet) sleep(2500)
# radio_receive_images.py from microbit import * import radio radio.on() radio.config(channel=7) sleep(1000) while True: packet = radio.receive() if packet: print("Receive:", packet) display.show(getattr(Image, packet))
If the sniffer micro:bit also displays the same images, it means the person with that micro:bit can monitor all your data exchanges!
The transmitter script broadcasts a string containing a term every 2.5 seconds. Those terms HAPPY, SAD, ANGRY can be used by receivers in statements that work the same way as display.show(Image.HAPPY), display.show(Image.SAD), and so on.
The statements below set up and turn on the micro:bit module’s built-in radio and also provide a helpful 1-second delay before sending data to the terminal. To find out more, try out the Cybersecurity: Radio Basics [3] activities.
# radio_send_images.py from microbit import * import radio radio.on() radio.config(channel=7) sleep(1000)
A convenient approach to sending predefined image data through the radio is to simply send strings that contain words like "HAPPY", "SAD", "ANGRY" that can be used as arguments. The receiver script can build a statement like display.show(Image.HAPPY). In addition to sending the data packet, the transmitter script prints the word on the terminal so you can monitor which mood/reaction image it’s sending through radio.send(packet).
while True: packet = "HAPPY" print("Send:", packet) radio.send(packet) sleep(2500) ...
After the same familiar initialization, the receiver script's main loop checks for incoming messages with packet = radio.receive(). If nothing is received, packet = None, and the statements under if packet: get skipped. When packet stores a string like "HAPPY", "SAD", or "ANGRY", the statements indented below if packet: print the string to the terminal, and then display the corresponding image on the micro:bit module’s LEDs matrix.
while True: packet = radio.receive() if packet: print("Receive:", packet) display.show(getattr(Image, packet))
The trick to using a string to access the image is to use getattr (object, name). This “get attribute” function accepts an object and the string name of one of its properties. It responds by returning that property. When packet contains "HAPPY", then getattr(Image, "HAPPY") returns Image.HAPPY, and the micro:bit lights up its LEDs in response to display.show(Image.HAPPY).
If you’re skeptical that packet = "HAPPY" followed by getattr(Image, packet) is equivalent to Image.HAPPY, try this:
print("Image.HAPPY =", Image.HAPPY) sleep(500) packet = "HAPPY" print( "getattr(Image, packet) =", getattr(Image, packet) ) sleep(500)
Image(…) has brightness data for each LED in the display. In this case it only uses 0 (LED off), and 9 (LED brightest). Values between 0 and 9 for each LED can be used to control the brightness.
This code takes up unnecessary program space in the micro:bit module’s memory.
packet = "HAPPY" print("Send:", packet) radio.send(packet) sleep(2500) packet = "SAD" print("Send:", packet) radio.send(packet) sleep(2500) packet = "ANGRY" print("Send:", packet) radio.send(packet) sleep(2500)
Your program can instead create a list like string_list = ["HAPPY", "SAD", "ANGRY"]. Then, inside the while True loop, the for packet in string_list can index through each string in string_list.
Links
[1] https://python.microbit.org
[2] https://learn.parallax.com/tutorials/language/python/cybersecurity-radio-basics/texting-terminals
[3] https://learn.parallax.com/tutorials/robot/cyberbot/cybersecurity-radio-basics