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:
We will use micro:bit cards to practice a communication network simulation. They allow us to approach the question with useful simplifications:
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:
radio module contains functions to interact with the micro:bit card's radioradio.config() function allows configuring the radioradio.on() function allows activating the radioradio.send() function allows sending data by radioIn 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:
input() function) - once the result of input() has been stored in a variable, we send the content of this variable with radio.send() - to avoid multiple sends, we can use immediately after radio.send() the sleep(100) function which will delay the program - with radio.receive(), we detect message reception, which we'll display with print() in the consoleA 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:
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?"]
+ operator allows concatenating strings:author = "Mathieu"
message = "Hello"
frame = author+message
# frame will equal "MathieuHello"