When the IoT platform and hardware are ready, we can start coding with the Micro:bit.
The program can be divided into 3 parts:
Connecting to the Internet.
Connecting to the IoT Platform.
Main program.(Publish/Subscribe)
Robotbit was used when creating this tutorial, for Armourbit users, please refer to the instructions to modify the program.
Navigate to MakeCode’s page before continuing.
MakeCodearrow-up-right
Importing Extensions
Search for KittenBot
Import KittenWifi or KOI depending on your hardware
1. Connecting to the Internet
This section is about connecting the Wifibrick to the internet.
Select KittenWifi from the menu.
These blocks are needed to connect to the internet.
Create the following program.
Sample Programarrow-up-right
Sample Programarrow-up-right
Before moving on, it is recommended that you run this program once to ensure the module can connect to the Wifi network.
Registering a ThingSpeak Account
Follow the instructions to register for a ThingSpeak account.
ThingSpeak chevron-right Create a new Channel
Create a new channel.
Choose a name for your channel.
Other fields can be ignored, click Save Channel.
Open the Sharing tab.
Set the sharing settings to “Share channel view with everyone”.
The Access will change to “Public”.
Add a new device
Open the Devices menu, select MQTT.
Add a new device.
Select the channels accessible by this device and click Add Channel.
Click Add Device.
IMPORTANT! These MQTT Credentials are used for connecting to ThingSpeak! Please save or download the credentials as they can’t be seen after closing this page.
MakeCode Coding
Connecting to ThingSpeak
Build the following program and use these parameters to connect to ThingSpeak.
Host: mqtt3.thingspeak.com
ID: ThingSpeak Device Client ID
Username: ThingSpeak Device Username
Password: ThingSpeak Device Password
We need the Channel ID for publishing. The channel ID is displayed on the Channel page as a 7-digit number.
Build the following program to publish a data to ThingSpeak:
MQTT Topic: channels/[Channel ID]/publish
Message: field[field No.]=[Numeric Data]
WiFiBrick Sample Programarrow-up-right
Koi Sample Programarrow-up-right
On the ThingSpeak page, you can see the published data.
We can read the channel data by subscribing to the channel.
MQTT Topic: channels/[Channel ID]/subscribe/fields/field[field No.]
Press A to publish a message to the channel, Micro:bit will display the data received.
WiFiBrick Sample Programarrow-up-right
KOI Sample Programarrow-up-right
You are welcome to join our Whatsapp group to share your IoT projects!
Extension Version and Updates
There may be updates to extensions periodically, please refer to the following link to update/downgrade your extension.
Makecode Extension Updatearrow-up-right