Micro-bits Skills
- Sending and receiving messages
- Using Channels to send and receive messages to and from different micro-bits.
Skill One: Sending and receiving messages
Code for micro-bit ONE
from microbit import *
import radio
# Turn on Radio
radio.on()
while True:
# button A
if button_a.was_pressed():
# Send Message
display.show("A")
radio.send("Hi!")
Code for micro-bit TWO
from microbit import *
import radio
# Turn on Radio
radio.on()
# wait for a message
while True:
message = radio.receive()
# Display message
if message:
display.scroll(message)
Skill Two: Using Channels
Using Channels to send and receive messages to and from different micro-bits.
Code for micro-bit ONE
from microbit import *
import radio
# Turn on Radio
radio.on()
# wait for button press
while True:
# button A
if button_a.was_pressed():
# change channel
radio.config(channel=12)
# send message
radio.send('TWO')
# button B
if button_b.was_pressed():
# change channel
radio.config(channel=13)
# send message
radio.send('THREE')
Code for micro-bit TWO
from microbit import *
import radio
radio.on()
radio.config(channel=12)
while True:
message = radio.receive()
if message:
display.scroll(message)
Code for micro-bit THREE: Use the same code for micro-bit TWO, just change the channel to 13
Skill Three: Routing messages
Send message from micro-bit ONE to the WAP and the WAP sends the message to the correct micro-bit
Code for micro-bit ONE
from microbit import *
import radio
radio.on()
radio.config(channel=10)
while True:
if button_a.was_pressed():
radio.send('TWO:hey')
if button_b.was_pressed():
radio.send('THREE:hey')
Code for Router micro-bit
from microbit import *
import radio
radio.on()
radio.config(channel=10)
while True:
message = radio.receive()
if message:
destination, msg = message.split(':')
if destination = “TWO”:
radio.config(channel=12)
if destination = “THREE”:
radio.config(channel=13)
radio.send(msg)
radio.config(channel=10)
Code for the two receiver micro-bits:
NOTE: Change the channel to suit the receiver micro-bit
from microbit import *
import radio
radio.on()
radio.config(channel=12)
while True:
message = radio.receive()
if message:
display.scroll(message)
Skill Three: Using a Routing Table
Send message from micro-bit ONE to the WAP and the WAP uses a routing table to send the message to the correct micro-bit
Code for Router micro-bit
from microbit import *
import radio
routing_table = { 'TWO': 12, 'THREE': 13}
radio.on()
radio.config(channel=10)
while True:
message = radio.receive()
if message:
destination, msg = message.split(':')
new_channel = routing_table[destination]
radio.config(channel=new_channel)
radio.send(msg)
radio.config(channel=10)
TWO digit Display
Each message sent needs to include the following information
- Sender ID ( ‘A5’ , ‘M15’, ‘E25’)
- Destination (‘M17’)
- Payload
# Code for Laptop
from microbit import *
#################################################################################
## ##
## Two Digit Display ##
## ##
#################################################################################
# Custom 2-column wide digits (0 to 9)
# 9 represents full brightness, 0 is off
MINI_DIGITS = {
'0': ["99", "99", "99", "99", "99"],
'1': ["09", "09", "09", "09", "09"],
'2': ["99", "09", "99", "90", "99"],
'3': ["99", "09", "99", "09", "99"],
'4': ["99", "99", "99", "09", "09"],
'5': ["99", "90", "99", "09", "99"],
'6': ["99", "90", "99", "99", "99"],
'7': ["99", "09", "09", "09", "09"],
'8': ["99", "99", "00", "99", "99"],
'9': ["99", "99", "99", "09", "09"]
}
def show_2_digits(number):
# Ensure it's a 2-digit string (e.g., 7 becomes "07")
num_str = "{:02d}".format(number)
tens_digit = MINI_DIGITS[num_str[0]]
ones_digit = MINI_DIGITS[num_str[1]]
# Construct the 5x5 frame string
# Left 2 cols = Tens, Middle col = Blank (0), Right 2 cols = Ones
frame_rows = []
for i in range(5):
row_str = tens_digit[i] + "0" + ones_digit[i]
frame_rows.append(row_str)
# Join rows with colons to create a micro:bit Image string
image_string = ":".join(frame_rows)
img = Image(image_string)
# if single digit number just show that number
if num_str[0] == '0':
display.show(num_str[1])
else:
display.show(img)
#################################################################################
## ##
## END : Two Digit Display ##
## ##
#################################################################################
import radio
#Modify deviceID to suit
deviceID = 'A17'
deviceClass = deviceID[0]
deviceChannel = int(deviceID[1:])
# Set the deviceID for TWO other devices
destinationAID = 'A16'
destinationBID = 'A18'
# message components
# { senderID, desinationID, payload }
# { 'A7', 'E24', 'Hi'}
#routing_table = { 'A': 5, 'M': 15, 'E': 25}
# Turn on Radio
radio.on()
radio.config(channel=deviceChannel)
display.show(deviceClass)
sleep(300)
while True:
#display.show(deviceChannel)
show_2_digits(deviceChannel)
if button_a.was_pressed():
destinationID = destinationAID
destinationChannel = int(destinationID[1:])
payload = 'Hi'
radio.config(channel=destinationChannel)
radio.send(deviceID + ':' + destinationID + ':' + payload)
radio.config(channel=deviceChannel)
if button_b.was_pressed():
destinationID = destinationBID
destinationChannel = int(destinationID[1:])
payload = 'Hi'
radio.config(channel=destinationChannel)
radio.send(deviceID + ':' + destinationID + ':' + payload)
radio.config(channel=deviceChannel)
msg = radio.receive()
if msg:
senderID, desinationID, payload = msg.split(':')
display.scroll(msg)