How to Read Data from Thingspeak: Arduino-ESP8266-NodeMCU

In this post we are going to learn how to read sensor data from Thingspeak using generic ESP8266 and Arduino and also using NodeMCU. We will learn how to fetch the uploaded data and print it on serial monitor and also on 16×2 LCD display.

We will see:

  • Reading values using generic ESP8266 and Arduino.
  • Reading values using NodeMCU with serial monitor.
  • Reading values using NodeMCU with LCD display.

Block Diagram:

Reading Data from Thingspeak Block Diagram
Reading Data from Thingspeak Block Diagram

To read values from Thingspeak we need to upload some data in real time, to do this, I am uploading temperature and humidity data to Thingspeak using GSM. You can upload data to Thingspeak using ESP8266 or NodeMCU, I choose to upload data using GSM because I had just one Wi-Fi network at home and I don’t want to send and receive the data on the same Wi-Fi network, so that I can show you guys that a faithful data transmission and reception occurred via Thingspeak and not data transferred locally in a Wi-Fi network.

If you are enthusiastically landed on this post, then probably you already know how to send data to Thingspeak, if not please check out the links below after you finish reading this post.

1) Send data to Thingspeak using GSM.

2) Send data to Thingspeak using ESP8266 and Arduino.

A glance on how I am going send data to Thingspeak:

I am going to send data to two fields: Temperature and humidity. Below is the circuit setup what I have used, you can checkout circuit diagram and program code in the first link given above.

Sending Data to Thingspeak:

Sending Data to Thingspeak
Sending Data to Thingspeak

Data Received on Thingspeak:

Thingspeak data
Thingspeak data

Channel Settings that you need to do:

Go to your Thingspeak account and do the following setting to receive temperature and humidity data.

Thingspeak Channel Settings
Thingspeak Channel Settings

Scroll to the bottom of this page and press save to save your settings.

Now you need to collect some information from your Thingspeak account to receive data from your channel, these channel information will be entered in the program code.

Channel ID:

You need your channel ID to read the fields on your channel you wish to read.

Channel ID
Channel ID

Read API Key:

You need your read API key of that channel.

Thingspeak API Keys
Thingspeak API Keys

Reading Data from Thingspeak Using Generic ESP8266 and Arduino board:

Here I am going to use an ESP8266 module and an Arduino board who’s microcontroller IC is removable. We need to upload a program to ESP8266 module to read data from Thingspeak; the Arduino board will receive and display the data on serial monitor.

Download the Thingspeak Library: Click here

Program code (Applicable to Generic ESP8266 and NodeMCU):

//--------------Electronics-project-hub-------------//
#include "ThingSpeak.h"
#include <ESP8266WiFi.h>
const char ssid[] = "xxxxxxxxx";  // your network SSID (name)
const char pass[] = "xxxxxxxx";   // your network password         
WiFiClient  client;

//---------Channel Details---------//
unsigned long counterChannelNumber = 12345;            // Channel ID
const char * myCounterReadAPIKey = "xxxxxxxxxxxxxxxx"; // Read API Key
const int FieldNumber1 = 1;  // The field you wish to read
const int FieldNumber2 = 2;  // The field you wish to read
//-------------------------------//

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

void loop()
{
  //----------------- Network -----------------//
  if (WiFi.status() != WL_CONNECTED)
  {
    Serial.print("Connecting to ");
    Serial.print(ssid);
    Serial.println(" ....");
    while (WiFi.status() != WL_CONNECTED)
    {
      WiFi.begin(ssid, pass);
      delay(5000);
    }
    Serial.println("Connected to Wi-Fi Succesfully.");
  }
  //--------- End of Network connection--------//

  //---------------- Channel 1 ----------------//
  long temp = ThingSpeak.readLongField(counterChannelNumber, FieldNumber1, myCounterReadAPIKey);
  statusCode = ThingSpeak.getLastReadStatus();
  if (statusCode == 200)
  {
    Serial.print("Temperature: ");
    Serial.println(temp);
  }
  else
  {
    Serial.println("Unable to read channel / No internet connection");
  }
  delay(100);
  //-------------- End of Channel 1 -------------//

  //---------------- Channel 2 ----------------//
  long humidity = ThingSpeak.readLongField(counterChannelNumber, FieldNumber2, myCounterReadAPIKey);
  statusCode = ThingSpeak.getLastReadStatus();
  if (statusCode == 200)
  {
    Serial.print("Humidity: ");
    Serial.println(humidity);
  }
  else
  {
    Serial.println("Unable to read channel / No internet connection");
  }
  delay(100);
  //-------------- End of Channel 2 -------------//
}
//--------------Electronics-project-hub-------------//

NOTE: Change the following credentials in the code:

const char ssid[] = "xxxxxxxxx";  // your network SSID (name)
const char pass[] = "xxxxxxxx";   // your network password         

//---------Channel Details---------//
unsigned long counterChannelNumber = 12345;            // Channel ID
const char * myCounterReadAPIKey = "xxxxxxxxxxxxxxxx"; // Read API Key

Circuit Diagram:

Reading Data from Thingspeak Circuit
Reading Data from Thingspeak Circuit

How to upload program to ESP8266:

You need to wire-up the circuit as illustrated above and upload the given code to read data from Thingspeak.

Reading From Thingspeak Circuit
Reading From Thingspeak Circuit
  • The IC must be removed from the Arduino board.
  • You should have the ESP8266 package installed on your Arduino IDE, if not click this link and read this topic: How to install ESP8266 board package to Arduino IDE?
  • Now Press and hold the flash button, using another finger press the reset button for a second and leave it, after this you can leave the flash button, this will make the ESP8266 module ready to upload a new code.
  • Select “Tools” > “Board” > select “Generic ESP8266”. Select the correct COM port.
  • Select the baud rate to 115200.
  • Click upload (which will take 2 min or more to compile and upload)
  • You will see this after successful upload:
Program uploaded to ESP8266
Program uploaded to ESP8266
  • Now open the serial monitor, you will see data from Thingspeak as illustrated:

Walla! Now you successfully read data from your Thingspeak channel.


NOTE: A circuit must be uploading data to the same channel while you reading it on serial monitor. 

Reading Data from Thingspeak using NodeMCU:

In this section we are going read data from Thingspeak using NodeMCU and see the data on the serial monitor.

Note: We are uploading the same program code to NodeMCU which we uploaded to generic ESP8266 previously. 

NodeMCU
NodeMCU

How to upload code to NodeMCU:

  • Connect NodeMCU to PC via micro USB cable.
  • You should have ESP8266 package installed on your Arduino IDE, if not click this link and read this topic: How to install ESP8266 board package to Arduino IDE?
  • To make your NodeMCU ready to upload a new code, Press and hold the flash button, now press the reset button for a seconds and leave it. Now leave the flash button.
  • Select “Tools” > “Board” > select “Generic ESP8266”. Select the correct COM port.
  • Select the baud rate to 115200.
  • Click upload (which will take 2 min or more to compile and upload)

On successful program upload you will see this:

Program uploaded to ESP8266
Program uploaded to ESP8266
  • Open the serial monitor; you will be able to see the values read from your channel.
Reading values from Thingsepak
Reading values from Thingsepak

Read Data from Thingspeak Using NodeMCU and LCD:

In this section we will see how to read Thingspeak channel data using NodeMCU and 16×2 LCD display. To make things easier I have used I2C module for LCD which will reduce the wire connections to 2 wires (SCL and SDA).

Circuit Diagram:

NodeMCU with LCD
NodeMCU with LCD

The circuit is self-explanatory, D1 is SCL and D2 is SDA which need to be connected with respective terminals of I2C module. The I2C module need 5V supply which will be provided by Vin pin of the NodeMCU, if you are powering it via USB.

Note: If you have LoLin V3, Vin pin does not provide proper 5V but “VU” pin provides 5V).

Download the LCD I2C Library: Click here

Program code:

//-----------Electronics-project-hub-------------//
#include "ThingSpeak.h"
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const char ssid[] = "xxxxxxxxx";  // your network SSID (name)
const char pass[] = "xxxxxxxxx";   // your network password
int statusCode = 0;
WiFiClient  client;
LiquidCrystal_I2C lcd(0x27, 16, 2);

//---------Channel Details---------//
unsigned long counterChannelNumber = 1234;            // Channel ID
const char * myCounterReadAPIKey = "xxxxxxxxxxxxx"; // Read API Key
const int FieldNumber1 = 1;
const int FieldNumber2 = 2;
//-------------------------------//

void setup()
{
  WiFi.mode(WIFI_STA);
  ThingSpeak.begin(client);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Display Test:OK");
  delay(1000);
}

void loop()
{
  //----------------- Network -----------------//
  if (WiFi.status() != WL_CONNECTED)
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Connecting to ");
    lcd.setCursor(0, 1);
    lcd.print(ssid);
    delay(1000);
    while (WiFi.status() != WL_CONNECTED)
    {
      WiFi.begin(ssid, pass);
      delay(5000);
    }
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Conn.. to Wi-Fi");
    lcd.setCursor(0, 1);
    lcd.print("Succesfully.");
    delay(1000);
    lcd.clear();
  }
  //--------- End of Network connection--------//

  //---------------- Channel 1 ----------------//
  long temp = ThingSpeak.readLongField(counterChannelNumber, FieldNumber1, myCounterReadAPIKey);
  statusCode = ThingSpeak.getLastReadStatus();
  if (statusCode == 200)
  {
    lcd.setCursor(0, 0);
    lcd.print("Temperature:");
    lcd.print(temp);
    lcd.print("*C");
  }
  else
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Unable to read");
    lcd.setCursor(0, 1);
    lcd.print("or No internet!");
  }
  delay(100);
  //-------------- End of Channel 1 -------------//

  //---------------- Channel 2 ----------------//
  long humidity = ThingSpeak.readLongField(counterChannelNumber, FieldNumber2, myCounterReadAPIKey);
  statusCode = ThingSpeak.getLastReadStatus();
  if (statusCode == 200)
  {
    lcd.setCursor(0, 1);
    lcd.print("Humidity:");
    lcd.print(humidity);
    lcd.print("%   ");
  }
  else
  {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Unable to read");
    lcd.setCursor(0, 1);
    lcd.print("or No internet!");
  }
  delay(3000);
  //-------------- End of Channel 2 -------------//
}
//-----------Electronics-project-hub-------------//

NOTE: Change the following data in the code:

const char ssid[] = "xxxxxxxxx";  // your network SSID (name)
const char pass[] = "xxxxxxxx";   // your network password      

//---------Channel Details---------//
unsigned long counterChannelNumber = 12345;            // Channel ID
const char * myCounterReadAPIKey = "xxxxxxxxxxxxxxxx"; // Read API Key
  

Now upload the code using previously given instruction for NodeMCU; on successful code upload you will be able to see the data from two fields of your channel.  

Reading data from Thingspeak
Reading data from Thingspeak

While uploading the program code to NodeMCU, a warning could popup saying that I2C library is written for AVR architecture may not work with ESP8266 NodeMCU, please ignore this warning.

Issues that could arise:

You may find that serial monitor or the LCD saying “unable to read / no internet” when you power your circuit.

Thingspeak data on Serial Monitor
Thingspeak data on Serial Monitor
Thingspeak Read Error
Thingspeak Read Error

This is normal it will take some time to read your channel, be patient wait for couple of minutes or so, sometimes it read right away. If this error message persist for more than 4 minutes, please disconnect the power and reconnect it after few seconds.

Once it updates value, it is the latest value uploaded by the sending device.

What will happen when the uploading circuit goes offline?

The last updated value will be read again and again by the receiving circuit.

How long will it take to update a new data on the receiving circuit?

With a decent internet, it should not take more than 30 seconds [sender uploading data to Thingspeak and read by the receiving circuit]. Usually it will take less than 20 seconds.    

If you have any questions regarding this project, feel free to say in the comments, you will get 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.