Introduction:

In this tutorial, I am going to explain about sending sensor values to the thing speak server.
ThingSpeak is an open-source server, so users just need to sign up with your email id. After sign up creates a new channel to get an API key, this key is unique for every channel. By using the write API key we can send the values to our own channel fields. Each field represents one column in the database.

To register in ThingSpeak click here.
To login click here 
Example sensor values posted in the thing speak server click here to see.

ESP 01

Requirements:

Arduino UNO – 1
DHT11 – 1
MQ2 – 1
Jumper wires
ESP01 – 1

Note:  Go through my previous blog to know How to interface dht11 and MQ2 with Arduino Uno click here.

Code:

#include <SoftwareSerial.h>

#define gassensor 8 //MQ
#define gate 9  //DHT11

SoftwareSerial esp(3,2); 

String apiKey= "Your api key"; //Channel apikey

String ssid = "iot";          // WiFi Name 
String password = "iot12345"; // WiFi Password
int temp,hum,z;
 
unsigned long duration=0;
unsigned char i[5];
unsigned int j[40];
unsigned char value=0;
unsigned answer=0;

void setup() 
{
  Serial.begin(9600);
  esp.begin(9600);
  pinMode(gassensor, INPUT);
  pinMode(gate,OUTPUT);
  Serial.println("Checking for wifi connection");
  connectwifi();
  Serial.println("Setup completed");
}

void loop() 
{
 int gas;
 temperature();
 gas  = digitalRead(gassensor); 
 
 Serial.print("Temperature: ");
 Serial.println(temp);
 Serial.print("Humidity: ");
 Serial.println(hum);
 Serial.print("Gas: ");
  if(!gas)
  {
    Serial.println("Alert: Gas Leakage");
    gas=1;
  }
  else
  {
    Serial.println("Gas: No leakage");
    gas=0;
  }

 post(temp,hum,gas);
 delay(15000);
}

void temperature()
{
  digitalWrite(gate,LOW);
  delay(20);
  digitalWrite(gate,HIGH);
  pinMode(gate,INPUT_PULLUP);
  
  duration=pulseIn(gate, LOW);
  if(duration <= 84 && duration >= 72)
  {
    while(1)
    {
      duration=pulseIn(gate, HIGH);
      if(duration <= 26 && duration >= 20)
      value=0;
      else if(duration <= 74 && duration >= 65)
      value=1;
      else if(z==40)
      break;
      i[z/8]|=value<<(7- (z%8));
      j[z]=value;
      z++;
    }
  }
answer=i[0]+i[1]+i[2]+i[3];

if(answer==i[4] && answer!=0)
{
  temp=i[2];
  hum=i[0];
}
z=0;
i[0]=i[1]=i[2]=i[3]=i[4]=0;
  pinMode(gate,OUTPUT);
}

void post(int value1, int value2, int value3, int value4)
{
  String cmd = "AT+CIPSTART=\"TCP\",\"";                 
  cmd += "184.106.153.149";                              
  cmd += "\",80";
  esp.println(cmd);
  response(); 
  showResponse(300);
  String getStr = "GET /update?api_key=";              
  getStr += apiKey;
  getStr +="&field1=";
  getStr += String(value1);
  getStr +="&field2=";
  getStr += String(value2);
  getStr +="&field3=";
  getStr += String(value3);
  getStr +="&field4=";
  getStr += String(value4);
  getStr += "\r\n";
  
  cmd = "AT+CIPSEND=";         
  cmd += String(getStr.length());
  esp.println(cmd);
  delay(100);
  if(esp.find(">"))
  {
    Serial.println(getStr);
    esp.print(getStr);
    showResponse(5000);
  }
  else           
    Serial.println("Connection Lost");
}

void response()
{
   while(esp.available()!=0)
   Serial.write(char (esp.read()));
}

void showResponse(int waitTime)
{
    long t=millis();
    char c;
    while (t+waitTime>millis()){
      if (esp.available()>0){
          c=esp.read();
          Serial.print(c);
      }
    }               
}

int connectwifi()
{
  String cmd ="AT+CWJAP=\"" + ssid + "\",\"" + password + "\"";
  esp.println(cmd);
  delay(5000);
  if(esp.find("OK"))
  {
    Serial.println("Wifi Connected");
    return 1;
  }
  else 
  {
    Serial.println("Connecting wifi");
    connectwifi();
  }
}


Output Screenshots:

Leave a Reply

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