5. Chapter 1: Upload Data to ThingSpeak

ThingSpeak is IoT platform for user to gather real-time data; for instance, climate information, location data and other device data. In different channels in ThingSpeak, you can summarize information and visualize data online in charts and analyze useful information.
ThingSpeak can integrate IoT:bit (micro:bit) and other software/ hardware platforms. Through IoT:bit, you can upload sensors data to ThingSpeak (e.g. temperature, humidity, light intensity, noise, motion, raindrop, distance and other device information).

auto_fit

5.1. Thingspeak Configuration


Goal:

we need to create the thingspeak channel and get the key

Step 1

Go to https://thingspeak.com/, register an account and login to the platform

auto_fit

Step 2

Choose Channels -> My Channels -> New Channel

auto_fit

Step 3

Input Channel name, Field1 , then click “Save Channel”

  • Channel name: smart-house

  • Field 1: Temperature auto_fit

Step 4

You will see a chat for data field1

auto_fit

Step 5

Open your web browser, go to https://thingspeak.com , select your channel > “API Keys” ,copy the API key as follows:

auto_fit

5.2. Coding (Makecode)


Step 1

Before we do the thingspeak uploading part, we already know how to connect to the WiFi on the first chapter.
auto_fit

Step 2

On every 15 seconds, if the WiFi is connected, it will send data to ThingSpeak.

  • Use if-statement inside the forever loop to check WiFi connected? status

  • Snap the Send Thingspeak key..... block inside the if-statement

  • Fill in the api key from Thingspeak and the temperature values

  • Snap pause to the loop to wait 15 second

auto_fit

Step 3

If you want to show the thingspeak status, you can use the on Thingspeak Uploaded handler to get the variable for status and error code. You may use the OLED to display the status and error code.

  • Go to OLED Tab

  • Snap initialize OLED with width 128 height 64 to on start

  • Snap the show string inside the On Thingspeak Uploaded

  • Draw the variable from On Thingspeak Uploaded to the show string block placeholder.

auto_fit

Full Solution

MakeCode: https://makecode.microbit.org/_PHU0oJLwHAYD

You could also download the program from the following website:

5.3. Result


After Connected to Internet, the program start upload the data to Thingspeak.

When the Upload is finish, it will shown the status OK and Error Code 0.
auto_fit

If the thingspeak cannot be uploaded successfully, the status will return FAIL with error code.
For the error 400, the user inputted the wrong API Key. For the error code -28674, there is no internet connection.
auto_fit

The charts in your channel in ThingSpeak will be updated. auto_fit