ESP8266

Connecting to WI-FI network / WI-FI Hotspot


/*
 * http://arduspot.blogspot.in/p/esp8266.html
 * ESP8266 - 02           Arduino 
 *    TX                           D11
 *    RX                           D12
 *    GND                       GND
 */
#include<SoftwareSerial.h>
SoftwareSerial esp(11,12);

char k[] =   {'O','K'};
char response[2];
void setup()
{
  Serial.begin(9600);
  esp.begin(9600);
}

void loop()
{
  do
  {
    esp.println("AT");
    if(esp.available()>0)
    {
      response[0] = esp.read();
      response[1] = esp.read();
      Serial.println(response);
    }
    delay(2000);
  }while(!(array_cmp(k,response,2,2)));

  do
  {
    esp.println("AT+RST");
    if(esp.available()>0)
    {
      response[0] = esp.read();
      response[1] = esp.read();
      Serial.println(response);
    }
    delay(2000);
  }while(!(array_cmp(k,response,2,2)));
 
  do
  {
    esp.println("AT+CWJAP=\"Vignesh\",\"vignesh12345\"");
    if(esp.available()>0)
    {
      response[0] = esp.read();
      response[1] = esp.read();
      Serial.println(response);
    }
    delay(2000);
  }while(!(array_cmp(k,response,2,2)));

  while(1);
}

boolean array_cmp(char *a,char *b, int len_a, int len_b)
{
     int n;

     // if their lengths are different, return false
     if (len_a != len_b) return false;

     // test each element to be the same. if not, return false
     for (n=0;n<len_a;n++) if (a[n]!=b[n]) return false;

     //ok, if we have not returned yet, they are equal :)
     return true;
}



Sending Sensor Data to thingspeak.com channel


/*
 * http://arduspot.blogspot.in/p/esp8266.html
 * ESP8266 - 02           Arduino 
 *    TX                            D3
 *    RX                            D4
 *    GND                        GND
 */

#include <String.h>
#include <SoftwareSerial.h>

SoftwareSerial Ser(3, 4);

String apiKey = "IPNXHU30P38Q5LAR";

int SENSOR1 = A0;
int SENSOR2 = 8;
int S1,S2;

void setup() 
  pinMode(SENSOR1,INPUT);
  Serial.begin(9600); 
  Ser.begin(9600);
  delay(100);

void loop() 
{
  S1 = analogRead(SENSOR1);
  S2 = digitalRead(SENSOR2);

  String STR1 = String(S1);
  String STR2 = String(S2);
  
  Serial.println(S1);
  Serial.println(S2);
  
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += "184.106.153.149";
  cmd += "\",80";
  Ser.println(cmd);
  if(Ser.find("Error"))
  {
    Serial.println("AT+CIPSTART error");
    return;
  }
  
  String getStr = "GET /update?api_key=";
  getStr += apiKey;
  getStr +="&field1=";
  getStr += String(STR1);
  getStr +="&field2=";
  getStr += String(STR2);
  getStr += "\r\n\r\n";
  
  cmd = "AT+CIPSEND=";
  cmd += String(getStr.length());
  Ser.println(cmd);
  
  if(Ser.find(">"))
  {
    Ser.print(getStr);
  }
  
  else
  {
    Ser.println("AT+CIPCLOSE");
    // alert uSer
    Serial.println("AT+CIPCLOSE");
  }
}

No comments:

Post a Comment