How to Send Data to Thingspeak Using ESP8266

In this post we are going to learn how to send sensor data to Thingspeak IoT platform using ESP8266 and Arduino. In a previous tutorial we saw how to send data to thingspeak using GSM modem and Arduino which didn’t require a Wi-Fi hotspot. In this post we will learn how to connect your Arduino projects to Wi-Fi and observe data in real time on Thingspeak.

We will see:

  • What is ESP8266 Wi-Fi Module?
  • What is Thingspeak platform, how to create an account and get API key?
  • How to upload code to ESP8266 module?
  • How to install ESP8266 board package to Arduino IDE?
  • Circuit diagram for uploading a code to ESP8266.
  • Possible errors while uploading a code to ESP8266 and solutions.
  • Final circuit diagram for sending temperature and humidity data to thingspeak.
  • Program codes for ESP8266 and Arduino.

UPDATE: An easier method on How to Send Data to Thingspeak (without sensor) using ESP8266 and NodeMCU is explained here.

Quick overview: What we are going to do?

  • We are going to send temperature and humidity data to thingspeak using DHT11, ESP8266 module and Arduino board.
  • We will learn how to upload a program code to ESP8266 module; the uploaded program code on ESP8266 will accept serial data from Arduino which will be forwarded to Thingspeak platform via Wi-Fi connection.
  • The Arduino board will collect temperature and humidity data from DHT11 sensor and will be sent serially to ESP8266 module.
  • We will be uploading two sets of program codes: One for Arduino and another for ESP8266 module. We will be utilizing the same Arduino board to upload the program code to ESP8266.
Block Diagram of sending data to Thingspeak
Block Diagram for sending data to Thingspeak

What is ESP8266 Wi-Fi Module?

ESP8266
ESP8266

ESP8266 module is a microcontroller board with Wi-Fi capability which is designed to operate as a standalone device as well as a slave device when it is connected to a microcontroller (master).

Beginners may think ESP8266 module is a slave device and need a microcontroller to make it functional, but this is not always true. The ESP8266 can operate as a standalone device as it sport a 32-bit microcontroller chip, RAM, GPIO pins and a flash memory which can be programmed using Arduino IDE.

But this module have only two GPIO pins and may limit our number of sensors and peripherals which we may connect to it. If you are going to connect one or two (max) sensors then you may not need any additional microcontroller to support the sensors, you can directly add the library files (if necessary) and write a code just as you do for any arduino boards.

As we need more than two pins for the sensors, we are using the ESP8266 as a slave device; this is done by uploading a program code to ESP8266 which will accept serial data from arduino. We will see how to upload the code to ESP8266 using Arduino Uno board in detail later part of this article.

Now let’s take a look at its specifications of ESP8266.

Specifications:

  • ESP8266 module has its own SoC or system on chip, it utilizes Tensilica L106 32-bit processor clocked at 80MHz.
  • It has 32KB instruction RAM, 32KB instruction cache RAM, 80KB user data RAM and 16 KBETS system data RAM.
  • It can operate from 2.5V to 3.6V (nominal 3.3V) and 5V will kill the board. It has average current consumption about 80mA.
  • It supports 802.11n (2.4 GHz) and can communicate with a speed of 72.2 Mbps (max).
  • It supports these network protocols: IPv4, TCP/UDP/HTTP.

Pin Diagram of ESP8266:

ESP8266 Pin diagram
ESP8266 Pin diagram

The pin diagram is also printed on the back of ESP8266 module, which makes our tasks much easier.

Pin Diagram of ESP8266
Pin Diagram of ESP8266

By now you would have got an idea on ESP8266 module and you might have understood that we have to upload a program code to ESP8266 to make it operational either as a standalone or as a slave device.

What is Thingspeak platform, how to create an account and get API key?

If you are just exploring these things for the first time please click here and read the following topics in the post.

  • What is Thingspeak IoT analytics Platform?
  • What is API Key?
  • How to Sign up for Thingspeak and get API Key

NOTE: Please follow the channel settings in the mentioned tutorial link and replicate the same channel set-up for your thingspeak account for this tutorial.

How to upload code to ESP8266?

As we mentioned at the beginning of this tutorial ESP8266 is a microcontroller board with Wi-Fi capability and we need to upload some program code to make it functional.

So now we are going upload a program code which is written using Arduino IDE and we are going to use Arduino Uno board’s build-in programmer to upload the code to ESP8266.

Circuit Diagram for uploading code to ESP8266:

Circuit diagram for uploading code to ESP8266
Circuit diagram for uploading code to ESP8266

You need to wire up the circuit as per the above schematic, you will need 2 push buttons and don’t forget to remove the ATmega328P from the board. Double check the voltage supply to ESP8266 as it operates on 3.3V and 5V will kill the board.

Uploading code to ESP8266
Uploading code to ESP8266

The ESP8266 will get slightly warm after you connect it to the power supply which is normal but, if it gets very hot then you might have probably messed with its wire connections/supply voltage.

Once the hardware is ready we need to install the ESP8266 board package, now let’s see how to do that.

How to install ESP8266 board package to Arduino IDE?

This package will make the Arduino IDE compatible with ESP8266 module.

  •  A window will popup:

    boards manager
    Boards manager
  • Type “ESP8266” on the box as shown and you will get installation option, select the latest version and click install.
  • Now the IDE will download the necessary packages and this could take more than 5 minute to complete.
  • Now go to Tools > Board > select “Generic ESP8266 Module”.
  • Now copy the given below ESP8266 program code and paste it on to Arduino IDE software and insert your SSID and Password of your Wi-Fi hotspot and Copy the “write API key” and “channel ID” from your thingspeak account and insert it to the code.
  • Now press compile button (Green tick button). The compilation of code may take more than couple of minutes so be patient. If the compilation failed please check whether have you selected the “Generic ESP8266 Module” in the board option or not.
  • After successful compilation of the code, connect the Arduino to your PC and go to Tools > Port and make sure that correct COM port is selected.
  • Now Press and hold the FLASH button and press RESET button momentarily once and leave the FLASH button. By doing this ESP8266 will get ready for a new program to be uploaded.
  • Now click upload, the code will compile again and begin to upload and uploading could take 1 -2 minutes.
  • Once the code is successfully uploaded, it looks something like this:

Uploading to ESP8266

  • The orange colored text is not error or warning, but the uploading progress bar.
  • Now the code is successfully uploaded to ESP8266 Module and now it can accept the data serially via its UART pins.

Download this Thingspeak library before you compile the code: Click here

Program code for ESP8266:

// ----------(c) Electronics-project-hub-------- //
#include "ThingSpeak.h"
#include <ESP8266WiFi.h>

//------- WI-FI details ----------//
char ssid[] = "xxxxxxxxx"; //SSID here
char pass[] = "yyyyyyyyy"; // Passowrd here
//--------------------------------//

//----------- Channel details ----------------//
unsigned long Channel_ID = 12345; // Your Channel ID
const char * myWriteAPIKey = "ABCDEF12345"; //Your write API key
//-------------------------------------------//

const int Field_Number_1 = 1;
const int Field_Number_2 = 2;
String value = "";
int value_1 = 0, value_2 = 0;
int x, y;
WiFiClient  client;

void setup()
{
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  ThingSpeak.begin(client);
  internet();
}

void loop()
{
  internet();
  if (Serial.available() > 0)
  {
    delay(100);
    while (Serial.available() > 0)
    {
      value = Serial.readString();
      if (value[0] == '*')
      {
        if (value[5] == '#')
        {
          value_1 = ((value[1] - 0x30) * 10 + (value[2] - 0x30));
          value_2 = ((value[3] - 0x30) * 10 + (value[4] - 0x30));
        }
      }
    }
  }
  upload();
}

void internet()
{
  if (WiFi.status() != WL_CONNECTED)
  {
    while (WiFi.status() != WL_CONNECTED)
    {
      WiFi.begin(ssid, pass);
      delay(5000);
    }
  }
}

void upload()
{
  ThingSpeak.writeField(Channel_ID, Field_Number_1, value_1, myWriteAPIKey);
  delay(15000);
  ThingSpeak.writeField(Channel_ID, Field_Number_2, value_2, myWriteAPIKey);
  delay(15000);
  value = "";

}
// ----------(c) Electronics-project-hub-------- //

Possible Errors you may get while uploading to ESP8266:

There is a good chance that you could fail to upload the code in the first attempt, here is a possible error you may get:

Upload Error ESP8266
Upload Error ESP8266

If you see this error message in your IDE, the code did not get uploaded.

Solutions:

  • You did not press the flash and reset button in the correct sequence.
  • You did not connect TX and RX terminals properly. Please note that Tx should go to Tx and Rx should go to Rx. Also check other the wire connections too.
  • The board could have damaged because you applied 5V to the board mistakenly before, the only possible solution for this is to replace the ESP8266 module.
  • If your code did not compile successfully in the first place, you did not select the correct board type. Go to Tools > Board > select “Generic ESP8266 Module”.

 Explanation for the ESP8266 Code:

The code will accept the data if it only can find “*” and “#” in the data stream. “*” is the start character and “#” is the end character. The below given code accepts 4 integer data after the “*” and stop accepting after the “#”.

      if (buffer1[0] == '*')
      {
        if (buffer1[5] == '#')
        {
          Serial.println(buffer1);
          data1 = ((buffer1[1] - 0x30) * 10 + (buffer1[2] - 0x30));
          data2 = ((buffer1[3] - 0x30) * 10 + (buffer1[4] - 0x30));
        }
      }

The incoming data stream should be like this *2345# here, 23 is temperature and 45 is humidity. You can open serial monitor with the same upload setup and enter *2345#, Thingspeak will plot graph at 23 on temperature graph and 45 at humidity graph.

Please note that only four digits will accepted in between * and # with the above code and no decimal point should be send.

If you want to send 3 digits per graph then please refer this code:

      if (buffer1[0] == '*')
      {
        if (buffer1[7] == '#')
        {
          Serial.println(buffer1);
          data1 = ((buffer1[1] - 0x30) * 100 + (buffer1[2] - 0x30)*10 + (buffer1[3] - 0x30));
          data2 = ((buffer1[4] - 0x30) * 100 + (buffer1[5] - 0x30)*10+ (buffer1[6] - 0x30));
        }
      }

Please note that after “*” there should be precisely 6 digits (no less or no more) otherwise data won’t be sent.

Full circuit diagram for sending data to Thingspeak using Arduino and ESP8266:

Sending data to thingspeak using ESP8266
Sending data to thingspeak using ESP8266

The above circuit is self-explanatory, just wire up as per the above circuit diagram. Please double check Vcc to ESP8266 it must be 3.3V and 5V can kill the module.

Download this DHT library for DHT11 sensor: Click here

Program code for Arduino:

//-------------Electronics-project-hub>com-------------//
#include <dht.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
dht DHT;	
#define DHTxxPIN 2
int ack;
void setup()
{
  Serial.begin(9600);
  mySerial.begin(115200);
}
void loop()
{
  ack = 0;
  int chk = DHT.read11(DHTxxPIN);
  switch (chk)
  {
    case DHTLIB_ERROR_CONNECT:
      ack = 1;
      break;
  }
  if (ack == 0)
  {
    Serial.print("Temperature(*C) = ");
    Serial.println(DHT.temperature, 0);
    Serial.print("Humidity(%) = ");
    Serial.println(DHT.humidity, 0);
    Serial.println("-------------------------");
    //------Sending Data to ESP8266--------//
    mySerial.print('*'); // Starting char
    mySerial.print(DHT.temperature, 0); //2 digit data
    mySerial.print(DHT.humidity, 0); //2 digit data
    mySerial.println('#'); // Ending char
    //------------------------------------//
    delay(2000);
  }
  if (ack == 1)
  {
    Serial.print("NO DATA");
    Serial.print("\n\n");
    delay(2000);
  }
}
//-------------Electronics-project-hub>com-------------//

Upload the above code to Arduino Uno board after inserting ATmega328P to the board. Don’t forget to select the board back to Arduino Uno, you can do this by going to Tools> boards> Arduino Uno, otherwise you will get compilation error.

Outcome of the above project:

You can open serial monitor and see DHT11’s data:

Serial monitor output
Serial monitor output

Graph generated on Thingspeak:

Graphs on Thingspeak
Graphs on Thingspeak

Prototype Image:

Send data to Thingspeak using ESP8266
Send data to Thingspeak using ESP8266

Troubleshooting:

If you can’t see any data on your Thingspeak’s private view, then you should check wheather your ESp8266 is connected to your Wi-Fi hotspot or not. You can do this by checking the connected devices in your router’s admin panel.

Your connected ESP8266 on admin panel will be like ESP, followed by some numbers as shown below:

NOTE 1: Once you complete reading or accomplishing this project, it is equally important to know  How to Read Data from Thingspeak

NOTE 2: You can also send data to thingspeak without wifi by using GSM module, click here to get the program code and circuit.

If you have any question regarding this project, feel free to ask us. You can anticipate a guaranteed reply from us. 

Avatar photo

My nick name is blogthor, I am a professional electronics engineer specialized in Embedded System. I am a experienced programmer and electronics hardware developer. I am the founder of this website, I am also a hobbyist, DIYer and a constant learner. I love to solve your technical queries via comment section.