IoT Based Smart Garbage Monitoring System

Dependencies:   mbed hcsr04 ESP8266

Committer:
seetarampandey
Date:
Thu Dec 06 15:39:28 2018 +0000
Revision:
0:f7af876a43e9
IoT Based Smart Garbage Monitoring System

Who changed what in which revision?

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