Tuesday, 25 December 2018

Arduino : Sending Data from ESP8266 to thingSpeak


Setting of ThingSpeak
1.       Create account in Think speak. And complete the validation

2.       Create a new Channel in thingSpeak.


Then click on save channel. By default it will show the empty chart as like below.
It is create the new API for you which will accept the get Verb and from which you can send the data.


3.       Get the API key from API Keys tab. ThingSpeek dashboard from here you can see the two different keys that you can use it based on needs.



Connection/ circuit of Arduino with ESP8266
Creation a circuit for Arduino with ESP8266, you need the following items for it.
1.       Arduino Uno
2.       ESP8266 (WIFI Module)
3.       Serial port connect to Arduino with Computer
4.       Jumber
5.       Breadboard

Circuit will be look like below as given in figure for Arduino with ESP8266 for sharing data to API.


ESP8266 pin details are as below.

Connection for it in the borad will be like below.
UNO
ESP-01
RX
TX
TX
RX
3.3v
VCC
GND
GND
RST
3.3v / float
CH_PD
3.3v


Code for Arduino IDE,  For connecting the ESP8266

Before starting the coding let arduino to make compatible.

esp8266 arduino compatible

In order to setup your Arduino IDE to work with your esp8266 arduino compatible module you need to make the following steps:

1.         Connect your ESP8266-01 Module to PC

2.         Open your Arduino  IDE

3.         Go to File -> Preferences

4.         Add this link to Additional Board Manager

 (http://arduino.esp8266.com/versions/2.5.0-beta2/package_esp8266com_index.json)

5.         Go to Tools -> Board Manager

6.         Find ESP8266 board set and activate it

7.         Select Generic ESP8266 board from Tools->Boards

8.         Choose your programmer COM port


9.         You are ready to go!


#include <SoftwareSerial.h>       //Software Serial library


#define RX 6
#define TX 7
#define DST_IP "api.thingspeak.com"
SoftwareSerial espSerial(RX,TX);   //Pin 6 and 7 act as RX and TX. Connect them to TX and RX of ESP8266  


#define DEBUG false
String mySSID = "******";       // WiFi SSID
String myPWD = "*******"; // WiFi Password
String myAPI = "5Y4U0YAOVIBK1***";   // API Key
String myHOST = "api.thingspeak.com";
String myPORT = "80";
String myFIELD = "field1";
int sendVal;

/***********************************************/
/*  Setting up the Serial ports              **/
/*********************************************/
void setup()
{

 espSerial.begin(115200);
 Serial.begin(115200);  // Begin serial monitor to receive 115200 bits per second (BAUD RATE)
  espSerial.begin(115200);
  espSerial.println("AT+UART_DEF=9600,8,1,0,0"); // set WiFi Send/Receive from 115200 bits per second (BAUD RATE) to 9600 bits per second
  delay(2000);
  espSerial.begin(9600); // Begin serial monitor to receive 9600 bits per second (BAUD RATE)
  espSerial.println("ATE0");
  delay(200);
  espSerial.println("AT+CWQAP"); // Disconnect from previous network connections
  delay(1000);
 
}


/***********************************************/
/*  Check for ESP board is connected or not  **/
/*********************************************/
bool WIFI_Check()
{
  espSerial.println("AT"); // send a Attention command
  delay(500);
  if (espSerial.available())
  {
    if (espSerial.find("OK")) // check with expected output
    {
      Serial.println("WIFI PLUGGED ON TO THE BOARD..!");
      Serial.println();
      espSerial.println("AT+CWMODE=1"); //set mode to client mode
      delay(500);
    
      return true;
    }
  }
  else
  {
    Serial.println("WIFI NOT PLUGGED..!");
    Serial.println();
    Serial.println("PLUG IN YOUR WIFI CHIP");
    return false;
  }
}


/***********************************************/
/*  Check for WIFI is connected or not       **/
/*********************************************/
bool connectWiFi()
{
  espSerial.println("AT+CWJAP?"); //check if WIFI connected to any WiFi network
  delay(5000);
  if (espSerial.available())
  {
    if (espSerial.find("No AP")) //we receive reponse "No AP" when not connected to any network
    {
      Serial.println("NOT CONNECTED TO WIFI NETWORK");
      Serial.println();
      Serial.println();
      Serial.println("Trying to Connect to WiFi Network");
      String cmd = "AT+CWJAP=\""; // connected to specified network passing mentioned WiFi username and password
      cmd += mySSID;
      cmd += "\",\"";
      cmd += myPWD;
      cmd += "\"";
      espSerial.println(cmd);
      Serial.println(cmd);
      delay(5000);
      if (espSerial.available())
      {
        String RES_input = "";
        while (espSerial.available()) // read the data into a variable as long as the
        {
          RES_input += (char)espSerial.read();
        }
        Serial.println(RES_input);
        if (espSerial.find("WIFI CONNECTED"))
        {
          Serial.println("CONNECTED TO WIFI NETWORK..!");
          return true;
        }
      }
    }
    else
    {
      Serial.println("CONNECTED TO WIFI NETWORK..!");
      Serial.println();
      Serial.println();
    //  post();
      //i = 0;
    }
    return false;
  }
}


/***********************************************/
/*  Looping it to send the data to the API  **/
/*********************************************/
void loop()
{
    if(WIFI_Check())
    {
      if(connectWiFi())
      delay(1000);
      Serial.println("Posting data...");
      Serial.println("========================================");
      post();
      Serial.println("========================================");
      Serial.println("data posted...");
      delay(1000);
    }
}


/***********************************************/
/*  Function to post the data to API         **/
/*********************************************/
void post()
{
 
    sendVal = random(1000); // Send a random number between 1 and 1000
    String sendData = "GET /update?api_key="+ myAPI +"&"+ myFIELD +"="+String(sendVal);
    espData("AT+CIPMUX=1", 1000, DEBUG);       //Allow multiple connections
    espData("AT+CIPSTART=0,\"TCP\",\""+ myHOST +"\","+ myPORT, 1000, DEBUG);
    espData("AT+CIPSEND=0," +String(sendData.length()+4),1000,DEBUG); 
    espSerial.find(">");
    espSerial.println(sendData);
    Serial.print("Value to be sent: ");
    Serial.println(sendVal);
    
    espData("AT+CIPCLOSE=0",1000,DEBUG);
    delay(10000);
}



/***********************************************/
/*  Supporting method to check the commands  **/
/*********************************************/
String espData(String command, const int timeout, boolean debug)
{
  Serial.print("AT Command ==> ");
  Serial.print(command);
  Serial.println("     ");
 
  String response = "";
  espSerial.println(command);
  long int time = millis();
  while ( (time + timeout) > millis())
  {
    while (espSerial.available())
    {
      char c = espSerial.read();
      response += c;
    }
  }
  if (debug)
  {
    //Serial.print(response);
  }
  return response;
}
 
 

OUTPUT.
You can see the output in serial monitor that your ESP8266 board is connecting properly or not and then check that WIFI is connecting by your given SSID and password.
Output in serial monitor as:

The first data send it to thingspeek is as like below.


If you see on the screen value 80 has send it to thingspeek API
You can see the output in thingspeek in the chart as.




No comments:

Post a Comment