The code uploads analog value to a local phant server running on local network. Make sure to change the IP address to the server IP address. Use the link to dload phant. https://nodejs.org/en/ Run "npm -g install phant" in CMD admin mode. Type "phant" in CMD admin panel to start server.

Dependencies:   ESP8266 mbed

main.cpp

Committer:
janhavi
Date:
2016-05-23
Revision:
0:b8b5056535fb

File content as of revision 0:b8b5056535fb:

#include "mbed.h"
#include "ESP8266.h"
 
Serial pc(USBTX,USBRX);
AnalogIn inputPin(A2); // pir senor input
ESP8266 wifi(PTE0, PTE1, 115200); // baud rate for wifi
char snd[255],rcv[1000];
char http_cmd[300];
 
//#define IP "data.sparkfun.com" 
//#define IP "184.106.153.149"
#define IP "192.168.0.15"
 
float val = 0; // value to holed the high/low info from pir from pin D2
int cnt = 0; // counter for motion

char* Public_Key = "jDYlpAdrebHwAaPBmMB8UDaVxmay";
char* Private_Key = "wydQD8njzwh7m2eZ5YZLUMbrPnb2";

/************ WiFi INTIALIZATION *********/
 
void wifi_initialize(void);
void wifi_send(void);
 
int main () {
    

 wifi_initialize();
    while (1) {
       
   val = inputPin.read();
   pc.printf(" The Sensor is ON And I Detected = %i Till NOW\r\n",cnt);
   pc.printf("PLEASE STAY AWAY\r\n");
    pc.printf("Sending WiFi information\n\r");
    wifi_send();
   wait(4);
   cnt++;
          }
    }
    
void wifi_initialize()
{
pc.baud(9600);   
pc.printf("SET mode to AP\r\n");
wifi.SetMode(1);    // set ESP mode to 1
wifi.RcvReply(rcv, 1000);    //receive a response from ESP
pc.printf("%s",rcv);    //Print the response onscreen
pc.printf("Conneting to AP\r\n");
wifi.Join("Eduvance_WiFi", "eduvance");     // Your wifi username & Password 
wifi.RcvReply(rcv, 1000);    //receive a response from ESP
pc.printf("%s", rcv);    //Print the response onscreen
wait(8);     //waits for response from ESP
pc.printf("Getting IP\r\n");    //get IP addresss from the connected AP
wifi.GetIP(rcv);    //receive an IP address from the AP
pc.printf("%s", rcv);
wait(5); // Delay 5 sec to give the pir time to get snapshut of the surrounding
pc.printf("Initializing WiFi\r\n"); 
}

void wifi_send(void){

    strcpy(snd,"AT+CIPMODE=0");//Setting WiFi into MultiChannel mode
    wifi.SendCMD(snd);
    //pc.printf(snd);
    wifi.RcvReply(rcv, 3000);
    pc.printf("%s", rcv);
 
  //WIFI updates the Status to Thingspeak servers//
  strcpy(snd,"AT+CIPMUX=1");//Setting WiFi into MultiChannel mode
  wifi.SendCMD(snd);
  //pc.printf(snd);
  wifi.RcvReply(rcv, 3000);
  pc.printf("%s", rcv);
  
  
  sprintf(snd,"AT+CIPSTART=4,\"TCP\",\"%s\",8080",IP); //Initiate connection with THINGSPEAK server 
  wifi.SendCMD(snd);
  //pc.printf(snd);
  wifi.RcvReply(rcv, 3000);
  pc.printf("%s", rcv);
 
  sprintf(http_cmd,"GET /input/%s?private_key=%s&f=%0.2f&t=%0.2f HTTP/1.0\n\n",Public_Key,Private_Key,val,val); //Post values to thingspeak
  pc.printf("%d",strlen(http_cmd));
  strcpy(snd,"AT+CIPSEND=4,105");    //Send Number of open connections,Characters to send 
  wifi.SendCMD(snd);
  //pc.printf(snd);
  wifi.RcvReply(rcv, 3000);
  pc.printf("%s", rcv);
  
    
  sprintf(snd,"GET /input/%s?private_key=%s&f=%0.2f&t=%0.2f HTTP/1.0\n\n",Public_Key,Private_Key,val,val); //Post values to thingspeak
  strcpy(snd, http_cmd);
  pc.printf("%s",http_cmd);
  wifi.SendCMD(http_cmd);
  wifi.RcvReply(rcv, 3000);
  pc.printf("%s", rcv);
  
  //wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server
  //wifi.RcvReply(rcv, 3000);
  //pc.printf("%s", rcv);
}