The ThingSpeak Server - uraich/IoT4AQ GitHub Wiki
Sending Results to the Cloud
Introduction
ThingSpeak is a cloud based service that allows you to collect and analyse data streams from Internet connected sensors. It is provided by (c) MathWorks, the company behind MatLab. A MathWorks account must be registered before being able to use the services. Free accounts are available but have certain limitations.
Channels
In order to create a dash board with widgets that show the results from your sensor you must create a ThingSpeak channel. With a free account you may have up to four channels maximum.
Each channel can have a maximum of eight fields, which corresponds to eight sensor values.
For each field that you enable, a chart will be created. These charts display the values that have been received by ThingSpeak for the corresponding field. A limited number of widgets is available showing the current state of the sensor.
For the IoT4AQ demo I have created a gauge for temperature and humidity in addition to the charts for
- temperature
- humidity
- pm 1.0, pm 2.5, pm 10 dust concentration measurements
Sending data to ThingSpeak
ThingSpeak gives you the choice to communicate with its server either using the REST API (REpresentational State Transfer) or the MQTT (Message Queue Telemetry Transport) protocol. During this workshop we will restrict ourselves to the REST API, which conforms to the HTTP protocol. In order to access the channel you need a number of keys. First you need the channel ID which is given at the top of the dashboard screen.
When a channel is created it is considered private and you need API keys to get access to it. Click on the API Keys tab, which will show you a key for reading and another one for writing. If you want to clear the channel from previous data or you want to remove the channel altogether using the REST API, you need the user API key in addition. You can get it clicking on the user button (UR at the top right) and selecting My Profile. You can also make a channel public and give access to anybody. Have a look at Channel Settings to find out how to accomplish this.
The ThingSpeak library
Like for sensor readout or the LCD display, there is a ThingSpeak Arduino library, which hides or the protocol details from you. In my demo I created secret.h, a separate include file, containing all the keys and passwords:
const char *ssid = "your SSID"
const char *password = "your password"
const unsigned long channel_ID = 2413223;
const char *API_write_key = "WWWWWWWWWWWWWWWW";
const char *API_read_key = "RRRRRRRRRRRRRRRR";
const char *API_user_key = "UUUUUUUUUUUUUUUU";
In the Arduino source code we must include "ThingSpeak.h" as the last include file. Then the WiFi connection must be established, a WiFi client must be created and the ThingSpeak library must be initialized (which is of course done in setup()).
WiFiClient client; // created as a global variable
void setup(){
// initialization of Serial, sensors ...
ThingSpeak.begin(client);
}
Finally we can set the channel fields, send the REST message to ThingSpeak and check if the message has been correctly received (HTTP_SUCCESS is defined to be 200, which is the HTTP code for success).
ThingSpeak.setField(1, temperature_f);
ThingSpeak.setField(2, humidity_f);
ThingSpeak.setField(3, pm_1_0_f);
ThingSpeak.setField(4, pm_2_5_f);
ThingSpeak.setField(5, pm_10_f);
// write to the ThingSpeak channel
int retCode = ThingSpeak.writeFields(channel_ID, API_write_key);
if(retCode == HTTP_SUCCESS){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(retCode));
}