Makecode Coding and Thinkspeak

Foreword

When the IoT platform and hardware are ready, we can start coding with the Micro:bit.

The program can be divided into 3 parts:

  1. Connecting to the Internet.

  2. Connecting to the IoT Platform.

  3. 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.

MakeCode

Importing Extensions

Search for KittenBot

Import KittenWifi or KOI depending on your hardware

  請按自己的硬件選擇Powerbrick或Robotbit插件。

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.

Robotbit/IOBit

Sample Program

Armourbit

Sample Program


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.

pageThingSpeak

Setting Up ThingSpeak Platform

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

Publishing to ThingSpeak Channel

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 Program

Koi Sample Program

On the ThingSpeak page, you can see the published data.

Subscribing to ThingSpeak Channel

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 Program

KOI Sample Program

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 Update

Last updated