Aegidius
 Plüss   Aplulogo
   
 www.aplu.ch
 www.java-online.eu
     Print Text 
© 2019, V10.3
 
 
 
Examples 2: Cloud Computing
Arduino
 

 

Cloud Computing

In many IoT applications, the storage of sensor values in the cloud is of particular interest, because this data can then be retrieved from anywhere via the Internet.

We use the cloud server ThingSpeak, which is available free of charge for small data amounts. In addition, this cloud service offers Mathworks (MatLab distribution), which data makes it easy to graphically display data as part of an website.

To use the services, you must create a personal account with ThingSpeak (www.thingspeak.com). The data is organized in tables called channels. First you define a new channel by selecting a name (e.g. Sensor Data) and a field descriptor (column name, e.g. Temperature (degC)). Under Channel Sharing Setting, select Share channel with everyone. Under API Keys note the the Write and Read API Keys.

Under API Request you get the information how to enter a new value (Update a Channel Feed) using a HTTP or HTTPS GET request, e.g.

https://api.thingspeak.com/update?api_key=Y03F0Y1IPUKP6XYZ&field1=25

You may copy this line to the browser URL and see the result under My Channels.

ex2_1 ex2_2

The following program connects to na access point and sends a new temperature value every 20 s (for a free account the maximum data rate is 1 sample every 15 s). The temperature is also shown on the display of the micro:bit's and written into the console. The HTTP response received from the cloud server is shown in the console. It is the total number of channel values.

// ThingSpeakTemp.ino

#include <linkup.h>
#include <sht.h>

const char* key = "TJH7REA0WQD0QC5P";
char buf[64];

void setup()
{
  Serial.begin(9600);
  Wire.begin(); 
  char reply[128];
  Serial.println("Connecting...");
  connectAP("raspilink", "aabbaabbaabb", reply);
}

void loop()
{
  float temp;
  float humi;
  getTempHumi(temp, humi);    
  char temp_s[20];
  dtostrf(temp, 4, 1, temp_s);
  strcpy(buf, "New temperature: ");
  strcat(buf, temp_s);
  strcat(buf, " C");
  Serial.println(buf);
  char url[256];
  strcpy(url, "http://api.thingspeak.com/update?api_key=");
  strcat(url, key);
  strcat(url, "&field1=");
  char buf[20];
  dtostrf(temp, 4, 2, buf);
  strcat(url, buf);
  Serial.println("Sending url:");
  Serial.println(url);
  char response[10];
  httpGet(url, response);
  Serial.print("Response: ");
  Serial.println(response);
  delay(20000);
}
 

You can retrieve the channel data of a public channel with a HTTP GET request, e.g. the last 100 entries with

https://api.thingspeak.com/channels/859260/feeds.json?results=100  

where the number is the Channel ID coming from your ThinkSpeak account. The data is json-formattet and will display well in a browser if you use the above URL. (Don't forget that your channel must have the Share option view with everyone enabled).

Clicking the small window icon in the Field 1 Chart opens a link that displays the graph the browser. You can copy this link to any browser in the world to see your channel data.

ex8_1
ex8_2