The Dictionary is Key
The countdown_sender script prompts for countdown start and message values and adds them to a dictionary. The dictionary has a 'start' key that is paired with the number the receiver starts counting from. It also has a string paired with an 'after' key that the receiver displays after it’s done counting down. It then creates a string that represents the dictionary (the data packet) and sends it over the radio.
The countdown_reciever script receives the packet string and creates a dictionary from it. Then, it creates a value variable from the int value paired with the 'start' key and a string from the string value paired with the 'after' key.
Let’s take a closer look at how statements in each script do their jobs.
How countdown_sender works
The packet in the example has a string with 37 characters. That’s larger than the default length of 32. So length= was increased to 50 to make enough room for typical countdown values and messages.
radio.config(channel=7,length=50)
This is the same code that’s in countdown_timer.py from Input/Print for Applications. It gets a value and message from the serial monitor.
text = input("Enter countdown start: ") value = int(text) message = input("Enter message after countdown: ")
There are many ways to transmit a value and a message to another micro:bit. However, coding the receiver script to figure out what the purpose of each item is can be tricky. Dictionaries make it much easier, especially when you have more than just the two items we are starting with here.
First, dictionary = { } creates an empty dictionary (though it did not have to use dictionary as the name, it's like naming any other variable.) Next, two key-value pairs were added with dictionary['start'] = value and dictionary['after'] = message. Assuming you entered 3 and Liftoff! when the script was running, dictionary would look like this: {'start': 3, 'after': 'Liftoff!'}. The receiver script will now be able to use the 'start' key to get the int value 3, and the 'after' key to get the string value ‘Liftoff!”
dictionary = { } dictionary['start'] = value dictionary['after'] = message
This could actually have been done in one step, with dictionary = {'start' : value, ‘after’ : message}. The reason we don’t do it that way here is because the receiver script will use complementary statements to store and retrieve values using their keys. See how closely adding dictionary elements resembles accessing them?
Transmitter
dictionary['start'] = value
dictionary['after'] = message
Receiver
value = dictionary['start']
message = dictionary['after']
The radio.send method cannot send a dictionary, but it can send a string. Fortunately, creating a string from other data types is simple with the str function. The packet = str(dictionary) creates a string representation of the dictionary and names it packet. The print statement is there so you can see what the packet string looks like before radio.send transmits it.
packet = str(dictionary) print("Send: ", packet) radio.send(packet)
This just displays an empty line before the loop repeats.
print()
How countdown_receiver works
If the send length is 50, the receive length should also be 50.
radio.config(channel=7,length=50)
Like the other examples that call radio.receive, action only has to be taken when radio.receive returns something other than None. In other words, when radio.receive returns a string that gets stored in a packet, that will have to be processed.
packet = radio.receive() if packet is not None:
This is not a required first step in processing the packet. It’s just so you can see what’s in it before parsing. Below that, headings for the parse results are also printed.
print("Receive: ", packet) print() print("Parse: ")
The first step in parsing the packet is converting it from a string back to a dictionary. That’s what dictionary = eval(packet) does. Let’s say the packet variable contains this string: "{'start': 3, 'after': 'Liftoff!'}". The eval function evaluates what’s in a string as a Python expression and returns the result. In this case, it returns the dictionary object: {'start': 3, 'after': 'Liftoff!'}. Lastly, dictionary = makes a name reference for that dictionary object.
dictionary = eval(packet)
Again, the name dictionary is not a requirement. For example, you could choose a brief memory-saving name like d, or a longer name like my_countdown_dictionary. Of course, if you change it in one place, you’ll also have to update the other parts of the script that use the name dictionary to make it work again.
Now that there’s a dictionary, the script can use the 'start' key to access the value (3 in the example). Then, it can use the 'after' key to access the "Liftoff!" string value.
value = dictionary['start'] message = dictionary['after']
These print statements can help you verify that the value the receiver received matches the one the sender sent. Likewise with the message.
print("value = ", value) print("message = ", message, "\n")
Now that the receiver script has value and message, it can finish the job, which is the loop in countdown_timer.py from Input/Print for Applications.
while value >= 0: print(value) sleep(1000) value = value - 1 print(message) print()