DATA TRANSFER TO THINGSPEAK USING WIFI:BIT AND MAKECODE

WiFi:bit-easyC ecosystem

The appearance of IoT (Internet of Things) devices also caused the appearance of simple storage services, processing and data exchange. One of the providers of such services is the platform ThingSpeak. It provides the ability of creating communication channels in which various data can be sent and downloaded. There is also a graphical representation of these data, which can be publicly available and incorporated into your own solutions.
Within free usage of ThingSpeak, the number of channels is not limited. Along with the status and location, you can define up to 8 data in ThingSpeak. The values are limited to a maximum of 256 characters. There should be a pause of at least 15 seconds between sending data. About 8200 data messages can be sent per day, and a total of 10 million messages can be stored.

Using ThingSpeak is quite simple. First, as usual, log into the page and confirm your e-mail address. Then, create a channel, name it, add a description and decide which data it will contain. Right after that send some data. If you are interested in graphical representation, edit your channel according to your needs. All sent data can be read by other devices or you can download them for purposes of analysing in other tools.

A task: As part of these instructions, you will create an example of rolling dice experiment. If micro:bit is shaken, the result of dice roll will be shown at micro:bit (number from 1 to 6). By clicking the button A, the current result will be sent to ThingSpeak.

 

CREATING A CHANNEL ON THINGSPEAK

Once you have successfully completed the registration, make a channel which you will use for sending data from micro:bit. In the main menu click Channels and then My Channels. On the channel page click New Channel:

Click Save Channel and you channel is created. The channel page will open. For now, you are only interested in the key for sending data. Click on API Keys for access to it. It is called Write API Keys. Copy and save it somewhere temporarily.

CREATING A PROGRAM IN MAKECODE

Launch MakeCode and make a new project with the name of your choice. Go to More… (gear icon), and then Extensions. Search the iot-environment-kit and select the extension of the same name:

We are interested in set of commands ESP8266_IoT:
It contains these commands (the photo is below the description)
1. Command for serial connection of micro:bit and ESP on WiFi:bit. Since WiF:bit works at a different speed (115200) than the mentioned one (9600), you will use the basic MakeCode command instead.
2. Command for connecting micro:bit to a WiFi network. Instead of your ssid enter the network name, and instead of your key enter the WiFi network password. Once you have successfully connected to the network, WiFi:bit will furthermore, while starting, connect by itself. It means that you can execute this command once and then exclude it from the program.
3. Command for connecting to ThingSpeak. (Always precedes command number 4.)
4. Command for entering data which is sent to ThingSpeak. In this case, you only send one data so only the first value should be filled.
5. Command for sending data to ThingSpeak. (Always follows command number 4.)

Making a program is pretty simple. First you will serial connect micro:bit i WiFi:bit in the on start part of the program:
If you have not used this command so far, it is located in the Advanced.Serial set of commands. The next step is to write the part of the program which mimics rolling the dice and displays the result on micro:bit screen. It is a classic example of working with micro:bit. 

Since connecting to WiFi network should only be done once, when you first use this program, you will need to join it to the button B. Instead of name and password enter your own data:
What is left for you to do, is enter the part of the program which sends data to ThingSpeak. Join it to the button A. It consists of 3, previously described commands. In the second one, you should enter the, previously saved, API key and join the variable dice to the field1. Since sending data takes a few seconds, in the end you will display a tick icon, which will let you know that the data is sent.

This is how it all looks in the JavaScript part of the MakeCode: 

let kockica = 0
 
input.onGesture(Gesture.Shake, function () {
   basic.clearScreen()
   kockica = Math.randomRange(1, 6)
   basic.showNumber(kockica)
})
 
input.onButtonPressed(Button.B, function () {
   ESP8266_IoT.connectwifi("naziv", "lozinka")
})
 
input.onButtonPressed(Button.A, function () {
   ESP8266_IoT.connectthingspeak()
   ESP8266_IoT.tosendtext(
   "8L7CCVB7ENMT5R5S", /* change to your API key! */
   kockica,
   0,
   0,
   0,
   0,
   0,
   0,
   0
   )
   ESP8266_IoT.senddata()
   basic.showIcon(IconNames.Yes)
})
 
serial.redirect(
SerialPin.P16,
SerialPin.P8,
BaudRate.BaudRate115200
)

The executable .hex file can be found here.

 

THE REPRESENTATION OF SENT DATA

After several sent data, the classic ThingSpeak representation of data looks like this:

The type of graph can be changed and further described by title, data names, etc. Additionally, you can also add accompanying representations, such as the last value print:

The possibilities are not numerous, but they will provide a more simple insight into data. Channel data can be exported in JSON, XML and CSV formats and then displayed or processed in more detail in programs such as Excel. Data can also be directly accessed. Excel, for example, supports data retrieval over the link, and also their refreshing. In the example below, via link https://api.thingspeak.com/channels/700130/fields/1.csv?api_key=AC6RDIWKNTN2QXPI&results=5, the last 5 values are retrieved and then graphically displayed. (The link is downloaded and updated (results=5) from the site where you have downloaded your API writing key.) Clicking the Refresh All button, the data is refreshed and graphic representation is updated:

Find Excel .xlsx file here. For those of you who are more advanced, check a call set for the application Postman (ThingSpeak.postman_collection.json).