project

Dependencies:   mbed HTTPClient LM75B mbed-rtos EthernetInterface MMA7660

Committer:
omar1646
Date:
Sun Mar 28 18:20:05 2021 +0000
Revision:
1:21d76b260bc6
Parent:
0:34d3f68b920e
Child:
2:f7645c6a85f3
Home Security System PIR Sensor + Grove Buzzer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
omar1646 1:21d76b260bc6 1
omar1646 1:21d76b260bc6 2 // PIR Motion Sensor Alarm + ESP8622 WiFi Module + FRDM-k64f
omar1646 1:21d76b260bc6 3 //PIR sensor can detect change on its surrounding by measuring the change of infra red
omar1646 1:21d76b260bc6 4 // Then will send high=1 to the FRDM-k64f to flash the LED, Run the Buzzer,
omar1646 1:21d76b260bc6 5 //Send information through UART serial Port to Tera Term and display how many motion been
omar1646 1:21d76b260bc6 6 //detected,also will send information to Thingspeak IoT website using WiFi connection
omar1646 1:21d76b260bc6 7
omar1646 1:21d76b260bc6 8
omar1646 1:21d76b260bc6 9
eduvanceIoT 0:34d3f68b920e 10 #include "mbed.h"
eduvanceIoT 0:34d3f68b920e 11 #include "ESP8266.h"
eduvanceIoT 0:34d3f68b920e 12
omar1646 1:21d76b260bc6 13 #define APIKEY YTDILMQL53ASDCJ4
eduvanceIoT 0:34d3f68b920e 14
omar1646 1:21d76b260bc6 15 Serial pc(USBTX,USBRX);
omar1646 1:21d76b260bc6 16 DigitalOut Buzzer(D3); // output
omar1646 1:21d76b260bc6 17 PwmOut LED(D13); // flashing the led
omar1646 1:21d76b260bc6 18 DigitalIn inputPin(D2); // pir senor input
omar1646 1:21d76b260bc6 19 DigitalOut redled(LED1);
omar1646 1:21d76b260bc6 20 ESP8266 wifi(PTE0, PTE1, 115200); // baud rate for wifi
omar1646 1:21d76b260bc6 21 char snd[255],rcv[1000];
eduvanceIoT 0:34d3f68b920e 22
omar1646 1:21d76b260bc6 23 #define IP "184.106.153.149" // thingspeak.com IP Address
eduvanceIoT 0:34d3f68b920e 24
omar1646 1:21d76b260bc6 25 int val = 0; // value to holed the high/low info from pir from pin D2
omar1646 1:21d76b260bc6 26 int cnt = 0; // counter for motion
omar1646 1:21d76b260bc6 27 //String thingtweetAPIKey = "TTEVLP931ODJ5GMT";
omar1646 1:21d76b260bc6 28
omar1646 1:21d76b260bc6 29 /************ WiFi INTIALIZATION *********/
omar1646 1:21d76b260bc6 30
omar1646 1:21d76b260bc6 31 void wifi_initialize(void);
omar1646 1:21d76b260bc6 32 void wifi_send(void);
eduvanceIoT 0:34d3f68b920e 33
eduvanceIoT 0:34d3f68b920e 34 int main () {
eduvanceIoT 0:34d3f68b920e 35
omar1646 1:21d76b260bc6 36 pc.baud(9600);
omar1646 1:21d76b260bc6 37 pc.printf("SET mode to AP\r\n");
omar1646 1:21d76b260bc6 38 wifi.SetMode(1); // set ESP mode to 1
omar1646 1:21d76b260bc6 39 wifi.RcvReply(rcv, 1000); //receive a response from ESP
omar1646 1:21d76b260bc6 40 pc.printf("%s",rcv); //Print the response onscreen
omar1646 1:21d76b260bc6 41 pc.printf("Conneting to AP\r\n");
omar1646 1:21d76b260bc6 42 wifi.Join("BHTG1672G3FC2-2G", "5f17baeb"); // Your wifi username & Password
omar1646 1:21d76b260bc6 43 wifi.RcvReply(rcv, 1000); //receive a response from ESP
omar1646 1:21d76b260bc6 44 pc.printf("%s", rcv); //Print the response onscreen
omar1646 1:21d76b260bc6 45 wait(8); //waits for response from ESP
omar1646 1:21d76b260bc6 46 pc.printf("Getting IP\r\n"); //get IP addresss from the connected AP
omar1646 1:21d76b260bc6 47 wifi.GetIP(rcv); //receive an IP address from the AP
omar1646 1:21d76b260bc6 48 pc.printf("%s", rcv);
omar1646 1:21d76b260bc6 49
eduvanceIoT 0:34d3f68b920e 50
omar1646 1:21d76b260bc6 51 wait(5); // Delay 5 sec to give the pir time to get snapshut of the surrounding
omar1646 1:21d76b260bc6 52
omar1646 1:21d76b260bc6 53 pc.printf("Initializing WiFi\r\n");
omar1646 1:21d76b260bc6 54 //wifi_initialize();
eduvanceIoT 0:34d3f68b920e 55 while (1) {
omar1646 1:21d76b260bc6 56
omar1646 1:21d76b260bc6 57 val = inputPin.read();
omar1646 1:21d76b260bc6 58
omar1646 1:21d76b260bc6 59 if (val==0) {
omar1646 1:21d76b260bc6 60 cnt++;
omar1646 1:21d76b260bc6 61 pc.printf(" The Sensor is ON And I Detected = %i Till NOW\r\n",cnt);
omar1646 1:21d76b260bc6 62 Buzzer = 1;
omar1646 1:21d76b260bc6 63 pc.printf("PLEASE STAY AWAY\r\n");
omar1646 1:21d76b260bc6 64 pc.printf("Sending WiFi information\n\r");
omar1646 1:21d76b260bc6 65 wifi_send();
omar1646 1:21d76b260bc6 66 redled=1; // when the motion detected turn of the on board red led
omar1646 1:21d76b260bc6 67 LED.period(2.0f); // 2 seconds period
omar1646 1:21d76b260bc6 68 wait(2.0f);
omar1646 1:21d76b260bc6 69 LED.pulsewidth(.02); // 2 mseconds pulse (on)
omar1646 1:21d76b260bc6 70 redled=1;
omar1646 1:21d76b260bc6 71 // Buzzer = 1;
omar1646 1:21d76b260bc6 72 wait(1.5f);
omar1646 1:21d76b260bc6 73 }
omar1646 1:21d76b260bc6 74 else {
omar1646 1:21d76b260bc6 75
omar1646 1:21d76b260bc6 76 pc.printf(" The Sensor is OFF \r\n");
omar1646 1:21d76b260bc6 77 LED = 0;
omar1646 1:21d76b260bc6 78 Buzzer =0;
omar1646 1:21d76b260bc6 79 redled=0; // turn the on board red led on
omar1646 1:21d76b260bc6 80 wait(1.5f);
omar1646 1:21d76b260bc6 81 }
omar1646 1:21d76b260bc6 82 }
eduvanceIoT 0:34d3f68b920e 83 }
omar1646 1:21d76b260bc6 84
omar1646 1:21d76b260bc6 85 void wifi_send(void){
omar1646 1:21d76b260bc6 86
omar1646 1:21d76b260bc6 87 strcpy(snd,"AT+CIPMODE=0");//Setting WiFi into MultiChannel mode
omar1646 1:21d76b260bc6 88 wifi.SendCMD(snd);
omar1646 1:21d76b260bc6 89 pc.printf(snd);
omar1646 1:21d76b260bc6 90 wifi.RcvReply(rcv, 3000);
omar1646 1:21d76b260bc6 91 pc.printf("%s", rcv);
omar1646 1:21d76b260bc6 92
omar1646 1:21d76b260bc6 93 //WIFI updates the Status to Thingspeak servers//
omar1646 1:21d76b260bc6 94 strcpy(snd,"AT+CIPMUX=1");//Setting WiFi into MultiChannel mode
omar1646 1:21d76b260bc6 95 wifi.SendCMD(snd);
omar1646 1:21d76b260bc6 96 pc.printf(snd);
omar1646 1:21d76b260bc6 97 wifi.RcvReply(rcv, 3000);
omar1646 1:21d76b260bc6 98 pc.printf("%s", rcv);
omar1646 1:21d76b260bc6 99
omar1646 1:21d76b260bc6 100
omar1646 1:21d76b260bc6 101 sprintf(snd,"AT+CIPSTART=4,\"TCP\",\"%s\",80",IP); //Initiate connection with THINGSPEAK server
omar1646 1:21d76b260bc6 102 wifi.SendCMD(snd);
omar1646 1:21d76b260bc6 103 pc.printf(snd);
omar1646 1:21d76b260bc6 104 wifi.RcvReply(rcv, 3000);
omar1646 1:21d76b260bc6 105 pc.printf("%s", rcv);
omar1646 1:21d76b260bc6 106
omar1646 1:21d76b260bc6 107 strcpy(snd,"AT+CIPSEND=4,47"); //Send Number of open connections,Characters to send
omar1646 1:21d76b260bc6 108 wifi.SendCMD(snd);
omar1646 1:21d76b260bc6 109 pc.printf(snd);
omar1646 1:21d76b260bc6 110 wifi.RcvReply(rcv, 3000);
omar1646 1:21d76b260bc6 111 pc.printf("%s", rcv);
omar1646 1:21d76b260bc6 112
omar1646 1:21d76b260bc6 113
omar1646 1:21d76b260bc6 114 sprintf(snd,"GET /update?key=WZXOHNJSLN9G1AGP&field1=%2.2f\r\n",1.0); //Post values to thingspeak
omar1646 1:21d76b260bc6 115 pc.printf("%s",snd);
omar1646 1:21d76b260bc6 116 wifi.SendCMD(snd);
omar1646 1:21d76b260bc6 117
omar1646 1:21d76b260bc6 118 wifi.RcvReply(rcv, 3000);
omar1646 1:21d76b260bc6 119 pc.printf("%s", rcv);
omar1646 1:21d76b260bc6 120
omar1646 1:21d76b260bc6 121 wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server
omar1646 1:21d76b260bc6 122 wifi.RcvReply(rcv, 3000);
omar1646 1:21d76b260bc6 123 pc.printf("%s", rcv);
omar1646 1:21d76b260bc6 124 }