# 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](images/Ch1/Ch1_des.png)

## Thingspeak Configuration


Goal:

we need to create the thingspeak channel and get the key Step 1

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

![auto_fit](images/Ch1/Ch1_reg1.png)

Step 2

Choose Channels -> My Channels -> New Channel

![auto_fit](images/Ch1/Ch1_reg2.png)

Step 3

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

* Channel name: smart-house * Field 1: Temperature ![auto_fit](images/Ch1/Ch1_reg3.png)

Step 4

You will see a chat for data field1

![auto_fit](images/Ch1/Ch1_reg4.png)

Step 5

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

![auto_fit](images/Ch1/Ch1_reg5.png)

## 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](images/Ch1/Ch1_p2.png)

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](images/Ch1/Ch1_p3.png)

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](images/Ch1/Ch1_p4.png)

Full Solution

MakeCode: [https://makecode.microbit.org/S50897-87530-58227-65452](https://makecode.microbit.org/S50897-87530-58227-65452)

You could also download the program from the following website:

## 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](images/Ch1/Ch1_result1.png)

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](images/Ch1/Ch1_result1_1.png)

The charts in your channel in ThingSpeak will be updated. ![auto_fit](images/Ch1/Ch1_result2.png)