Communication Protocols
Communication Protocol
We can make a parallel with human communication.
A protocol is a set of rules that ensure information is well understood. We can highlight two extremes:
- structured human conversation
- incoherent messages, hubbub
- sending global messages
- sending personal messages
- creating a discussion group
Using micro:bit cards
We will use micro:bit cards to practice a communication network simulation. They allow us to approach the question with useful simplifications:
- no network to configure
- radio communication, so wireless
- using Python
The radio module
The radio module of micro:bit cards is very simple to use. A code example:
from microbit import *
import radio
radio.on() # Activate radio
radio.config(channel=10) # Set a channel (10 here)
while True:
if button_a.was_pressed():
radio.send("Hello!") # Send a message
message = radio.receive() # Check regularly for message reception
if message:
print(message) # Display received message in console
Explanations:
- the
radio
module contains functions to interact with the micro:bit card's radio - the
radio.config()
function allows configuring the radio - the
radio.on()
function allows activating the radio - the
radio.send()
function allows sending data by radio
Communications without protocol
In this first exercise, we will get familiar with the editor interface, ensure proper functioning of sending a program to a card, and set up our first chat discussion system.
Instructions
The objective is simple:
- create a program to join channel 10
- the microbit card detects button A press - if button A is pressed, it asks the user for the message to send (
input()
function) - once the result ofinput()
has been stored in a variable, we send the content of this variable withradio.send()
- to avoid multiple sends, we can use immediately afterradio.send()
thesleep(100)
function which will delay the program - withradio.receive()
, we detect message reception, which we'll display withprint()
in the console
Setting up a protocol
A communication protocol will easily allow us to improve our messages to reach our objective: a large forum where you can communicate with whoever you want, knowing precisely who is speaking to you and to whom you are addressing.
We remind some useful functions:
- the
split()
function allows dividing a string into a list of strings. The argument is the separator.
message = "Hello! How are you?"
message.split('.') # Will give a list: ["Hello ", " How are you?"]
- the
+
operator allows concatenating strings:
author = "Mathieu"
message = "Hello"
frame = author+message
# frame will equal "MathieuHello"