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: Electricity is always dangerous. Proper skill is required to work with electricity. Do work at your own risk. The author will not be responsible for any misuse or harmful act or any mistake you make. The contents of this website are unique and copyright protected. Kindly don’t do any nonsensical act copying and claiming it as yours. Most of the articles published here are kept as open-source to help you. Take the knowledge for free and use it, but if you are interested you can buy the ready resources offered here. If you need any help or guide feel free to comment below, the author will try to help you. Thanks.

About ESP8266:

ESP8266 is a pretty popular WiFi module. There are different versions of this module.

ESP8266 proteus library

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. 

ESP8266 ThingSpeak

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.

ESP8266 ThingSpeak

Goto API Keys, Select and copy the Write a channel feed link except for the GET. Paste it in your new tab.

ESP8266 ThingSpeak

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:

ESP8266 ThingSpeak
ESP8266 ThingSpeak
ESP8266 ThingSpeak

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.

Loading

JLCPCB – Only $2 for PCB Prototype (Any Color)

24 Hours fast turnaround, Excellent quality & Unbeatable prices

$18 Welcome Bonus for new registrations Now!!! https://jlcpcb.com


You can check: Reading SMS with Arduino


Lab Projects BD

This is a personal electronics blog. The author loves to write on different topics and shares his knowledge to help others.

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *