ThingSpeakā¢ is an IoT analytics cloud platform service that allows you to aggregate, visualize and analyze live data streams in the cloud. ThingSpeak provides instant visualizations of data posted by ESP8266 to ThingSpeak. ThingSpeak is often used for prototyping and proof of concept IoT systems that require analytics.
In this tutorial, We will learn following thinds
- How to Configuring ThingSpeak Cloud server Account ?
- How to program ESP8266 to upload sensor data ?
Lets understand the cloud concept first. Uploading of ESP8266 sensor data is done using Internet. It is three step process.
- Connect to your WiFi hot spot having internet access.
- Read Sensor data
- Upload data to ThingSpeak
Step 1: Sign up ThingSpeak
Its simple just enter your email id and verify your account.
Step 2: Configuring ThingSpeak
Configuration is just few clicks job
Step 2.1: Create New Channel
Click on New Channel
Enter Name and Field. You may have multiple Fields depending on number of sensor create multiple fields such as Light, Temperature, Humidity, etc.
Keep everything else as it is. Blank or default values. and click on Save Channel.
Step 2.2: Getting API Key
Click on API Key Tab and look for these two fields Write Api Key and Update channel feed line.
This line is important for data upload to cloud server
GET https://api.thingspeak.com/update?api_key=akjshfajkhfowei&field1=0
First parameter is Api Key. Do not share API key it makes decision of which user it is. In most cases I create own cloud server as per requirements. Second parameter is field1=0 here you can pass the ADC value ex. field1=1234
value in front of field1=0 is your sensor data for example if your adc value is 1234 then you call this GET request with https://api.thingspeak.com/update?api_key=yourapikey&field1=1234
For multiple sensors you need to add multiple fields as shown in below example
https://api.thingspeak.com/update?api_key=yourapikey&field1=1234&field2=442
Step 3: Programming ESP8266 to upload data to ThingSpeak cloud server
Make Changes in program for API KEY, SSID and PASSWORD
/* * HTTP Client * Copyright (c) 2015, circuits4you.com * All rights reserved. /* Connects to WiFi HotSpot. */ #include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266WebServer.h> /* Set these to your desired credentials. */ const char *ssid = "yournetworkSSID"; //ENTER YOUR WIFI SETTINGS <<<<<<<<< const char *password = "yourPassword"; //Web address to read from const char *host = "api.thingspeak.com"; String apiKey = "Your API KEY"; //ENTER YOUR API KEY <<<<<<<<<<< //======================================================================= // Power on setup //======================================================================= void setup() { delay(1000); Serial.begin(115200); WiFi.mode(WIFI_STA); //This line hides the viewing of ESP as wifi hotspot //WiFi.mode(WIFI_AP_STA); //Both hotspot and client are enabled //WiFi.mode(WIFI_AP); //Only Access point WiFi.begin(ssid, password); //Connect to your WiFi router Serial.println(""); Serial.print("Connecting"); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } //If connection successful show IP address in serial monitor Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); //IP address assigned to your ESP } //======================================================================= // Main Program Loop //======================================================================= void loop() { WiFiClient client; const int httpPort = 80; //Port 80 is commonly used for www //--------------------------------------------------------------------- //Connect to host, host(web site) is define at top if(!client.connect(host, httpPort)){ Serial.println("Connection Failed"); delay(300); return; //Keep retrying until we get connected } //--------------------------------------------------------------------- //Make GET request as pet HTTP GET Protocol format String ADCData; int adcvalue=analogRead(A0); //Read Analog value of LDR ADCData = String(adcvalue); //String to interger conversion String Link="GET /update?api_key="+apiKey+"&field1="; //Requeste webpage Link = Link + ADCData; Link = Link + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"; client.print(Link); delay(100); //--------------------------------------------------------------------- //Wait for server to respond with timeout of 5 Seconds int timeout=0; while((!client.available()) && (timeout < 1000)) //Wait 5 seconds for data { delay(10); //Use this with time out timeout++; } //--------------------------------------------------------------------- //If data is available before time out read it. if(timeout < 500) { while(client.available()){ Serial.println(client.readString()); //Response from ThingSpeak } } else { Serial.println("Request timeout.."); } delay(5000); //Read Web Page every 5 seconds } //=======================================================================
Upload Program and Open Serial monitor with baud rate of 115200. First it will show connection status to your wifi router then It uploads data to server and displays response from server. Do not try to upload data at very high rate ThingSpeak server will simply blocks it. 5 Seconds update time is okay.
Step 4: Check Data on ThingSpeak Server
Open Your ThingSpeak Account and Click on Private View of Your Channel
You did it well….Thats it….You can Create multiple Fields and Upload multiple sensor data. and also you can create multiple channels for multiple Data Nodes.
In case of any problem upload your project on Steps2Make.com to hear from me.
Hi sir…Can u plz tell me the reason why do the sensor values are not getting uploaded to thingspeak though they are appearing on serial monitor. What might be the problem
Check your API key, Field names
Good day sir… I have a question. Can you kindly explain how to create multiple fields and upload multiple sensor data from esp8266.
int airPollution=1234; //For floating point use double data type
int humidity=32;
int temperature=25;
String Link=”GET /update?api_key=”+apiKey; //Requeste webpage
Link = Link + “&field1=” + String(airPollution);
Link = Link + “&field2=” + String(humidity);
Link = Link + “&field3=” + String(temperature);
Link = Link + ” HTTP/1.1\r\n” + “Host: ” + host + “\r\n” + “Connection: close\r\n\r\n”;
client.print(Link);
delay(100);
Thanks for the post. Which ESP 8266 module did you use?
NodeMCU ESP-12E