Sending Sensor Data to the ThingSpeak Server

Nowadays hardware based projects require to be wireless and their data accessible to the web so that they can be manipulated or further used. One of the modules used to transmit data over Wi-Fi is the Esp 8266 01 Wi Fi module. It can be used both with Arduino and Raspberri pi.

The ESP 8266-01 module :

The Esp8266 01 module comes with preloaded  full TCP/IP stack and microcontroller capability. The pinout is as follows for the common ESP-01 module:

  1. VCC, Voltage (+3.3 V; can handle up to 3.6 V)
  2. GND, Ground (0 V)
  3. RX, Receive data bit X
  4. TX, Transmit data bit X
  5. CH_PD, Chip power-down
  6. RST, Reset
  7. GPIO 0, General-purpose input/output No. 0
  8. GPIO 2, General-purpose input/output No. 2

The circuit diagram :

I have used the MQ 135 air quality sensor and GPS to take values and transmit the reading to the ThingSpeak server through the ESP 8266-01 Wi Fi module.

Once the connection is complete burn the following code to the Arduino :

#include <SoftwareSerial.h>
#define RX 2
#define TX 3
String AP = “JioFi3_0A2502”;       // AP NAME
String PASS = “asd12345”; // AP PASSWORD
String API = “NR82E4PHF14P57MU”;   // Write API KEY
String HOST = “api.thingspeak.com”;

String PORT = “80”;
String field = “field1”;
int countTrueCommand;
int countTimeCommand;
boolean found = false;
int valSensor = 1;
int b;
SoftwareSerial esp8266(RX,TX);

void setup()
{
Serial.begin(9600);
esp8266.begin(115200);
sendCommand(“AT”,5,”OK”);
sendCommand(“AT+CWMODE=1″,5,”OK”);
sendCommand(“AT+CWJAP=\””+ AP +”\”,\””+ PASS +”\””,20,”OK”);
}

void loop()
{
valSensor = getSensorData();
String getData = “GET /update?api_key=”+ API +”&”+ field +”=”+String(valSensor);
sendCommand(“AT+CIPMUX=1″,5,”OK”);
sendCommand(“AT+CIPSTART=0,\”TCP\”,\””+ HOST +”\”,”+ PORT,15,”OK”);
sendCommand(“AT+CIPSEND=0,” +String(getData.length()+4),4,”>”);
esp8266.println(getData);delay(1500);countTrueCommand++;
sendCommand(“AT+CIPCLOSE=0″,5,”OK”);
}

int getSensorData()
{
b=analogRead(8);
return b;
}

void sendCommand(String command, int maxTime, char readReplay[])
{
Serial.print(countTrueCommand);
Serial.print(“. at command => “);
Serial.print(command);
Serial.print(” “);
while(countTimeCommand < (maxTime*1))
{
esp8266.println(command);//at+cipsend
if(esp8266.find(readReplay))//ok

 {
found = true;
break;
}
countTimeCommand++;
}
if(found == true)
{
  Serial.println(“OYI”);
  countTrueCommand++;

    countTimeCommand = 0;
}
  if(found == false)
{
  Serial.println(“Fail”);
countTrueCommand = 0;
   countTimeCommand = 0;
}
found = false;
 }

Now power on the circuit and remember  that the ESP 8266 module requires 3.3 V of power. Higher voltage will kill it. And there you go your sensorvalue would be transmitted to the ThingSpeak server live every minute.

To use the ThingSpeak server you have to create a channel and replace the API write key in the code with your own key. Also AP is the SSID of the network and AP Password is the network password. Here is the snip of my output.

Leave a Reply

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