Using ESP8266 with Thingspeak platform

 ThingSpeak is an IoT platform that aggregates, visualizes, and analyzes real-time data streams in the cloud. You can send data to ThingSpeak from your devices, create instant real-time data visualizations, and send alerts.


Using ESP8266 with Thingspeak platform

In this project, we will try to make ESP8266 friends with the Thingspeak platform. We will connect ESP8266 to DHT11 and send temperature and humidity data using MQTT to Thingspeak platform over the internet.


The platform primarily targets IoT projects and data analytics using visual elements. To start using Thingspeak's free services, you first need to register using your email ID, once done you will see the following page.


Using ESP8266 with Thingspeak platform

Now let's look at some terms that can help you better understand Thingspeak and make your experience with these web servers more convenient.


API Key: To keep the data secure and prevent anyone from accidentally reading / writing the data to your server, there must be some type of security / password, and the API key is for that. An API key is a long alphanumeric key that is required to read / transmit data to the server.


Channel: A channel in Thingspeak is a software analog of the IoT hardware device that you connect to Thingspeak, in our case the ESP8266 will use one entire channel of our bandwidth. With a free Thingspeak account, you can have a maximum of 4 channels.


Field: Each channel has 8 fields. The field is a variable and stores / separates the data type, for example, when we send temperature and humidity from our device to the server, both parameters will use one field for each channel.


Copy and save the recording API key, we will need it later when testing the communication with Thingspeak.


Using ESP8266 with Thingspeak platform

To organize communication, we also need MQTT. MQTT is a lightweight data transfer protocol that we can use for the same purposes for which we use Thingspeak. Mosquitto is an organization that provides a free MQTT server / broker for testing purposes. More information can be found at Mosquitto.org. In this article, we will not delve into how MQTT works.


Using ESP8266 with Thingspeak platform

Now let's go directly to testing.


Connect DHT11 to pin D0 of the ESP8266, don't forget about the power lines. After that, using the Arduino IDE, load the following code to write information to thingspeak.



#include <dht.h>
#include <ESP8266WiFi.h>

String api_key = "YOUR API KEY";     
const char *ssid =  "YOUR WIFI SSID";     
const char *pass =  "YOUR WIFI PASSWORD"; 
const char* server = "api.thingspeak.com";

#define DHTPIN 0
dht DHT;

WiFiClient client;
 
void setup() 
{
       Serial.begin(115200);
       delay(10);
       pinMode(2, OUTPUT);
       digitalWrite(2, 0);
       Serial.println("Connecting to ");
       Serial.println(ssid);
 
       WiFi.begin(ssid, pass);
 
      while (WiFi.status() != WL_CONNECTED) 
     {
            delay(500);
            Serial.print(".");
     }
      Serial.println("");
      Serial.println("WiFi connected");
 
}
 
void loop() 
{
      int chk = DHT.read11(DHTPIN);
      float h = DHT.humidity;
      float t = DHT.temperature;
      
              if (isnan(h) || isnan(t)) 
                 {
                     Serial.println("Failed to read from DHT sensor!");
                      return;
                 }

               if (client.connect(server,80))   //   "184.106.153.149" или api.thingspeak.com
            {  
                   String data_to_send = api_key;
                    data_to_send += "&field1=";
                    data_to_send += h;
                    data_to_send += "&field2=";
                    data_to_send += t;
                    data_to_send += "\r\n\r\n";

                   client.print("POST /update HTTP/1.1\n");
                   client.print("Host: api.thingspeak.com\n");
                   client.print("Connection: close\n");
                   client.print("X-THINGSPEAKAPIKEY: " + api_key + "\n");
                   client.print("Content-Type: application/x-www-form-urlencoded\n");
                   client.print("Content-Length: ");
                   client.print(data_to_send.length());
                   client.print("\n\n");
                   client.print(data_to_send);
                   delay(1000);
                   Serial.print("Temperature: ");
                   Serial.print(t);
                   Serial.print(" degrees Celcius, Humidity: ");
                   Serial.print(h);
                   Serial.println("%. Send to Thingspeak.");
              }
          client.stop();
 
          Serial.println("Waiting...");
  
  delay(10000);
}

Don't forget to include in the platform API code. Now after successfully loading the code in the serial monitor, you will receive messages like the following image. The module first connects to WiFi and then sends data to the server after reading parameters from DHT11.


Using ESP8266 with Thingspeak platform

On the Thingspeak page, you will be able to see entries like the following image.


Using ESP8266 with Thingspeak platform

If you want to go the other way and control things from Thingspeak with an ESP8266 and read the server data, you can use the following code.



#include "ThingSpeak.h"
#include <ESP8266WiFi.h>

const char* ssid     = "SSID";  //  Wi-Fi
const char* password = "PASSWORD;   //  Wi-Fi
unsigned long channel =742359;  // 

unsigned int led1 = 1;
unsigned int led2 = 2;
unsigned int led3 = 3;
WiFiClient  client;
void setup() {
  Serial.begin(115200);
  delay(100);
  
  pinMode(D1, OUTPUT);
  pinMode(D2, OUTPUT);
  pinMode(D3, OUTPUT);
  digitalWrite(D1, 0);
  digitalWrite(D2, 0);
  digitalWrite(D3, 0);
 
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.print("Netmask: ");
  Serial.println(WiFi.subnetMask());
  Serial.print("Gateway: ");
  Serial.println(WiFi.gatewayIP());
  ThingSpeak.begin(client);
}
void loop() {
 
  int led_1 = ThingSpeak.readFloatField(channel, led1);
  int led_2 = ThingSpeak.readFloatField(channel, led2);
  int led_3 = ThingSpeak.readFloatField(channel, led3);
 
  if(led_1 == 1){
    digitalWrite(D1, 1);
    Serial.println("D1 is On..!");
  }
  else if(led_1 == 0){
    digitalWrite(D1, 0);
    Serial.println("D1 is Off..!");
  }
 
  if(led_2 == 1){
    digitalWrite(D2, 1);
    Serial.println("D2 is On..!");
  }
  else if(led_2 == 0){
    digitalWrite(D2, 0);
    Serial.println("D2 is Off..!");
  }
 
  if(led_3 == 1){
    digitalWrite(D3, 1);
    Serial.println("D3 is On..!");
  }
  else if(led_3 == 0){
    digitalWrite(D3, 0);
    Serial.println("D3 is Off..!");
  }
    
  Serial.println(led_1);
  Serial.println(led_2);
  Serial.println(led_3);
  delay(5000);
 
}
Previous Post Next Post

Contact Form