IoT Based Smart Garbage Monitoring System/Debjyoti Sinha and his group/Purdue School of Engineering and Technology
Dependencies: mbed HCSR04 ESP8266
main.cpp@0:c46e0e7ec1bb, 2018-12-13 (annotated)
- Committer:
- debjyoti123
- Date:
- Thu Dec 13 01:41:15 2018 +0000
- Revision:
- 0:c46e0e7ec1bb
IOT Based Smart Garbage Monitoring System using FRDM-K64F Development board-Debjyoti Sinha and group
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
debjyoti123 | 0:c46e0e7ec1bb | 1 | #include "mbed.h" |
debjyoti123 | 0:c46e0e7ec1bb | 2 | #include "mbed.h" |
debjyoti123 | 0:c46e0e7ec1bb | 3 | #include "hcsr04.h" |
debjyoti123 | 0:c46e0e7ec1bb | 4 | #include "ESP8266.h" |
debjyoti123 | 0:c46e0e7ec1bb | 5 | |
debjyoti123 | 0:c46e0e7ec1bb | 6 | #define APIKEY QSL7YVXB73QHN2EK //Put "Write key" of your channel in thingspeak.com |
debjyoti123 | 0:c46e0e7ec1bb | 7 | #define IP "184.106.153.149" // IP Address of "api.thingspeak.com\" |
debjyoti123 | 0:c46e0e7ec1bb | 8 | const char *WIFI_SSID = "ssid"; // Enter your Wi-Fi name |
debjyoti123 | 0:c46e0e7ec1bb | 9 | const char *WIFI_PASS = "password" ; // Enter your Wi-Fi password |
debjyoti123 | 0:c46e0e7ec1bb | 10 | //#define WIFI_SSID "ssid" |
debjyoti123 | 0:c46e0e7ec1bb | 11 | //#define WIFI_PASS "password" |
debjyoti123 | 0:c46e0e7ec1bb | 12 | HCSR04 usensor(PTC12,PTC4); |
debjyoti123 | 0:c46e0e7ec1bb | 13 | DigitalOut trigger(PTC12); |
debjyoti123 | 0:c46e0e7ec1bb | 14 | DigitalOut RED(LED1); //monitor trigger |
debjyoti123 | 0:c46e0e7ec1bb | 15 | DigitalOut GREEN(LED2); //monitor echo |
debjyoti123 | 0:c46e0e7ec1bb | 16 | InterruptIn echo(PTC4); |
debjyoti123 | 0:c46e0e7ec1bb | 17 | |
debjyoti123 | 0:c46e0e7ec1bb | 18 | |
debjyoti123 | 0:c46e0e7ec1bb | 19 | Serial pc (USBTX, USBRX); //Serial Communication |
debjyoti123 | 0:c46e0e7ec1bb | 20 | ESP8266 wifi (PTC17, PTC16, 115200); //Setting baud rate for Wifi Esp Module |
debjyoti123 | 0:c46e0e7ec1bb | 21 | char snd[255], rcv[1000]; |
debjyoti123 | 0:c46e0e7ec1bb | 22 | #define IP "184.106.153.149"//IP address of thingspeak.com |
debjyoti123 | 0:c46e0e7ec1bb | 23 | unsigned int pulsedur; |
debjyoti123 | 0:c46e0e7ec1bb | 24 | |
debjyoti123 | 0:c46e0e7ec1bb | 25 | Timer pulsetime; |
debjyoti123 | 0:c46e0e7ec1bb | 26 | |
debjyoti123 | 0:c46e0e7ec1bb | 27 | void wifi_send(void); |
debjyoti123 | 0:c46e0e7ec1bb | 28 | int main() |
debjyoti123 | 0:c46e0e7ec1bb | 29 | { |
debjyoti123 | 0:c46e0e7ec1bb | 30 | unsigned int distance; |
debjyoti123 | 0:c46e0e7ec1bb | 31 | pc.baud(115200); |
debjyoti123 | 0:c46e0e7ec1bb | 32 | //Initializing Wi-Fi |
debjyoti123 | 0:c46e0e7ec1bb | 33 | pc.printf("SET mode to AP\r\n"); |
debjyoti123 | 0:c46e0e7ec1bb | 34 | wifi.SetMode(1); //set the mode of ESP to 1 |
debjyoti123 | 0:c46e0e7ec1bb | 35 | wifi.RcvReply(rcv,1000); //receive response by ESP |
debjyoti123 | 0:c46e0e7ec1bb | 36 | pc.printf("%s",rcv); //print the response |
debjyoti123 | 0:c46e0e7ec1bb | 37 | pc.printf("Connect to the Wifi\r\n"); //wifi name & password |
debjyoti123 | 0:c46e0e7ec1bb | 38 | wifi.Join("WIFI_SSID","WIFI_PASS"); |
debjyoti123 | 0:c46e0e7ec1bb | 39 | wifi.RcvReply(rcv, 1000); //receive response by ESP |
debjyoti123 | 0:c46e0e7ec1bb | 40 | pc.printf("%s\n",rcv); //print the response |
debjyoti123 | 0:c46e0e7ec1bb | 41 | wait(8); //waiting for the response from ESP |
debjyoti123 | 0:c46e0e7ec1bb | 42 | pc.printf("Getting IP\r\n"); //getting the IP address from the connected AP |
debjyoti123 | 0:c46e0e7ec1bb | 43 | wifi.GetIP(rcv); //IP address received from AP |
debjyoti123 | 0:c46e0e7ec1bb | 44 | pc.printf("%s\n",rcv); //print response on TeraTerm |
debjyoti123 | 0:c46e0e7ec1bb | 45 | pc.printf("Initializing the Wi-Fi\r\n"); |
debjyoti123 | 0:c46e0e7ec1bb | 46 | |
debjyoti123 | 0:c46e0e7ec1bb | 47 | |
debjyoti123 | 0:c46e0e7ec1bb | 48 | //Loop to read distance, scale and print |
debjyoti123 | 0:c46e0e7ec1bb | 49 | while(1) |
debjyoti123 | 0:c46e0e7ec1bb | 50 | { |
debjyoti123 | 0:c46e0e7ec1bb | 51 | trigger=1; |
debjyoti123 | 0:c46e0e7ec1bb | 52 | RED=1; |
debjyoti123 | 0:c46e0e7ec1bb | 53 | GREEN=0; |
debjyoti123 | 0:c46e0e7ec1bb | 54 | pulsetime.reset(); |
debjyoti123 | 0:c46e0e7ec1bb | 55 | wait_us(10); |
debjyoti123 | 0:c46e0e7ec1bb | 56 | trigger=0; |
debjyoti123 | 0:c46e0e7ec1bb | 57 | RED=0; |
debjyoti123 | 0:c46e0e7ec1bb | 58 | while(echo==0){}; |
debjyoti123 | 0:c46e0e7ec1bb | 59 | GREEN=echo; |
debjyoti123 | 0:c46e0e7ec1bb | 60 | pulsetime.start(); |
debjyoti123 | 0:c46e0e7ec1bb | 61 | while(echo==1){}; |
debjyoti123 | 0:c46e0e7ec1bb | 62 | pulsetime.stop(); |
debjyoti123 | 0:c46e0e7ec1bb | 63 | pulsedur = pulsetime.read_us(); |
debjyoti123 | 0:c46e0e7ec1bb | 64 | distance= (pulsedur*343)/20000; |
debjyoti123 | 0:c46e0e7ec1bb | 65 | GREEN=0; |
debjyoti123 | 0:c46e0e7ec1bb | 66 | pc.printf("Distance is %d cm\n\r" ,distance); |
debjyoti123 | 0:c46e0e7ec1bb | 67 | pc.printf("Uploading status to the cloud\n\r"); |
debjyoti123 | 0:c46e0e7ec1bb | 68 | //wait before sending another ping since echo(s) may return |
debjyoti123 | 0:c46e0e7ec1bb | 69 | wait(10); |
debjyoti123 | 0:c46e0e7ec1bb | 70 | wifi_send(); |
debjyoti123 | 0:c46e0e7ec1bb | 71 | }; |
debjyoti123 | 0:c46e0e7ec1bb | 72 | } |
debjyoti123 | 0:c46e0e7ec1bb | 73 | //End of main program |
debjyoti123 | 0:c46e0e7ec1bb | 74 | //Routing data to cloud |
debjyoti123 | 0:c46e0e7ec1bb | 75 | void wifi_send(void) |
debjyoti123 | 0:c46e0e7ec1bb | 76 | { |
debjyoti123 | 0:c46e0e7ec1bb | 77 | unsigned int distance; |
debjyoti123 | 0:c46e0e7ec1bb | 78 | pc.printf("Uploading Wi-Fi Data"); |
debjyoti123 | 0:c46e0e7ec1bb | 79 | pc.printf("\r\nSet Wi-Fi into Single Channel Mode\r\n"); |
debjyoti123 | 0:c46e0e7ec1bb | 80 | strcpy(snd,"AT+CIPMUX=0"); //set wi-fi into single channel mode |
debjyoti123 | 0:c46e0e7ec1bb | 81 | wifi.SendCMD(snd); |
debjyoti123 | 0:c46e0e7ec1bb | 82 | pc.printf(snd); |
debjyoti123 | 0:c46e0e7ec1bb | 83 | wifi.RcvReply(rcv,1000); |
debjyoti123 | 0:c46e0e7ec1bb | 84 | pc.printf("\r\nMode status is %s",rcv); |
debjyoti123 | 0:c46e0e7ec1bb | 85 | //Connecting to Thingspeak server |
debjyoti123 | 0:c46e0e7ec1bb | 86 | pc.printf("\r\nConnecting to Thingspeak server\r\n"); |
debjyoti123 | 0:c46e0e7ec1bb | 87 | strcpy(snd,"AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",80"); |
debjyoti123 | 0:c46e0e7ec1bb | 88 | pc.printf("\r\nSending data: %s",snd); |
debjyoti123 | 0:c46e0e7ec1bb | 89 | wifi.SendCMD(snd); |
debjyoti123 | 0:c46e0e7ec1bb | 90 | wifi.RcvReply(rcv,1000); |
debjyoti123 | 0:c46e0e7ec1bb | 91 | pc.printf("\r\nServer status: %s",rcv); |
debjyoti123 | 0:c46e0e7ec1bb | 92 | //Deliver Data |
debjyoti123 | 0:c46e0e7ec1bb | 93 | pc.printf("\r\nDelivering Characters"); |
debjyoti123 | 0:c46e0e7ec1bb | 94 | strcpy(snd,"AT+CIPSEND=4,47\n"); |
debjyoti123 | 0:c46e0e7ec1bb | 95 | wifi.SendCMD(snd); |
debjyoti123 | 0:c46e0e7ec1bb | 96 | wifi.RcvReply(rcv,1000); |
debjyoti123 | 0:c46e0e7ec1bb | 97 | pc.printf("\r\nStatus sent: %s",rcv); |
debjyoti123 | 0:c46e0e7ec1bb | 98 | //Upload values to cloud website |
debjyoti123 | 0:c46e0e7ec1bb | 99 | pc.printf("Uploading data to Thingspeak\r\n"); |
debjyoti123 | 0:c46e0e7ec1bb | 100 | sprintf(snd,"GET https://api.thingspeak.com/update?key=QSL7YVXB73QHN2EK\n",distance); |
debjyoti123 | 0:c46e0e7ec1bb | 101 | pc.printf("%s",snd); |
debjyoti123 | 0:c46e0e7ec1bb | 102 | wifi.SendCMD(snd); |
debjyoti123 | 0:c46e0e7ec1bb | 103 | wait(2); |
debjyoti123 | 0:c46e0e7ec1bb | 104 | wifi.RcvReply(rcv,1000); |
debjyoti123 | 0:c46e0e7ec1bb | 105 | pc.printf("\r\nStatus sent: %s",rcv); |
debjyoti123 | 0:c46e0e7ec1bb | 106 | wait(2); |
debjyoti123 | 0:c46e0e7ec1bb | 107 | //Closing connection |
debjyoti123 | 0:c46e0e7ec1bb | 108 | pc.printf("\r\nClose the connection"); |
debjyoti123 | 0:c46e0e7ec1bb | 109 | strcpy(snd,"AT+CIPCLOSE");//Closing connection to the server |
debjyoti123 | 0:c46e0e7ec1bb | 110 | wifi.SendCMD(snd); |
debjyoti123 | 0:c46e0e7ec1bb | 111 | wifi.RcvReply(rcv,1000); |
debjyoti123 | 0:c46e0e7ec1bb | 112 | pc.printf("%s",rcv); |
debjyoti123 | 0:c46e0e7ec1bb | 113 | } |
debjyoti123 | 0:c46e0e7ec1bb | 114 | |
debjyoti123 | 0:c46e0e7ec1bb | 115 | |
debjyoti123 | 0:c46e0e7ec1bb | 116 |