omar siedahmed
/
HomeSecuirtySystemSpring2021
Eduvance ThingSpeak using ESP8266 and FRDM KL25Z
Revision 1:21d76b260bc6, committed 2021-03-28
- Comitter:
- omar1646
- Date:
- Sun Mar 28 18:20:05 2021 +0000
- Parent:
- 0:34d3f68b920e
- Commit message:
- Home Security System PIR Sensor + Grove Buzzer
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 34d3f68b920e -r 21d76b260bc6 main.cpp --- a/main.cpp Sat Mar 18 09:00:35 2017 +0000 +++ b/main.cpp Sun Mar 28 18:20:05 2021 +0000 @@ -1,123 +1,124 @@ + +// 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" - -Serial pc(USBTX,USBRX); -//POT sensor -AnalogIn pot(PTB0); - -//wifi UART port and baud rate -ESP8266 wifi(PTE0, PTE1, 115200); - -//buffers for wifi library -char snd[255],resp[1000]; -char http_cmd[300], comm[300]; +#define APIKEY YTDILMQL53ASDCJ4 -int timeout = 8000; //timeout for wifi commands - -//SSID and password for connection -#define SSID "eduvanceAP" //"AmeyaHotspot"//"IoTServer" //"AmeyaHotspot" //"Xperia S_f19d" -#define PASS "winteriscoming" //"12345678" //"iotserver" //"12345678" //"profpappu" - -//Remote IP -#define IP "184.106.153.149" -//#define IP "https://api.thingspeak.com" -//#define IP "192.168.0.25" -//waterlevelue global variable -float waterlevel = 0; +Serial pc(USBTX,USBRX); +DigitalOut Buzzer(D3); // output +PwmOut LED(D13); // flashing the led +DigitalIn inputPin(D2); // pir senor input +DigitalOut redled(LED1); +ESP8266 wifi(PTE0, PTE1, 115200); // baud rate for wifi +char snd[255],rcv[1000]; -//Public and private keys for phant -char* Update_Key = "4K5STYBPH6MC8O54"; -//char* Private_Key = "GPoWnRM60yidrB0e1pXD"; - -//Wifi init function -void wifi_initialize(void){ - - pc.printf("******** Resetting wifi module ********\r\n"); - wifi.Reset(); - - //wait for 5 seconds for response, else display no response receiveed - if (wifi.RcvReply(resp, 5000)) - pc.printf("%s",resp); - else - pc.printf("No response"); - - pc.printf("******** Setting Station mode of wifi with AP ********\r\n"); - wifi.SetMode(1); // set transparent mode - if (wifi.RcvReply(resp, timeout)) //receive a response from ESP - pc.printf("%s",resp); //Print the response onscreen - else - pc.printf("No response while setting mode. \r\n"); - - pc.printf("******** Joining network with SSID and PASS ********\r\n"); - wifi.Join(SSID, PASS); - if (wifi.RcvReply(resp, timeout)) - pc.printf("%s",resp); - else - pc.printf("No response while connecting to network \r\n"); - - pc.printf("******** Getting IP and MAC of module ********\r\n"); - wifi.GetIP(resp); - if (wifi.RcvReply(resp, timeout)) - pc.printf("%s",resp); - else - pc.printf("No response while getting IP \r\n"); - - pc.printf("******** Setting WIFI UART passthrough ********\r\n"); - wifi.setTransparent(); - if (wifi.RcvReply(resp, timeout)) - pc.printf("%s",resp); - else - pc.printf("No response while setting wifi passthrough. \r\n"); - wait(1); - - pc.printf("******** Setting single connection mode ********\r\n"); - wifi.SetSingle(); - wifi.RcvReply(resp, timeout); - if (wifi.RcvReply(resp, timeout)) - pc.printf("%s",resp); - else - pc.printf("No response while setting single connection \r\n"); - wait(1); -} +#define IP "184.106.153.149" // thingspeak.com IP Address -void wifi_send(void){ - - pc.printf("******** Starting TCP connection on IP and port ********\r\n"); - wifi.startTCPConn(IP,80); //cipstart - wifi.RcvReply(resp, timeout); - if (wifi.RcvReply(resp, timeout)) - pc.printf("%s",resp); - else - pc.printf("No response while starting TCP connection \r\n"); - wait(1); - - //create link - sprintf(http_cmd,"/update?api_key=%s&field1=%f",Update_Key,waterlevel); - pc.printf(http_cmd); - - pc.printf("******** Sending URL to wifi ********\r\n"); - wifi.sendURL(http_cmd, comm); //cipsend and get command - if (wifi.RcvReply(resp, timeout)) - pc.printf("%s",resp); - - pc.printf("No response while sending URL \r\n"); - - //wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server - //wifi.RcvReply(resp, timeout); - //pc.printf("%s", resp); -} +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(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("BHTG1672G3FC2-2G", "5f17baeb"); // 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); + - wifi_initialize(); - + 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) { - waterlevel = 255*pot.read(); - pc.printf("Current waterlevel is = %.3f \r\n", waterlevel); - - wifi_send(); - //wait(5); + + val = inputPin.read(); + + if (val==0) { + cnt++; + pc.printf(" The Sensor is ON And I Detected = %i Till NOW\r\n",cnt); + Buzzer = 1; + pc.printf("PLEASE STAY AWAY\r\n"); + pc.printf("Sending WiFi information\n\r"); + 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); + } + } } -} \ No newline at end of file + +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\",80",IP); //Initiate connection with THINGSPEAK server + wifi.SendCMD(snd); + pc.printf(snd); + wifi.RcvReply(rcv, 3000); + pc.printf("%s", rcv); + + strcpy(snd,"AT+CIPSEND=4,47"); //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 /update?key=WZXOHNJSLN9G1AGP&field1=%2.2f\r\n",1.0); //Post values to thingspeak + pc.printf("%s",snd); + wifi.SendCMD(snd); + + 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); +}