LEARN.PARALLAX.COM
Published on LEARN.PARALLAX.COM (https://learn.parallax.com)
Home > Cybersecurity: Radio Basics

Cybersecurity: Radio Basics

What it’s about

Each micro:bit has a built-in radio it can use to exchange data. This tutorial shows how to use the Makecode radio module’s methods to make micro:bits exchange text. It also includes examples that change the radio channel and a bidirectional texting app.

Before you start

You will need:

  • Two or more micro:bit modules (on or off a cyber:bot)
  • Two USB A to MicroB cables
  • One or more computers with:
    • Access to makecode.microbit.org
  • Note that Google Chrome Labs Serial Terminal is used in this tutorial’s screen captures.

After you finish

You will be able to write Makecode projects that make micro:bit modules send and receive messages with their built-in radios. Your projects will also be able to change channels to access different data streams.

After this, you will also be ready to move on to the next tutorials:

  • Cybersecurity: Radio Data
  • Cybersecurity: Navigation Control from a Keyboard
  • Cybersecurity: Radio Tilt Control

Introducing the micro:bit radio

The micro:bit module’s main microcontroller is called the Nordic nRF51822 system on a chip (SOC). It’s the processor that runs your Makecode projects, but it also has a built-in radio. In Makecode, this radio can work with either the Bluetooth or the Nordic Gazell communication protocols. 

Bluetooth makes devices like wireless earbuds, mice, and keyboards pair with devices like computers, tablets and phones. After pairing, the two devices work much like their “wired” counterparts.

Nordic’s Gazell protocol allows more than just two micro:bit modules to be part of the conversation, and it also takes fewer memory resources, making room for more statements in your projects. 

Did you know?

A communication protocol is a set of rules two devices can follow to exchange information.

Bluetooth is named after a 10th Century Viking king named Harald “Bluetooth” Gormsson, who brought together various Danish tribes into a kingdom. Now, computers and phones use Bluetooth to bring various other devices together with its radio communication protocol.

micro:bit Radio Facts

  • The radio’s frequency is 2.4 GHz. This means that the radio waves emitted by the antenna oscillate at 2.4 billion times per second! (Imagine turning an LED on/off that fast…)
  • The 2.4 GHz “band” is a group of radio frequencies used by Bluetooth, Wi-Fi, and other devices. Even microwave ovens use 2.4 GHz, emitted at very high power to heat your food.
  • micro:bit modules can be configured to use certain channels, from 0 to 255. When micro:bit modules share the same channel, they can communicate. They will not interfere with micro:bit modules using other channels.
  • At maximum power, micro:bit modules can radio-communicate up to 230 feet line-of–sight (no obstacles or walls). The default power is great for use in the same room. 
  • The radio module for the micro:bit uses the Nordic Gazell protocol to send/receive data.
  • The radio module sends open data—not encrypted. Any micro:bit that’s close enough and set to the same channel can receive the messages your micro:bit sends.

Make micro:bit Radios Send and Receive

In this activity, a project will make one micro:bit repeatedly send messages through its radio. Another micro:bit will run a different project that receives those radio messages and prints them to its terminal. 

This block diagram shows how the one-direction application will work. The sender micro:bit prints its activity to the terminal, but that’s just so you can verify that its project is running. The important part is that the sender micro:bit transmits radio messages to a receiver micro:bit. That receiver micro:bit prints the messages it receives to its own terminal, where you can check and verify the radio data exchange.

Send Radio Messages

The best way to get started with testing the radio connection is to make one micro:bit module repeatedly broadcast messages. Once this project is running, the next section will have you load a project that receives radio messages and prints messages into a different micro:bit.

IMPORTANT! If you are doing this in a classroom, start with ONLY ONE micro:bit transmitting messages by running the project in this section. All the rest of the micro:bit modules should be running the project in the next section that receives and displays the messages.

  • Connect your micro:bit module to your computer with a USB cable.
  • Open a browser and go to makecode.microbit.org to match this tutorial’s screen captures.

Hardware Setup

  • Connect your micro:bit module to your computer with a USB cable.
  • Repeat with the second micro:bit module, either connecting it to a second computer, or to a second USB port on the same computer.

Software Setup

  • In a browser, go to makecode.microbit.org to open the micro:bit Makecode Editor.

Example project: send_radio_test_message

  • Set the name to send_radio_test_message and enter the project below.
    (See Save & Edit Projects and Flash Projects with MakeCode Editor.)
  • Click Download.
    (See Flash Projects with MakeCode Editor.)

  • Open another tab and go to Google Chrome Labs Serial Terminal.
  • Click connect at the top of the screen and choose the connected device which is the micro:bit.
  • Verify that the terminal repeatedly displays Send: Testing 123… followed by Done! on the next line. 
  • You may notice that some of the messages have random letters missing in the middle. Don’t worry about it, it’s an error on the terminals side not your code.
  • Either leave this project running, or be ready to power that micro:bit back up after you start running the receiver project in another micro:bit.

How send_radio_test_message Works

Let’s take a closer look at statements you probably haven’t seen in earlier activities. 

In this activity, we are just using the channel setting, which can be set from 0 to 255. Keep in mind that micro:bit modules can only radio-communicate when they are set to the same channel. Let’s say that one pair of micro:bit modules is running projects with channel = 10, and a second pair is using channel = 20. The channel = 10 micro:bit modules will not receive any messages sent by the channel = 20 micro:bit modules, and vice-versa. The Did You Know section later in this tutorial has more info about channels.

(In later activities, you will use the group and address settings to set up wireless networks. They will allow micro:bit modules to communicate in pairs or groups, even when they are sharing the same channel.)

The set message to (“Testing 123…”) statement creates a variable that stores a string with the “Testing 123…” characters. Next, serial write line (join (“Send: “)(message)) displays Send: Testing 123… in the terminal. Then, radio send string (message) uses the micro:bit radio to broadcast that “Testing 123…” text in the message variable.

What do you think would happen if you replaced the three blocks above with this? 

Although it would transmit the same message, you’d also have to update the serial write line statement. When it came time to change the message, you’d also have to do it by hand in two places. 

Before repeating the loop, serial write line (“Done!”) makes the terminal tell you that the project has reached the block that follows radio send string. It’s a way to verify that the message was sent by the radio. The pause(2000) makes it so messages are only broadcasted every 2 seconds. 

Receive Radio Messages

Now that we’ve got one micro:bit broadcasting radio messages every 2 seconds, let’s program another micro:bit to receive those messages and display them in the terminal. Both micro:bit modules have to be running their projects at the same time. The radio receiver micro:bit will also have to be connected to a terminal so that it can print what it receives.

  • Complete one of these three options: 
    • Option 1: Connect the micro:bit that will run this receiving project to a second computer and terminal.
      • Click the Connect button in a makecode.microbit.org editor and set up the connection to the receiver micro:bit.
      • Then connect to Google Chrome Labs Serial Terminal as well.
    • Option 2: Disconnect the micro:bit that’s sending radio messages from your computer, and connect it to a battery supply. Then, connect a micro:bit that’s going to recieve messages to your computer’s USB port and a terminal. 
      • After disconnecting the radio sender micro:bit from USB, connect it to a battery supply so that it continues to run the project that sends radio messages.
      • Connect the micro:bit that’s going to run the radio receiver project to your computer’s USB.
      • Click the Connect button in the makecode.microbit.org editor and set up the connection to the receiver micro:bit.
      • Then connect to Google Chrome Labs Serial Terminal as well.
    • Option 3: Connect the micro:bit that will run this receiving program to a second USB port on the same computer. Also connect it to a terminal. 
      • Unplug the micro:bit that’s sending messages from its USB port.
      • Connect the micro:bit that will receive messages to the USB port.
      • Click the Connect button in the makecode.microbit.org editor and set up the connection to the receiver micro:bit.
      • Then connect to Google Chrome Labs Serial Terminal as well.
      • After the receiver micro:bit has the makecode.microbit.org editor and the terminal connection, plug the sender micro:bit to a second USB port. At this point, you should have both sender and receiver micro:bit modules connected to USB ports. Since the sender micro:bit is connected to USB, but not to a makecode.microbit.org editor, it will only draw power to run its project and send it over radio. It will not interfere with the receiver micro:bit’s programming or terminal.

Now, you are ready to load a project into a second micro:bit for receiving radio messages.

Example project: receive_radio_test_message

  • Enter and flash the receive_radio_test_message project:

  • Go to the terminal tab and make sure it’s connected.
  • Verify that it displays the Receive: Testing 123… line, and adds a new line every 2 seconds.

How Receiving Messages Works

The micro:bit that’s receiving messages has to be set to the same channel as the one that’s sending messages.

Inside this on radio received loop, the statement will repeatedly check to find out if the radio received a message. It will only run through a message it received to the terminal once, and then go back to waiting for the next message.

A receivedString variable is created and set to the result that the radio receives. If a radio message was received, that result is a string. If it isnt a string then nothing will be shown and a different loop is needed to display it.

If the message variable is None at this point, the project will skip the statement that displays the message. On the other hand, if message has a string, the project will print it in the terminal. That’s because if <not< received string is empty>> only executes statements below and indented from it if on radio received copied a string into the message variable.

If the message variable does have a string, display it in the terminal with serial write line (join (“Receive: “)( message)).

Did You Know?

The radio set group (7) setting is actually the default channel. Classrooms with micro:bit modules can set up more than one broadcasting module, each set to a different channel that sends a different message. The rest of the class can run receiver projects that “tune in” to one of those broadcasting channels. For best results, keep the channels as widely separated as possible. For example, if there are five broadcasting micro:bit modules, use channels 0, 20, 40, 60, and 80. Each one should broadcast a unique message so that students can verify which micro:bit they are receiving messages from.

RF is an abbreviation for radio frequency.

The sender micro:bit is broadcasting. Broadcasting is one-to-many communication. One broadcasting unit, many receivers. True, if you just have one receiver, it’s actually one-to-one communication. However, if you have more than one micro:bit, they can also run projects that receive and print, which makes it one-to-many.

Here’s an excerpt from the micro:bit MicroPython Radio reference:

The radio set group (…) can be an integer value from 0 to 255 (inclusive) that defines an arbitrary “channel” to which the radio is tuned. Messages will be sent via this channel and only messages received via this channel will be put onto the incoming message queue. Each step is 1MHz wide, based at 2400MHz.

2400 MHz is a radio frequency that reverses the voltage applied to the antenna at 2400-million times per second. It’s another way of saying 2.4 GHz, which is 2.4-billion times per second. In terms of the numbers:

2400 M = 24 million = 2400 * 1,000,000 = 2,400,000,000
2.4 G = 2.4 billion = 2.4 * 1,000,000,000 = 2,400,000,000

Channel 0 sets the radio frequency to 2400 MHz, channel 1 to 2401 MHz, and so on up to channel 255 at 2655 MHz. In terms of GHz, that’s 2.401, 2.402, and so on through 2.483 GHz.

The channel setting also makes it possible to avoid a particular Wi-Fi network that might be crowding the band with radio traffic. Wireless routers use channels 1 through 14, with frequencies of 2412 through 2484 MHz (2.412 through 2.484 GHz).

Try This: Changing Frequencies

Changing frequencies is a common practice for aircraft and watercraft radio operators. As airplanes near certain airports, their pilots will switch their radio frequencies to communicate on the airport’s channel. The same applies to ships which use certain radio channels when communicating offshore and other channels when in certain ports.

The send_radio_test_message project can be modified to send different messages on different channels. Then, the receiver micro:bit will then display only the messages that go to whatever channel it’s set to. 

  • Duplicate and make changes to send_radio_test_message as shown below. 
  • Change the name of the project to send_radio_test_message_try_this.
  • Flash the modified project to the sender micro:bit.

  • Verify that the terminal display from the sender micro:bit resembles this:

  • Check the terminal displaying messages from the micro:bit that’s running the receive_radio_test_message project. 
  • Verify that the content resembles this:

  • Open and find this block in the receive_radio_test_message project:

  • Change it to this:

  • Change the project name to receive_radio_test_message_try_this.
  • Connect to the receiver micro:bit and flash the modified project.
  • Verify that its terminal output now resembles this:

Your Turn: Receive from Both Channels

The transmitter code in the Try This section is frequency hopping. For frequency hopping to work, both the transmitter and the receiver radios automatically cycle through a sequence of frequencies. It’s one step they can take to make it more difficult for others to listen in on conversations, and it’s especially important for law enforcement. In modern radios, the sequence of frequencies changes thousands of times per second.

Can you modify the project receive_radio_test_message_try_this to sync with the send_radio_test_message_try_this project’s frequency hopping? If you succeed, your project will capture and display messages from both channels.

Hints:

Change the radio set group call at the start of the program so that it uses a variable to set the frequency and makes a received variable, like this:

After the serial write line (join(“Receive: “)(message)) block inside the on radio received loop, add a set (received) to (1). Then in a forever loop add an if block to check if somethings been received. Then inside that add an if…else if… statement that checks the current channel and changes it to the next channel if it recieved a message. Be careful to make sure you set the radio group and reset the received variable:

Texting with Terminals

In this activity, you will first try an application of one-way (unidirectional) texting between terminals through a micro:bit-to-micro:bit radio link. Then, you will then expand it for two-way (bidirectional) texting. 

Here is the block diagram for unidirectional texting. You will type text into the terminal connected to the sender micro:bit. The sender micro:bit’s project will input those messages from the terminal and broadcast them with its radio. The receiver micro:bit will receive those messages, and print them to the terminal it’s connected to.

This activity is similar to the previous one since you’ll need to have two micro:bit modules running at the same time. But now, they both have to be connected to USB ports and terminal apps at the same time. So, you will either need to use two computers, each with a micro:bit connected to its USB, or use one computer with two micro:bit modules connected to two USB different ports and two browsers, each with a terminal app.

Unidirectional Texts

Time to choose your hardware setup!

  • Complete one of these:
    • Option 1: Connect each micro:bit to a different computer.
      • Use USB cables to connect a micro:bit to each computer.
      • In each computer, open the makecode.microbit.org editor, click the Connect button, and set up the connection.
      • Then connect to Google Chrome Labs Serial Terminal as well, make sure that you check the box at the top that says local echo.
    • Option 2: Connect the micro:bit modules to different USB ports on the same computer.
      • Use USB cables to connect two micro:bits to two USB ports on your computer.
      • Open two separate chrome browsers, and take each one to makecode.microbit.org.
      • Click the Connect button in one browser, choose one micro:bit from the list, and confirm by clicking Connect in the dialog.
      • Click the Connect button in the other browser, choose the other micro:bit from the list, and confirm by clicking Connect in the dialog.
      • Then in another browser connect to Google Chrome Labs Serial Terminal as well and choose one of the available micro:bits, make sure that you check the box at the top that says local echo.
      • Then in yet another browser connect to Google Chrome Labs Serial Terminal and choose the other micro:bit, make sure that you check the box at the top that says local echo.

If you accidentally connect both browsers to the same micro:bit, you might need to unplug/replug their UB cables and repeat the browser connecting steps.

It is time to decide which micro:bit you want to be the sender and which one you want to be the receiver. You will start with the browser connected to the receiver micro:bit

  • Open the receive_radio_test_message in the receiver micro:bit.
  • Update the project to your assigned channel if you are working in a classroom setting. (Each pair of students should share a channel. Channels should be evenly spaced in the 0…255 range.)
  • Flash the project.
  • Open the terminal browser and make sure your connected to the receiver. Make sure the terminal displays the micro:bit radio receiver message. 

Now, you will use the browser connected to the sender micro:bit.

  • Enter and flash the text_terminal_to_radio project to the sender micro:bit.

  • Open the terminal browser and make sure the local echo is checked.
  • Verify that the “micro:bit radio sender” message is printed.
  • Click in the transmitter terminal next to the Send: prompt and type a message like Hello! Then Press Enter.
  • Verify that the receiver micro:bit’s terminal displays Receive: Hello!
  • Try typing some more messages and verify that the receiver micro:bit is receiving them.
  • Try sending longer messages. Do all the characters make it through? How many can go through in one message?

Sender terminal

Receiver terminal

Bidirectional Texts

With unidirectional RF text working, the next step is bidirectional texting. Each micro:bit transceiver (transmitter/receiver) will take input that you type into its terminal and send it through the radio. Each micro:bit will also receive radio messages and print them to the terminal.

Example project: terminal_chat_through_microbits

  • If you are in a classroom, pair off and use your assigned channel.
  • Enter and flash the program below into the first micro:bit module (Transceiver A).
  • Update the first print statement, changing serial write line (“micro:bit transceiver A”) to serial write line (“micro:bit transceiver B”).
  • Flash the project into the second micro:bit module (Transceiver B).
  • Have fun typing messages to each other!

Transceiver A

 

Transceiver B

How Texting Works

The main loop in each program is constantly waiting for you to type a message and hit enter. It’s also constantly waiting for a radio message to be received. If there are no messages coming in then it will constantly be waiting for you to send one.

If a message is received and it is not empty, meaning you have gotten a new message, then a new line is started, the message is displayed by the serial write string (join(Receive: )(message)), and then it restores the Send: prompt. It then continues waiting for you to type a message and hit enter. Then, radio send string (tx) transmits that string over the radio for another micro:bit to receive and forward to its terminal.

Remember that on radio received either stores a string or None in message. If message has a string, then it’s displayed.

Did You Know?

UART — it stands for universal asynchronous receiver transmitter. UART communication is one of several common communication protocols that devices use to exchange data.

Buffers — Incoming messages to the UART are stored in a special string called a buffer that is set aside for keeping characters until you need them. The radio object also has a buffer.

TX & RX — The abbreviation tx is short for transmit. If you see tx in projects, you might also see rx, which is shorthand for receive. For example, the message variable could be named rx instead.

Dropped Packets — In its current form, the application drops packets sometimes. In other words, not every message you type will be received and displayed by the other micro:bit. 

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024


Source URL:https://learn.parallax.com/courses/cybersecurity-radio-basics_makecode/
Links