Wireless Remote Weather Station with micro:bit
Experiment 1: Sending and Receiving a Signal
Introduction
There are a few methods of transmitting data between two or more micro:bits. You can send a number or a string. If you are unfamiliar with strings, it is basically a sequence of characters that make up a word.
If you followed along with the wireless remote micro:bot, you will remember that we simply sent one number to control the robot. For the scope of this tutorial, we will be transmitting the values from more than one sensor so we will want to know what number is associated with the reading. To do that, we will send a string and number pair between two micro:bits. We will animate the LEDs for each micro:bit as an indicator.
radio
code blocks? Try looking at the MakeCode reference guide!
Parts Needed
You will need the following parts:
- 2x micro:bit Boards
- 2x Micro-B USB Cables
For a few items listed, you will need more than one unit (i.e. micro:bits, micro-B USB cables, etc.). You may not need everything though depending on what you have. Add it to your cart, read through the guide, and adjust the cart as necessary.
1.1: Sending
For this part of the experiment, we are going to send a string and number from one micro:bit to another micro:bit on the same channel.
Hardware Hookup
Connect the first micro:bit to your computer via USB cable.
Running Your Script
We are going to use Microsoft MakeCode to program the micro:bit. You can download the following example script and move the *.hex file to your micro:bit. Or use it as an example to build it from scratch in MakeCode.
Code to Note
When the micro:bit is powered up, we'll want to set the radio to a certain channel. In this case, we head to the Radio blocks
, grab the radio set group __
block, and set it to 1
. Later (in the next experiment) we'll have the transmitting micro:bit that is attached to the weather station continuously transmit data to a receiving micro:bit. In this section, however, we'll continuously send a string and number pair out - the word string
will be sent out with the number 1
. For feedback, we will use the Basic
blocks to have the LEDs animate with a dot expanding out into a square to represent a signal being sent out. Once the animation is finished, we will clear the screen. We will add a short delay with pause _____
from the basic blocks to give the second micro:bit some time to handle the data before sending out data again. We will set it to 1000ms.
1.2: Receiving
For this part of the experiment, we are going to have a second micro:bit receive a string and number from the first micro:bit.
Hardware Hookup
To avoid confusion when uploading code, unplug the first micro:bit from your computer. Then connect the second micro:bit to your computer via USB cable.
Running Your Script
Download the following example script and move the *.hex file to your micro:bit. Or use it as an example to build it from scratch in MakeCode. You will need to open a new project to receive the data from the first part of this experiment.
Code to Note
When the second micro:bit is powered up, we'll want to set the radio to the same channel as the first micro:bit. In this case, it will be 1
. Using the on radio received name value
block, we will want to check to see what signal was sent from the first micro:bit. In this case, we will want to check to see if the string that was received and stored in name is what we expect. Using the if
statement from the Logic
blocks, we will want to check to see if name matches word string
by using the "_" = "_"
block. If it matches, we will want to see if the value matches 1
by using the ___=___
. Note the differences in the two comparisons. One compares a string while the other compares a number.
If both match, we will want some sort of indicator. Visually, using LEDs would be the easiest so an animation of a square shrinking to a dot was chosen to represent the received signal. Once the animation is finished, we will want to clear the screen until we receive another signal.
What You Should See
Powering both micro:bits up, the first micro:bit (our transmitting) will send a string and number pair out on channel 1
. In this case, the pair was string
and 1
, respectively.
The second micro:bit (our receiving) will take that string and number pair that was sent on channel 1
and do something based on the condition statement. In this case, there is an animation using the LED array to indicate when we have received the pair.