|
| 1 | +Radio |
| 2 | +----- |
| 3 | + |
| 4 | +Interaction at a distance feels like magic. |
| 5 | + |
| 6 | +Magic might be useful if you're an elf, wizard or unicorn, but such things only |
| 7 | +exist in stories. |
| 8 | + |
| 9 | +However, there's something much better than magic: physics! |
| 10 | + |
| 11 | +Wireless interaction is all about physics: radio waves (a type of |
| 12 | +electromagnetic radiation, similar to visible light) have some sort of property |
| 13 | +(such as their amplitude, phase or pulse width) modulated by a transmitter in |
| 14 | +such a way that information can be encoded and, thus, broadcast. When radio |
| 15 | +waves encounter an electrical conductor (i.e. an aerial), they cause an |
| 16 | +alternating current from which the information in the waves can be extracted |
| 17 | +and transformed back into its original form. |
| 18 | + |
| 19 | +Layers upon Layers |
| 20 | +++++++++++++++++++ |
| 21 | + |
| 22 | +If you remember, networks are built in layers. |
| 23 | + |
| 24 | +The most fundamental requirement for a network is some sort of connection that |
| 25 | +allows a signal to get from one device to the other. In our networking |
| 26 | +tutorial we used wires connected to the I/O pins. Thanks to the radio module we |
| 27 | +can do away with wires and use the physics summarised above as the invisible |
| 28 | +connection between devices. |
| 29 | + |
| 30 | +The next layer up in the network stack is also different from the example in |
| 31 | +the networking tutorial. With the wired example we used digital on and off to |
| 32 | +send and read a signal from the pins. With the built-in radio on the |
| 33 | +micro:bit the smallest useful part of the signal is a byte. |
| 34 | + |
| 35 | +Bytes |
| 36 | ++++++ |
| 37 | + |
| 38 | +A byte is a unit of information that (usually) consists of eight bits. A bit is |
| 39 | +the smallest possible unit of information since it can only be in two states: |
| 40 | +on or off. |
| 41 | + |
| 42 | +Bytes work like a sort of abacus: each position in the byte is like a |
| 43 | +column in an abacus - they represent an associated number. In an abacus these |
| 44 | +are usually thousands, hundreds, tens and units (in UK parlance). In a byte |
| 45 | +they are 128, 64, 32, 16, 8, 4, 2 and 1. As bits (on/off |
| 46 | +signals) are sent over the air, they are re-combined into bytes by the |
| 47 | +recipient. |
| 48 | + |
| 49 | +Have you spotted the pattern? (Hint: base 2.) |
| 50 | + |
| 51 | +By adding the numbers associated with the positions in a byte that are set to |
| 52 | +"on" we can represent numbers between 0 and 255. The image below shows how this |
| 53 | +works with five bits and counting from zero to 32: |
| 54 | + |
| 55 | +.. image:: binary_count.gif |
| 56 | + |
| 57 | +If we can agree what each one of the 255 numbers (encoded by a byte) represents ~ such as a character ~ then we can start to send text one character per byte |
| 58 | +at a time. |
| 59 | + |
| 60 | +Funnily enough, people have already |
| 61 | +`thought of this <https://en.wikipedia.org/wiki/ASCII>`_ ~ using bytes to |
| 62 | +encode and decode information is commonplace. This approximately corresponds to |
| 63 | +the Morse-code "protocol" layer in the wired networking example. |
| 64 | + |
| 65 | +A really great series of child (and teacher) friendly explanations of "all |
| 66 | +things bytes" can be found at the |
| 67 | +`CS unplugged <http://csunplugged.org/binary-numbers/>`_ website. |
| 68 | + |
| 69 | +Addressing |
| 70 | +++++++++++ |
| 71 | + |
| 72 | +The problem with radio is that you can't transmit directly to one person. |
| 73 | +Anyone with an appropriate aerial can receive the messages you transmit. As a |
| 74 | +result it's important to be able to differentiate who should be receiving |
| 75 | +broadcasts. |
| 76 | + |
| 77 | +The way the radio built into the micro:bit solves this problem is quite simple: |
| 78 | + |
| 79 | +* It's possible to tune the radio to different channels (numbered 0-100). This works in exactly the same way as kids' walkie-talkie radios: everyone tunes into the same channel and everyone hears what everyone else broadcasts via that channel. As with walkie-talkies, if you use adjacent channels there is a slight possibility of interference. |
| 80 | + |
| 81 | +* The radio module allows you to specify two pieces of information: an address and a group. The address is like a postal address whereas a group is like a specific recipient at the address. The important thing is the radio will filter out messages that it receives that do not match *your* address and group. As a result, it's important to pre-arrange the address and group your application is going to use. |
| 82 | + |
| 83 | +Of course, the micro:bit is still receiving broadcast messages for other |
| 84 | +address/group combinations. The important thing is you don't need to worry |
| 85 | +about filtering those out. Nevertheless, if someone were clever enough, they |
| 86 | +could just read *all the wireless network traffic* no matter what the target |
| 87 | +address/group was supposed to be. In this case, it's *essential* to use |
| 88 | +encrypted means of communication so only the desired recipient can actually |
| 89 | +read the message that was broadcast. Cryptography is a fascinating subject but, |
| 90 | +unfortunately, beyond the scope of this tutorial. |
| 91 | + |
| 92 | +Fireflies |
| 93 | ++++++++++ |
| 94 | + |
| 95 | +This is a firefly: |
| 96 | + |
| 97 | +.. image:: firefly.gif |
| 98 | + |
| 99 | +It's a sort of bug that uses bioluminescence to signal (without wires) to its |
| 100 | +friends. Here's what they look like when they signal to each other: |
| 101 | + |
| 102 | +.. image:: fireflies.gif |
| 103 | + |
| 104 | +The BBC have `rather a beautiful video <http://www.bbc.com/earth/story/20160224-worlds-largest-gathering-of-synchronised-fireflies>`_ of fireflies available online. |
| 105 | + |
| 106 | +We're going to use the radio module to create something akin to a swarm of |
| 107 | +fireflies signalling to each other. |
| 108 | + |
| 109 | +First ``import radio`` to make the functions available to your Python program. |
| 110 | +Then call the ``radio.on()`` function to turn the radio on. Since |
| 111 | +the radio draws power and takes up memory we've made it so *you* decide |
| 112 | +when it is enabled (there is, of course a ``radio.off()`` function). |
| 113 | + |
| 114 | +At this point the radio module is configured to sensible defaults that make |
| 115 | +it compatible with other platforms that may target the BBC micro:bit. It is |
| 116 | +possible to control many of the features discussed above (such as channel and |
| 117 | +addressing) as well as the amount of power used to broadcast messages and the |
| 118 | +amount of RAM the incoming message queue will take up. The API documentation |
| 119 | +contains all the information you need to configure the radio to your needs. |
| 120 | + |
| 121 | +Assuming we're happy with the defaults, the simplest way to send a message is |
| 122 | +like this:: |
| 123 | + |
| 124 | + radio.send("a message") |
| 125 | + |
| 126 | +The example uses the ``send`` function to simply broadcast the string |
| 127 | +"a message". To receive a message is even easier:: |
| 128 | + |
| 129 | + new_message = radio.receive() |
| 130 | + |
| 131 | +As messages are received they are put on a message queue. The ``receive`` |
| 132 | +function returns the oldest message from the queue as a string, making space |
| 133 | +for a new incoming message. If the message queue fills up, then new incoming |
| 134 | +messages are ignored. |
| 135 | + |
| 136 | +That's really all there is to it! (Although the radio module is also powerful |
| 137 | +enough that you can send any arbitrary type of data, not just strings. See the |
| 138 | +API documentation for how this works.) |
| 139 | + |
| 140 | +Armed with this knowledge, it's simple to make micro:bit fireflies like this: |
| 141 | + |
| 142 | +.. include:: ../../examples/radio.py |
| 143 | + :code: python |
| 144 | + |
| 145 | +The import stuff happens in the event loop. First, it checks if button A was |
| 146 | +pressed and, if it was, uses the radio to send the message "flash". Then it |
| 147 | +reads any messages from the message queue with ``radio.receive()``. If there is |
| 148 | +a message it sleeps a short, random period of time (to make the display more |
| 149 | +interesting) and uses ``display.show()`` to animate a firefly flash. Finally, |
| 150 | +to make things a bit exciting, it chooses a random number so that it has a 1 in |
| 151 | +10 chance of re-broadcasting the "flash" message to anyone else (this is how |
| 152 | +it's possible to sustain the firefly display among several devices). If it |
| 153 | +decides to re-broadcast then it waits for half a second (so the display from |
| 154 | +the initial flash message has chance to die down) before sending |
| 155 | +the "flash" signal again. Because this code is enclosed within a ``while True`` |
| 156 | +block, it loops back to the beginning of the event loop and repeats this |
| 157 | +process forever. |
| 158 | + |
| 159 | +The end result (using a group of micro:bits) should look something like this: |
| 160 | + |
| 161 | +.. image:: mb-firefly.gif |
| 162 | + |
| 163 | +.. footer:: The image of binary counting is released under the licensing details listed here: https://en.wikipedia.org/wiki/File:Binary_counter.gif |
0 commit comments