Micro Chat
Introduction
Use the radio to send and receive messages with other micro:bit.
Set a radio group
The first thing you want to do is to put in a ||radio:set group||
. This will make certain that you and another micro:bit will receive just the messages assigned to that group and not the messages sent from some other micro:bit that’s not part of your conversation. This like tuning to a particular channel to talk on.
radio.setGroup(1)
Sending a message
Use ||input:on button pressed||
to send a text message over radio with ||radio:send string||
.
Every micro:bit nearby will receive this message.
input.onButtonPressed(Button.A, function() {
radio.sendString(":)");
});
Receiving a message
Add a ||radio:on received string||
block to run when a message is received.
radio.onReceivedString(function (receivedString) {
})
Displaying text
Add a ||basic:show string||
to display the string on the screen. Pull the ||variables:receivedString||
out of ||radio:on received string||
and put it into ||basic:show string||
.
radio.onReceivedString(function (receivedString) {
basic.showString(receivedString);
})
Testing in the simulator
Press button A on the simulator, you will notice that a second micro:bit appears (if your screen is too small, the simulator might decide not to show it). Try pressing A again and notice that the “:)” message gets displayed on the other micro:bit.
input.onButtonPressed(Button.A, function() {
radio.sendString(":)");
});
radio.onReceivedString(function (receivedString) {
basic.showString(receivedString);
})
Try it for real
If you have two micro:bits, download the program to each one. Press button A on one and see if the other gets a message.
radio