Mona Khanafriboor / Mbed 2 deprecated PIR_Sensor_wifi

Dependencies:   ESP8266 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 // PIR Motion Sensor Alarm + ESP8622 WiFi Module + FRDM-k64f
00003 //PIR sensor can detect change on its surrounding by measuring the change of infra red
00004 // Then will send high=1 to the FRDM-k64f to flash the LED, Run the Buzzer,
00005 //Send information through UART serial Port to Tera Term and display how many motion been
00006 //detected,also will send information to Thingspeak IoT website using WiFi connection 
00007 
00008 
00009 
00010 #include "mbed.h"
00011 #include "ESP8266.h"
00012 #define APIKEY 7X2TD78DKY4459M0
00013 
00014 Serial pc(USBTX,USBRX);
00015 DigitalOut Buzzer(D10); // output
00016 PwmOut LED(D13); // flashing the led
00017 DigitalIn inputPin(D2); // pir senor input
00018 DigitalOut redled(LED1);
00019 ESP8266 wifi(PTC17, PTC16, 115200); // baud rate for wifi
00020 char snd[255],rcv[1000];
00021 
00022 #define IP "184.106.153.149" // thingspeak.com IP Address
00023 
00024 int val = 0; // value to holed the high/low info from pir from pin D2
00025 int cnt = 0; // counter for motion
00026 //String thingtweetAPIKey = "TTEVLP931ODJ5GMT";
00027 
00028 /************ WiFi INTIALIZATION *********/
00029 
00030 void wifi_initialize(void);
00031 void wifi_send(void);
00032 
00033 int main () {
00034     
00035 pc.baud(115200);   
00036 pc.printf("SET mode to AP\r\n");
00037 wifi.SetMode(1);    // set ESP mode to 1
00038 wifi.RcvReply(rcv, 1000);    //receive a response from ESP
00039 pc.printf("%s",rcv);    //Print the response onscreen
00040 pc.printf("Conneting to AP\r\n");
00041 wifi.Join("Network", "Password");     // Your wifi username & Password 
00042 wifi.RcvReply(rcv, 1000);    //receive a response from ESP
00043 pc.printf("%s", rcv);    //Print the response onscreen
00044 wait(8);     //waits for response from ESP
00045 pc.printf("Getting IP\r\n");    //get IP addresss from the connected AP
00046 wifi.GetIP(rcv);    //receive an IP address from the AP
00047 pc.printf("%s", rcv);
00048 
00049     
00050  wait(5); // Delay 5 sec to give the pir time to get snapshut of the surrounding
00051 
00052  pc.printf("Initializing WiFi\r\n");
00053  //wifi_initialize();
00054     while (1) {
00055        
00056    val = inputPin.read();
00057    
00058    if (val==0) {   
00059     cnt++;
00060    pc.printf(" The Sensor is ON And I Detected = %i Till NOW\r\n",cnt);
00061    pc.printf("PLEASE STAY AWAY\r\n");
00062     pc.printf("Sending WiFi information");
00063     wifi_send();
00064     redled=1; // when the motion detected turn of the on board red led
00065     LED.period(2.0f); // 2 seconds period
00066     wait(2.0f);
00067     LED.pulsewidth(.02);    // 2 mseconds pulse (on)
00068     redled=1;
00069     Buzzer = 1;
00070    wait(1.5f);
00071           }
00072     else {
00073          
00074     pc.printf(" The Sensor is OFF \r\n");
00075         LED = 0;
00076          Buzzer =0;
00077         redled=0; // turn the on board red led on
00078           wait(1.5f);
00079          }
00080         }
00081     }
00082 
00083 void wifi_send(void){
00084    
00085   //WIFI updates the Status to Thingspeak servers//
00086   strcpy(snd,"AT+CIPMUX=1");//Setting WiFi into MultiChannel mode
00087   wifi.SendCMD(snd);
00088   pc.printf(snd);
00089   wait(2.0);
00090   wifi.RcvReply(rcv, 1000);
00091   pc.printf("%s", rcv);
00092   wait(2);
00093   sprintf(snd,"AT+CIPSTART=4,\"TCP\",\"%s\",80",IP); //Initiate connection with THINGSPEAK server 
00094   pc.printf(snd);
00095   wait(3.0);
00096   wifi.RcvReply(rcv, 1000);
00097   pc.printf("%s", rcv);
00098   wait(2);
00099   strcpy(snd,"AT+CIPSEND=4,47");    //Send Number of open connections,Characters to send 
00100   wifi.SendCMD(snd);
00101   pc.printf(snd);
00102   wait(2.0);
00103   wifi.RcvReply(rcv, 1000);
00104   pc.printf("%s", rcv);
00105   wait(2);    
00106   sprintf(snd,"GET /update?key=7X2TD78DKY4459M0&field1=%2.2f\r\n",1.0); //Post values to thingspeak
00107   pc.printf("%s",snd);
00108   wifi.SendCMD(snd);
00109   wait(3.0f);
00110   wifi.RcvReply(rcv, 1000);
00111   pc.printf("%s", rcv);
00112   wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server
00113   wifi.RcvReply(rcv, 1000);
00114   pc.printf("%s", rcv);
00115 }
00116