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