Micro:bit unicasting

In Chapter 5 of our book Networking with the Micro:bit, we ask learners to program their Micro:bits for unicasting.

Unicast, sending messages to a single receiver, is the typical way we communicate on the Internet. For example, to view a web page, we send unicast messages to a server, which in turn sends us the page to display in our browser.

By programming unicast with Micro:bit, you will learn some basic ideas of computer networking besides unicast:

  • Protocol: A set of rules for how messages are sent and received
  • (Network) address: An address is a unique string that identifies a computer in a network. For instance, if the network is running the Internet Protocol, then this string is the IP address. 
  • Data packet and packet header:  A data packet is a piece of data sent over a network. This piece of data has an actual message part (for example, an image or a text) and one or more header parts. A header contains helpful information for protocols like the sender and receiver addresses.



For this exercise, we will assume that all micro:bits can send and receive from each other. If this was not the case, we would need routing. To learn more about the Internet and Routing, here is an excellent video -  The Internet: Packets, Routing & Reliability by code.org: https://youtu.be/AYdF7b3nMto


To exchange packets between two micro:bits, we first need to decide on the protocol. The protocol will include:
  1. What headers and packets will look like: In this simple example, our header will be sender and receiver addresses. The rest of the packet, called the payload, is a message string. 
  2. When we send a packet: We send a packet on pressing button A. 
  3. What we do when we receive a packet: When we receive a packet, we check whether we are the receiver, and then display the sender and its message on our screen. 
Here is an example code that does all the above:

 Sending a packet

Sending a packet is simple. We join the strings that make up the header and the message. But, we should pay attention to the length of our string. The largest string supported by the micro:bit radio is 19 characters long. Read more here: https://makecode.microbit.org/reference/radio/send-string

Receiving a packet

Receiving a packet is more complex.  To find out the content of the packet (the received string) we need to know the protocol. In other words, we need to know that the received string contains the sender and receiver addresses and the message (and in that order). So, using this knowledge, we parse the string. First, we find out how long it is. Then, we treat the first 2 characters as the sender address. The next two characters are the receiver address. The remaining characters are our message. But, being good citizens, we do not parse this part unless we are the receiver. 

This is obviously not a secure protocol. Others may read our messages. How would you make this protocol more secure?

Testing

To test, prepare two programs with the correct "my_address" and "friend_address" fields. For example, if the first program has "my_address=cs" and "friend_address=ak", the second program should have "my_address=ak" and "friend_address=cs". You may want to use different messages too.  

Take a look at our Chapter 5 of our book Networking with the Micro:bit, for more on unicast, coding challenges and problems. 

Comments