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:
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:
Data Received on Thingspeak:
Channel Settings that you need to do:
Go to your Thingspeak account and do the following setting to receive temperature and humidity data.
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.
Read API Key:
You need your read API key of that channel.
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:
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.
- 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:
- 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.
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:
- Open the serial monitor; you will be able to see the values read from your channel.
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:
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.
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.
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.
ESP8266WiFi.h
download dimana ya?
download dari web sebelah malah cuma support remoteXY
Terima kasih
How to Download
i am download from another web but only support to using RemoteXY
Hi,
I couldn’t fully understand your question Please ask us once more either in your native language or in English.
Regards
oh sorry my language is bad
I can’t upload this program because an error #include esp8266wifi.h
and when I downloaded the esp8266wifi.h library from another site
my library still causes file errors that support incomplete esp8266wifi.h
and then I tried testing with example -> thingspeak -> esp8266 -> board program directly -> readfield
but the program still cannot be uploaded with the same problem
maybe you can upload your library or share your website recommendations
Hi,
It would have been great if you copy paste the error here and mentioned which board you are using: NodeMCU or Generic ESP8266.
Anyway, What I believe is that you didn’t select the correct board like NodeMCU or Generic ESP8266 before you compile the code and not Arduino uno or mega or something else.
We face similar issue because of wrong board selection. This could be the reason why the IDE did not take the mentioned header file.
The next issue you may face is not able to upload to the board. If you face this you didn’t press the “rest” and “flash” button in the correct sequence.
Regards
Thanks to your response
Hi,
How to get data from Thingspeak in float value numbers with a decimal? (47.2 or 45.5)
Now receiving data from Thingspeak page are only in integers (47 or 45).
Hi Ramunas,
Replace long with float where necessary:
long humidity = ThingSpeak.readLongField(counterChannelNumber, FieldNumber2, myCounterReadAPIKey);
To
float humidity = ThingSpeak.readLongField(counterChannelNumber, FieldNumber2, myCounterReadAPIKey);
Hi,
I found, need to replace with Float
float humidity = ThingSpeak.readLongField(counterChannelNumber, FieldNumber2, myCounterReadAPIKey);
to
float humidity = ThingSpeak.readFloatField(counterChannelNumber, FieldNumber2, myCounterReadAPIKey);
Thanks Ramunas for your correction.
Hello,,
I wanna ask to you,
Why you make const int fieldnumber1 = 1? What the function of constanta?
Can i change the constanta to other number?
Hi,
const in C language means the number or character that you stored in the variable is not going to change or there is no need for change.
const is used to save memory allocated for that variable. In this program there are plenty of memory left in Arduino and you can remove “const” but a good programmer will always optimize memory.
Regards
Hi,
I try to read data from Thingspeak using your code for 2 different channel. Channel 1 contains 4 fields while Channel 2 contains 2 fields.
However, I only manage to read data from Channel 2. I have check the APIkey, channel ID, and debug the code. But I can’t find the cause of it.
Do you have any suggestion regarding this?
Thank you.
Hi,
Are you trying to read data from multiple channels at once? Technically that is possible.
What error message are you getting? like no internet..etc..
Regards
C:\Users\Muhammad Khalish\Documents\Arduino\libraries\thingspeak-arduino-master\src/ThingSpeak.h:132:7: note: candidate: bool ThingSpeakClass::begin(Client&, unsigned int)
bool begin(Client & client, unsigned int port)
C:\Users\Muhammad Khalish\Documents\Arduino\libraries\thingspeak-arduino-master\src/ThingSpeak.h:132:7: note: candidate expects 2 arguments, 1 provided
exit status 1
no matching function for call to ‘ThingSpeakClass::begin(bool)’
hi. im facing lot of these kind problems. can you help me?
i think i use wrong library
Hi,
Indeed it seems you have used some other library. Add the mentioned Thingspeak library the code will compile just fine.
Also do not modify the code that you did not understand.
Regards
Hi.. Could you tell me how to send a string from my own android app to thingspeak and to then send the string via nodemcu to arduino?
In your code for reading data from thingspeak using nodemcu, do we have to connect to arduino as well?
Could you help me?
Hi,
Currently we have to investigate how to send string to thingspeak, may be we’ll do a tutorial in future.
Arduino and NodeMCU are two different boards but programmed using Arduino IDE software. NodeMCU is a stand alone board and you no need to connect to Arduino board.
Regards
Am I have to connect the esp like that or can I not connect the IO0 and reset and tx to rx,rx to tx?
Hi,
Could you please rephrase your query, we couldn’t understand it.
I am connecting it 3.3V ->3.3V, RX->TX , TX->RX , EN -> 3.3V , GND -> GND. I am not connecting GPO0 and RST pins. And I sellect the Arduino Uno for board type. And code is not working. It dont return the field value.
Hi,
Without connecting GPIO0 and RST how can you upload the code? You should connect these two buttons.
Code is not working? You need to download the library file mentioned and add it to your IDE.
Regards
Hey! It started working. I thing the problem is pinMode(9,OUTPUT). I delete that line it worked. But I have a question. How can I power the led(whic is the 9th pin) when thw value is equals to 1?
pinMode(9,OUTPUT) ????? Did you made any changes to the existing code?
I connect the GPO2 to led’s +. And I added pinMode(2,OUTPUT), digitalWrite(2,HIGH). And it worked. Thanks.
Great!
Excuse me, I want to know what is the meaning of status code , can you explain it for me ?
thnak you
Hi,
Status code is a code from thingspeak that tell us about the status of a uploaded value like updated or failed.
Regards
sorry , I got another question . After I uploaded my code to esp8266, then oepn the serial monitor it came out with garbled text . (I have changed baud rate of the code and serial monitor)
i have fixed my problem , thanks for your tutorial !!
That’s good to hear..
I have a problem
I want to make a device that can control lights (3-5W) with a smartphone.
first, i can send commands from smartphone to thingspeak
second, i need to be able to read data from thingspeak to arduino.
the problem is in point two
how can i read data from thingspeak to ide serial monitor with ESP8266-01 and Arduino UNO?
I am using the library but it keeps getting errors like it can’t be used.
Can someone help me? before that I thank you, I hope you are in good health.
Hi,
We are also trying to do something similar and we’ll be posting a article soon or later, but it won’t be with a smartphone control but with a IoT remote. We need a Android / iOS developer to accomplish the project with a smartphone which will be done in far future.
Regards
Hi!
I installed the ThingSpeak library provided but I’m having the error “statusCode was not declared in this scope”, do you think ypu can help me please?
Thanks
Can you please tell me which code are you referring to?
I followed everything step by step. My first problem was when i verified the code, it couldnt compile because statusCode was not declared. I solved this problem by declaring int statusCode; Next, once i verify and upload the code i have reveived these error messages. Please help me torubleshoot the problem.
raise FatalError(‘Failed to connect to %s: %s’ % (self.CHIP_NAME, last_error))
esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header
esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header
There are few possibilities:
1) You did not press the buttons in the correct sequence.
2) You are trying to program wrong code to wrong ESP8266 board (Generic or NodeMCU).
3) You did not install the package files correctly / got installed improperly because there are no errors in the code in the first place.
hi
I faced a problem
first, I succeeded to write data to channel in thingspaeak but I failed to read data
I using a different code than you can you help me please to solve it
I can send the code to you
I used Arduino UNO and ESP8266-1
Hi,
I can only debug the code present in this website because it is the only code I understand fully and debugging others code is very time consuming.
Regards
thanks a lot
thank you for your project
in your project, you show the reading data in LCD, what if I need to use the value to show in Arduino output pin
thanks in advance
You cannot “show the value in Arduino pin” or Please rephrase your question…….
I read data from the channel in thingspeak but I need also to make some mathematical operation in this data and output it on uno output pin,
Yes, you can do that, but we are not sure what do you meant by “output it on uno output pin”, you mean on the serial pins?
yes, thanks for your reply I did it and it works successfully
Great!
thanks for all. it works perfectly. but i need to received last 50 or 100 data form my chart field 1 . how can i get those data to my serial monitor .
We will try to provide a solution in our next update…
which Required fields are marked.
i have no website
You no need any sort of website…..
Why are you not reading the data using ESP8266 rather than using a NodeMCU?
We can read from generic ESP8266 or from any other wi-fi board. NodeMCU is easiest for beginners.
brother iam getting only one const value from that particular field nut it is not getting new values. why?????
Hi, if no new values are getting upload to Thingspeak in real-time, then the last value will be read perpetually.
hi, i want to on led on connected to nodemcu on D7 pin based on certain temperature value. how can thingspeak automatically send command to nodemcu to on led when temperature rises? any idea
Hi,
You need to write an if condition and check the value and turn ON the LED. You just need to read the value and compare with your threshold value, no need to send any special commands.