nagaraju malla reddy palle / Mbed OS MotionDetection_PIR

Dependencies:   ESP8266

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "ESP8266.h"
00003 #include <string>
00004 #include <stdio.h>
00005 #define APIKEY 5ZVYJP19NQZJHIJM    //Put "Write key" of your channel in thingspeak.com 
00006 #define IP "184.106.153.149"       // IP Address of "api.thingspeak.com\"
00007 #define WIFI_SSID "iphone"
00008 #define WIFI_PASS "priyanka2024"
00009 
00010 
00011 char snd[255],rcv[1000],snd_Data[255];
00012 int cnt;
00013 
00014 
00015 ESP8266 esp(PTC17, PTC16, 115200); // baud rate for wifi
00016 Serial pc(USBTX, USBRX);
00017 
00018 InterruptIn motion(D2);
00019 int motion_detected = 0;
00020 void irq_handler(void)
00021 {
00022 motion_detected = 1;
00023 }
00024 void esp_initialize(void);
00025 void esp_send(void);
00026 
00027 int main(void)
00028 {   
00029 pc.baud(115200);
00030     esp_initialize();
00031 
00032  cnt = 0;     
00033 motion.rise(&irq_handler);          
00034 while(1)
00035  {        
00036  if(motion_detected) 
00037 {            
00038  cnt++;           
00039   motion_detected = 0;            
00040  printf("Hello! I've detected %d times since reset\n", cnt); 
00041 printf("Now uploading status to cloud\n\r");
00042     
00043    
00044     esp_send();
00045    }
00046  }
00047 }
00048 
00049 void esp_initialize(void)
00050 {
00051     //AT+CWJAP="priyanka2024","iPhone" ;
00052     
00053     pc.printf("Initializing ESP\r\n");
00054     pc.printf("Reset ESP\r\n");
00055     esp.Reset();                   //RESET ESP
00056     esp.RcvReply(rcv, 400);        //receive a response from ESP
00057     wait(2);
00058 
00059     strcpy(snd,"AT");
00060     esp.SendCMD(snd);
00061     pc.printf(snd);
00062     esp.RcvReply(rcv, 400);
00063     pc.printf(rcv);
00064     wait(2);
00065 
00066     strcpy(snd,"AT+CWMODE=1");
00067     esp.SendCMD(snd);
00068     pc.printf(snd);
00069     wait(2);
00070 
00071     strcpy(snd,"AT+CWJAP=\"");
00072     strcat(snd,WIFI_SSID);
00073     strcat(snd,"\",\"");
00074     strcat(snd,WIFI_PASS);
00075     strcat(snd,"\"");
00076 
00077     esp.SendCMD(snd);
00078     pc.printf(snd);
00079     wait(5);
00080     esp.RcvReply(rcv, 400);
00081     pc.printf("\n %s \n", rcv);
00082 
00083     strcpy(snd,"AT+CIPMUX=1");
00084     esp.SendCMD(snd);
00085     pc.printf(snd);
00086     esp.RcvReply(rcv, 400);
00087     pc.printf("\n %s \n", rcv);
00088 }
00089 
00090 void esp_send(void)
00091 {
00092     //ESP updates the Status of Thingspeak channel//
00093 
00094     strcpy(snd,"AT+CIPSTART=");
00095     strcat(snd,"\"TCP\",\"");
00096     strcat(snd,IP);
00097     strcat(snd,"\",80");
00098 
00099     esp.SendCMD(snd);
00100     pc.printf("Send\r\n%s",snd);
00101     esp.RcvReply(rcv, 1000);
00102     pc.printf("Receive\r\n%s",rcv);
00103     wait(2);
00104 
00105     
00106     pc.printf("Sending this information to thingspeak.com \r\n");
00107     sprintf(snd,"GET https://api.thingspeak.com/update?api_key=5ZVYJP19NQZJHIJM&field1=%d\r\n", cnt);
00108     //wait(5);
00109     //https://api.thingspeak.com/update?api_key=5ZVYJP19NQZJHIJM&field1=%d\r\n", cnt
00110     
00111     int i=0;
00112     for(i=0; snd[i]!='\0'; i++);
00113     i++;
00114     char cmd[255];
00115 
00116     sprintf(cmd,"AT+CIPSEND=%d",i);           //Send Number of open connection and Characters to send
00117     esp.SendCMD(cmd);
00118     pc.printf("Send\r\n%s",cmd);
00119     while(i<=20 || rcv == ">")
00120     {
00121         esp.RcvReply(rcv, 1000);
00122         wait(100);
00123         i++;
00124     }
00125     pc.printf("Receive\r\n%s",rcv);
00126 
00127     esp.SendCMD(snd);       //Post value to thingspeak channel
00128     pc.printf("Send\r\n%s",snd);
00129 
00130     while(i<=20 || rcv == "OK") 
00131     {
00132         esp.RcvReply(rcv, 1000);
00133         wait(100);
00134         i++;
00135     }
00136     pc.printf("Receive\r\n%s",rcv);
00137     
00138 }
00139 
00140 
00141 
00142 
00143