PIR Motion Alarm system + ESP8266 WiFi module was implemented using FRDM-k64f. PIR sensor will detect motion around its environment and then will send update information to Thingspeak.com website through wifi connection. The main components used in this project: Parallax PIR sensor, LED, ESP8266 WiFi module, 3.0 to 28VDC Voltage range Piezo buzzer.

Dependencies:   ESP8266 mbed

main.cpp

Committer:
mokhanaf
Date:
2015-05-14
Revision:
0:bab4cf7cdda8

File content as of revision 0:bab4cf7cdda8:


// PIR Motion Sensor Alarm + ESP8622 WiFi Module + FRDM-k64f
//PIR sensor can detect change on its surrounding by measuring the change of infra red
// Then will send high=1 to the FRDM-k64f to flash the LED, Run the Buzzer,
//Send information through UART serial Port to Tera Term and display how many motion been
//detected,also will send information to Thingspeak IoT website using WiFi connection 



#include "mbed.h"
#include "ESP8266.h"
#define APIKEY 7X2TD78DKY4459M0

Serial pc(USBTX,USBRX);
DigitalOut Buzzer(D10); // output
PwmOut LED(D13); // flashing the led
DigitalIn inputPin(D2); // pir senor input
DigitalOut redled(LED1);
ESP8266 wifi(PTC17, PTC16, 115200); // baud rate for wifi
char snd[255],rcv[1000];

#define IP "184.106.153.149" // thingspeak.com IP Address

int val = 0; // value to holed the high/low info from pir from pin D2
int cnt = 0; // counter for motion
//String thingtweetAPIKey = "TTEVLP931ODJ5GMT";

/************ WiFi INTIALIZATION *********/

void wifi_initialize(void);
void wifi_send(void);

int main () {
    
pc.baud(115200);   
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("Network", "Password");     // 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");
 //wifi_initialize();
    while (1) {
       
   val = inputPin.read();
   
   if (val==0) {   
    cnt++;
   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");
    wifi_send();
    redled=1; // when the motion detected turn of the on board red led
    LED.period(2.0f); // 2 seconds period
    wait(2.0f);
    LED.pulsewidth(.02);    // 2 mseconds pulse (on)
    redled=1;
    Buzzer = 1;
   wait(1.5f);
          }
    else {
         
    pc.printf(" The Sensor is OFF \r\n");
        LED = 0;
         Buzzer =0;
        redled=0; // turn the on board red led on
          wait(1.5f);
         }
        }
    }

void wifi_send(void){
   
  //WIFI updates the Status to Thingspeak servers//
  strcpy(snd,"AT+CIPMUX=1");//Setting WiFi into MultiChannel mode
  wifi.SendCMD(snd);
  pc.printf(snd);
  wait(2.0);
  wifi.RcvReply(rcv, 1000);
  pc.printf("%s", rcv);
  wait(2);
  sprintf(snd,"AT+CIPSTART=4,\"TCP\",\"%s\",80",IP); //Initiate connection with THINGSPEAK server 
  pc.printf(snd);
  wait(3.0);
  wifi.RcvReply(rcv, 1000);
  pc.printf("%s", rcv);
  wait(2);
  strcpy(snd,"AT+CIPSEND=4,47");    //Send Number of open connections,Characters to send 
  wifi.SendCMD(snd);
  pc.printf(snd);
  wait(2.0);
  wifi.RcvReply(rcv, 1000);
  pc.printf("%s", rcv);
  wait(2);    
  sprintf(snd,"GET /update?key=7X2TD78DKY4459M0&field1=%2.2f\r\n",1.0); //Post values to thingspeak
  pc.printf("%s",snd);
  wifi.SendCMD(snd);
  wait(3.0f);
  wifi.RcvReply(rcv, 1000);
  pc.printf("%s", rcv);
  wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server
  wifi.RcvReply(rcv, 1000);
  pc.printf("%s", rcv);
}