PIR grove sensor that sends the sensed data to Thingspeak.com through the wifi module ESP 8266

Dependencies:   mbed

Committer:
skrawool
Date:
Mon Dec 05 16:39:27 2016 +0000
Revision:
0:3954a906acc2
PIR grove sensor that sends the sensed data to thingspeak through the wifi module ESP 8266

Who changed what in which revision?

UserRevisionLine numberNew contents of line
skrawool 0:3954a906acc2 1 #include "mbed.h"
skrawool 0:3954a906acc2 2 #include "ESP8266.h"
skrawool 0:3954a906acc2 3
skrawool 0:3954a906acc2 4 InterruptIn motion(D2);
skrawool 0:3954a906acc2 5
skrawool 0:3954a906acc2 6 Serial pc(USBTX,USBRX); //UART/Serial Communication
skrawool 0:3954a906acc2 7
skrawool 0:3954a906acc2 8 ESP8266 wifi(PTE0, PTE1, 115200); // Setting up Baud Rate for WIFI ESP Module
skrawool 0:3954a906acc2 9 char snd[255],rcv[1000];
skrawool 0:3954a906acc2 10
skrawool 0:3954a906acc2 11 #define IP "184.106.153.149" // Defining Thingspeak IP Address
skrawool 0:3954a906acc2 12
skrawool 0:3954a906acc2 13
skrawool 0:3954a906acc2 14 int dm = 0; //dm = Detected Motion
skrawool 0:3954a906acc2 15
skrawool 0:3954a906acc2 16 void motiondetection(void)
skrawool 0:3954a906acc2 17 {
skrawool 0:3954a906acc2 18 dm = 1;
skrawool 0:3954a906acc2 19 }
skrawool 0:3954a906acc2 20
skrawool 0:3954a906acc2 21 //Defining the necessary Functions to be used//
skrawool 0:3954a906acc2 22
skrawool 0:3954a906acc2 23 void wifi_send(void);
skrawool 0:3954a906acc2 24
skrawool 0:3954a906acc2 25 int cnt;
skrawool 0:3954a906acc2 26
skrawool 0:3954a906acc2 27
skrawool 0:3954a906acc2 28 //Execution of Main program
skrawool 0:3954a906acc2 29
skrawool 0:3954a906acc2 30 int main(void)
skrawool 0:3954a906acc2 31 {
skrawool 0:3954a906acc2 32 cnt = 0; //Setting counter value to zero
skrawool 0:3954a906acc2 33 motion.rise(&motiondetection);
skrawool 0:3954a906acc2 34
skrawool 0:3954a906acc2 35 pc.baud(9600); //Setting up Baud Rate for UART
skrawool 0:3954a906acc2 36
skrawool 0:3954a906acc2 37 // WIFI Initialization Part//
skrawool 0:3954a906acc2 38
skrawool 0:3954a906acc2 39 pc.printf("SET mode to AP\r\n");
skrawool 0:3954a906acc2 40 wifi.SetMode(1); // Setting ESP to mode 1
skrawool 0:3954a906acc2 41 wifi.RcvReply(rcv, 1000); //In order to receive response from ESP
skrawool 0:3954a906acc2 42 pc.printf("%s",rcv); //Display obtained response on TeraTerm
skrawool 0:3954a906acc2 43 pc.printf("Conneting to AP\r\n");
skrawool 0:3954a906acc2 44
skrawool 0:3954a906acc2 45 //wifi.Join("BHNTG1682G0382", "a2c77382");
skrawool 0:3954a906acc2 46 wifi.Join("hello", "hellohello"); // Add Username and Password of the Network to which WIFI ESP module is connected
skrawool 0:3954a906acc2 47 wifi.RcvReply(rcv, 1000); //In order to receive response from ESP
skrawool 0:3954a906acc2 48 pc.printf("%s", rcv); //Display obtained response on TeraTerm
skrawool 0:3954a906acc2 49 wait(8); //Wait for 8 seconds
skrawool 0:3954a906acc2 50
skrawool 0:3954a906acc2 51 pc.printf("Getting IP\r\n"); // Gets IP address
skrawool 0:3954a906acc2 52 wifi.GetIP(rcv); //To receive IP
skrawool 0:3954a906acc2 53 pc.printf("%s", rcv);//Display obtained response on TeraTerm
skrawool 0:3954a906acc2 54 wait(5); // Delay 5 sec to give the pir time to get snapshut of the surrounding
skrawool 0:3954a906acc2 55 pc.printf("Initializing WiFi\r\n");
skrawool 0:3954a906acc2 56
skrawool 0:3954a906acc2 57 while(1) {
skrawool 0:3954a906acc2 58 if(dm) {
skrawool 0:3954a906acc2 59 cnt++;
skrawool 0:3954a906acc2 60 dm = 0;
skrawool 0:3954a906acc2 61 pc.printf("ALERT!! Intrusion Detected!! I've detected %d times since reset\n", cnt);
skrawool 0:3954a906acc2 62 pc.printf("Now uploading status to Cloud\n\r");
skrawool 0:3954a906acc2 63 wifi_send();
skrawool 0:3954a906acc2 64 }
skrawool 0:3954a906acc2 65 }
skrawool 0:3954a906acc2 66 }
skrawool 0:3954a906acc2 67 // Main program ends//
skrawool 0:3954a906acc2 68
skrawool 0:3954a906acc2 69
skrawool 0:3954a906acc2 70 //Routing the inofrmation to Cloud//
skrawool 0:3954a906acc2 71
skrawool 0:3954a906acc2 72 void wifi_send(void)
skrawool 0:3954a906acc2 73 {
skrawool 0:3954a906acc2 74 pc.printf("*************Uploading WIFI Data*************\r\n");
skrawool 0:3954a906acc2 75
skrawool 0:3954a906acc2 76 pc.printf("\r\nTo set WIFI into Single Channel mode\r\n");
skrawool 0:3954a906acc2 77
skrawool 0:3954a906acc2 78 strcpy(snd,"AT+CIPMUX=0");//To Set WIFI into Single Channel mode
skrawool 0:3954a906acc2 79 wifi.SendCMD(snd);
skrawool 0:3954a906acc2 80 pc.printf(snd);
skrawool 0:3954a906acc2 81
skrawool 0:3954a906acc2 82 wifi.RcvReply(rcv, 1000);
skrawool 0:3954a906acc2 83 pc.printf("\r\nMode Status: %s", rcv);
skrawool 0:3954a906acc2 84
skrawool 0:3954a906acc2 85
skrawool 0:3954a906acc2 86
skrawool 0:3954a906acc2 87 //Connecting to THINGSPEAK server//
skrawool 0:3954a906acc2 88 pc.printf("\r\nConnecting to THINGSPEAK server\r\n");
skrawool 0:3954a906acc2 89 strcpy(snd,"AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",80");
skrawool 0:3954a906acc2 90 pc.printf("\r\nSending: %s",snd);
skrawool 0:3954a906acc2 91 wifi.SendCMD(snd);
skrawool 0:3954a906acc2 92 wifi.RcvReply(rcv, 1000);
skrawool 0:3954a906acc2 93 pc.printf("\r\nSever Status: %s", rcv);
skrawool 0:3954a906acc2 94
skrawool 0:3954a906acc2 95 //Deliver Data/Characters//
skrawool 0:3954a906acc2 96 pc.printf("\r\nDelivering Data..");
skrawool 0:3954a906acc2 97 strcpy(snd,"AT+CIPSEND=84");
skrawool 0:3954a906acc2 98 wifi.SendCMD(snd);
skrawool 0:3954a906acc2 99 pc.printf("\r\nSending: %s",snd);
skrawool 0:3954a906acc2 100 wifi.RcvReply(rcv, 1000);
skrawool 0:3954a906acc2 101 pc.printf("\r\nSent Status: %s", rcv);
skrawool 0:3954a906acc2 102
skrawool 0:3954a906acc2 103 //Upload values to Cloud based website i.e. ThingSpeak in order to obtain Graphical Display//
skrawool 0:3954a906acc2 104
skrawool 0:3954a906acc2 105 pc.printf("Uploading values to Thingspeak\r\n");
skrawool 0:3954a906acc2 106 sprintf(snd,"GET https://api.thingspeak.com/update?key=2DNGOOKD3OZBMVV5&field1=%d HTTP/1.0\r\n\r\n",cnt); //67,76,84
skrawool 0:3954a906acc2 107 //sprintf(snd,"GET https://api.thingspeak.com/update?key=2DNGOOKD3OZBMVV5&field1=%d HTTP/1.0\r\n\r\n",cnt); //67,76,84
skrawool 0:3954a906acc2 108 pc.printf("\r\nSending: %s",snd);
skrawool 0:3954a906acc2 109 wifi.SendCMD(snd);
skrawool 0:3954a906acc2 110 wait(1);
skrawool 0:3954a906acc2 111 wifi.RcvReply(rcv, 1000);
skrawool 0:3954a906acc2 112 pc.printf("\r\nSent Status: %s", rcv);
skrawool 0:3954a906acc2 113 wait(1);
skrawool 0:3954a906acc2 114
skrawool 0:3954a906acc2 115 //Closing the connection with Server//
skrawool 0:3954a906acc2 116
skrawool 0:3954a906acc2 117 pc.printf("\r\nClose the Connection\r\n");
skrawool 0:3954a906acc2 118 strcpy(snd,"AT+CIPCLOSE"); //Command used to close the connection with server
skrawool 0:3954a906acc2 119 wifi.SendCMD(snd);
skrawool 0:3954a906acc2 120 pc.printf("\r\nSending: %s",snd);
skrawool 0:3954a906acc2 121 wait(1);
skrawool 0:3954a906acc2 122 wifi.RcvReply(rcv, 1000);
skrawool 0:3954a906acc2 123 pc.printf("Close Connection Status: %s", rcv);
skrawool 0:3954a906acc2 124
skrawool 0:3954a906acc2 125 pc.printf("\r\nClose connection\r\n");
skrawool 0:3954a906acc2 126 strcpy(snd,"AT+CIPCLOSE");
skrawool 0:3954a906acc2 127 wifi.SendCMD(snd);
skrawool 0:3954a906acc2 128 pc.printf("\r\nSending: %s",snd);
skrawool 0:3954a906acc2 129 wifi.RcvReply(rcv, 1000);
skrawool 0:3954a906acc2 130 pc.printf("Close Connection Status: %s", rcv);
skrawool 0:3954a906acc2 131 pc.printf("\r\nEnd of Closed Connection Status Response\r\n\r\n");
skrawool 0:3954a906acc2 132
skrawool 0:3954a906acc2 133 }