Thing Speak ESP8266

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

  1. How to Configuring ThingSpeak Cloud server Account ?
  2. 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.

  1. Connect to your WiFi hot spot having internet access.
  2. Read Sensor data
  3. 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

Create Channel Thing speak for ESP8266

Enter Name and Field. You may have multiple Fields depending on number of sensor create multiple fields such as Light, Temperature, Humidity, etc.
Enter Name and Label thing speak esp8266

Keep everything else as it is. Blank or default values. and click on Save Channel.thing speak 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.

api key esp8266 thing speak

This line is important for data upload to cloud server


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

For multiple sensors you need to add multiple fields as shown in below example

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,
 * 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 = "";
String apiKey = "Your API KEY";  //ENTER YOUR API KEY <<<<<<<<<<<
//                    Power on setup

void setup() {

  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

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {

  //If connection successful show IP address in serial monitor
  Serial.print("Connected to ");
  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");
   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";                
 //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

 //If data is available before time out read it.
 if(timeout < 500)
        Serial.println(client.readString()); //Response from ThingSpeak       
     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.

ThingSpeak Response on Serial Monitor

Step 4: Check Data on ThingSpeak Server

Open Your ThingSpeak Account and Click on Private View of Your Channel

thingspeak esp8266 dataupload

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 to hear from me.

6 thoughts on “Thing Speak ESP8266

  1. 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

    1. 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”;

Leave a Reply