radio module allows devices to work together via simple wireless
The radio module is conceptually very simple:
- Broadcast messages are of a certain configurable length (up to 251 bytes).
- Messages received are read from a queue of configurable size (the larger the queue the more RAM is used). If the queue is full, new messages are ignored.
- Messages are broadcast and received on a preselected channel (numbered 0-100).
- Broadcasts are at a certain level of power - more power means more range.
- Messages are filtered by address (like a house number) and group (like a named recipient at the specified address).
- The rate of throughput can be one of three pre-determined settings.
- Send and receieve bytes to work with arbitrary data.
- As a convenience for children, it’s easy to send and receive messages as strings.
- The default configuration is both sensible and compatible with other platforms that target the BBC micro:bit.
To access this module you need to:
We assume you have done this for the examples below.
Constant used to indicate a throughput of 256 Kbit a second.
Constant used to indicate a throughput of 1 MBit a second.
Constant used to indicate a throughput of 2 MBit a second.
Turns the radio on. This needs to be explicitly called since the radio draws power and takes up memory that you may otherwise need.
Turns off the radio, thus saving power and memory.
Configures various keyword based settings relating to the radio. The available settings and their sensible default values are listed below.
length(default=32) defines the maximum length, in bytes, of a message sent via the radio. It can be up to 251 bytes long (254 - 3 bytes for S0, LENGTH and S1 preamble).
queue(default=3) specifies the number of messages that can be stored on the incoming message queue. If there are no spaces left on the queue for incoming messages, then the incoming message is dropped.
channel(default=7) can be an integer value from 0 to 100 (inclusive) that defines an arbitrary « channel » to which the radio is tuned. Messages will be sent via this channel and only messages received via this channel will be put onto the incoming message queue. Each step is 1MHz wide, based at 2400MHz.
power(default=0) is an integer value from 0 to 7 (inclusive) to indicate the strength of signal used when broadcasting a message. The higher the value the stronger the signal, but the more power is consumed by the device. The numbering translates to positions in the following list of dBm (decibel milliwatt) values: -30, -20, -16, -12, -8, -4, 0, 4.
address(default=0x75626974) is an arbitrary name, expressed as a 32-bit address, that’s used to filter incoming packets at the hardware level, keeping only those that match the address you set. The default used by other micro:bit related platforms is the default setting used here.
group(default=0) is an 8-bit value (0-255) used with the
addresswhen filtering messages. Conceptually, « address » is like a house/office address and « group » is like the person at that address to which you want to send your message.
data_rate(default=radio.RATE_1MBIT) indicates the speed at which data throughput takes place. Can be one of the following contants defined in the
configis not called then the defaults described above are assumed.
Reset the settings to their default values (as listed in the documentation for the
None of the following send or receive methods will work until the radio is turned on.
Sends a message containing bytes.
Receive the next incoming message on the message queue. Returns
Noneif there are no pending messages. Messages are returned as bytes.
Sends a message string. This is the equivalent of
send_bytes(bytes(message, 'utf8'))but with
b'\x01\x00\x01'prepended to the front (to make it compatible with other platforms that target the micro:bit).
Works in exactly the same way as
receive_bytesbut returns whatever was sent.
Currently, it’s equivalent to
str(receive_bytes(), 'utf8')but with a check that the the first three bytes are
b'\x01\x00\x01'(to make it compatible with other platforms that may target the micro:bit). It strips the prepended bytes before converting to a string.
ValueErrorexception is raised if conversion to string fails.
# A micro:bit Firefly. # By Nicholas H.Tollervey. Released to the public domain. import radio import random from microbit import display, Image, button_a, sleep # Create the "flash" animation frames. Can you work out how it's done? flash = [Image().invert()*(i/9) for i in range(9, -1, -1)] # The radio won't work unless it's switched on. radio.on() # Event loop. while True: # Button A sends a "flash" message. if button_a.was_pressed(): radio.send('flash') # a-ha # Read any incoming messages. incoming = radio.receive() if incoming == 'flash': # If there's an incoming "flash" message display # the firefly flash animation after a random short # pause. sleep(random.randint(50, 350)) display.show(flash, delay=100, wait=False) # Randomly re-broadcast the flash message after a # slight delay. if random.randint(0, 9) == 0: sleep(500) radio.send('flash') # a-ha