In this article, we are going to learn how to Read ThingSpeak Channel using ESP8266 and Arduino. There are lots of example codes over the internet with data posting to ThingSpeak using ESP8266 and Arduino. But there are almost none that really read the channel feed using the ESP8266 Wi-Fi module and Arduino. So let’s find it out.
Disclaimer:
Handling electricity carries inherent risks. It’s essential to have the appropriate skills to manage it safely. Proceed at your own risk, as the author disclaims responsibility for any misuse, harm, or errors. All content on this website is unique and copyrighted; please avoid unauthorized copying. While most articles are open-source for your benefit, feel free to use the knowledge provided. If you find our resources helpful, consider purchasing available materials to support our work.
For assistance or guidance, leave a comment below; the author is committed to helping. Some articles may contain affiliate links that support the author with a commission at no additional cost to you. Thank you for your understanding and support.
Table of Contents
About ESP8266:
ESP8266 is a pretty popular WiFi module. There are different versions of this module.
Among these, the ESP-01 is the basic one and is used with Arduino or other microcontrollers in WiFi communications. Now-a-days, these are updated and other boards like Node MCU have arrived in the market.
But sometimes, it becomes necessary to work with the basic module.
About ThingSpeak:
ThingSpeak is an open-source software written in Ruby which allows users to communicate with internet-enabled devices. It facilitates data access, retrieval, and logging of data by providing an API to both the devices and social network websites.Â
With this online platform, you can make your IoT project within a few minutes. But if you look for an example, you’ll find the data posting only using the ESP-01 module.
But sometimes, you may need to read the channel feed from ThingSpeak. Here we can do that with the following codes:
Code:
#include "WiFiEsp.h" #include "ThingSpeak.h" char ssid[] = "SSID"; // your network SSID (name) char pass[] = "PSWRD"; // your network password WiFiEspClient client; #include "SoftwareSerial.h" SoftwareSerial Serial1(5, 6); // RX, TX // Channel details unsigned long myChannelNumber = 1509xx9; //your public channel number const char * myReadAPIKey = "KFKxxxK4FGOTISR"; //read API unsigned int FieldToMonitor = 6; //the field you want to monitor long count; void setup() { Serial.begin(115200); Serial1.begin(19200); Serial.print(F("Searching for ESP8266...")); WiFi.init(&Serial1); // check for the presence of the shield if (WiFi.status() == WL_NO_SHIELD) { Serial.println(F("ESP8266 not present")); // don't continue while (true); } Serial.println("found it!"); ThingSpeak.begin(client); // Initialize ThingSpeak } void loop() { // Connect or reconnect to WiFi if (WiFi.status() != WL_CONNECTED) { Serial.print(F("Attempting to connect to SSID: ")); Serial.println(ssid); while (WiFi.status() != WL_CONNECTED) { WiFi.begin(ssid, pass); Serial.print("."); delay(5000); } Serial.println(F("\nConnected")); } //read from channel count = ThingSpeak.readLongField(myChannelNumber, FieldToMonitor, myReadAPIKey); // Check the status of the read operation to see if it was successful int statusCode = 0; statusCode = ThingSpeak.getLastReadStatus(); if (statusCode == 200) { Serial.println("Read: " + String(count)); } else { Serial.println(F("Problem reading channel")); } delay(15000); // No need to read the counter too often. } //
Here, some libraries are used for this project. Get the WiFiEsp.h & ThingSpeak.h libraries.
ThingSpeak library is suitable for NodeMCU and most of the users over the internet posted articles on NodeMCU only using ThingSpeak. But to use the ESP8266 module with Arduino, we need to modify the code a little.
As you see here, we named the soft serial terminal as Serial1. Why? The ThingSpeak library calls this function when it is used with NodeMCU. As in NodeMCU, we can not change the soft serial port or pins, that is why this setting is not exposed in code rather than kept it inside the library. That is why we named it as Serial1 but you know, Arduino Nano, UNO, or pro-mini doesn’t have Serial1.
Commanding:
To give the command, we simply wrote a channel manually.
Goto API Keys, Select and copy the Write a channel feed link except for the GET. Paste it in your new tab.
Now, you need to edit the field number that field you want to update. In our code, you can check that we are reading the field6. So we’ll edit the link for field6.
Now, any value can be written.
T-Shirts:
Result:
Now, you can read the channel field with the ESP8266 module with the help of Arduino. I hope this article will help you in your IoT project. Thanks for reading.
For Professional Designs or Help:
You can check: Reading SMS with Arduino
0 Comments