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:

Advertisements

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.

For Professional Designs or Help:

Loading

You can check: Reading SMS with Arduino


MKDas

Mithun K. Das; B. Sc. in EEE from KUET; Head of R&D @ M's Lab Engineering Solution. "This is my personal blog. I post articles on different subjects related to electronics in the easiest way so that everything becomes easy for all, especially for beginners. If you have any questions, feel free to ask through the contact us page." Thanks.

0 Comments

Leave a Reply

Avatar placeholder

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