IoT Based Smart Garbage Monitoring System/Debjyoti Sinha and his group/Purdue School of Engineering and Technology
Dependencies: mbed HCSR04 ESP8266
main.cpp
00001 #include "mbed.h" 00002 #include "mbed.h" 00003 #include "hcsr04.h" 00004 #include "ESP8266.h" 00005 00006 #define APIKEY QSL7YVXB73QHN2EK //Put "Write key" of your channel in thingspeak.com 00007 #define IP "184.106.153.149" // IP Address of "api.thingspeak.com\" 00008 const char *WIFI_SSID = "ssid"; // Enter your Wi-Fi name 00009 const char *WIFI_PASS = "password" ; // Enter your Wi-Fi password 00010 //#define WIFI_SSID "ssid" 00011 //#define WIFI_PASS "password" 00012 HCSR04 usensor(PTC12,PTC4); 00013 DigitalOut trigger(PTC12); 00014 DigitalOut RED(LED1); //monitor trigger 00015 DigitalOut GREEN(LED2); //monitor echo 00016 InterruptIn echo(PTC4); 00017 00018 00019 Serial pc (USBTX, USBRX); //Serial Communication 00020 ESP8266 wifi (PTC17, PTC16, 115200); //Setting baud rate for Wifi Esp Module 00021 char snd[255], rcv[1000]; 00022 #define IP "184.106.153.149"//IP address of thingspeak.com 00023 unsigned int pulsedur; 00024 00025 Timer pulsetime; 00026 00027 void wifi_send(void); 00028 int main() 00029 { 00030 unsigned int distance; 00031 pc.baud(115200); 00032 //Initializing Wi-Fi 00033 pc.printf("SET mode to AP\r\n"); 00034 wifi.SetMode(1); //set the mode of ESP to 1 00035 wifi.RcvReply(rcv,1000); //receive response by ESP 00036 pc.printf("%s",rcv); //print the response 00037 pc.printf("Connect to the Wifi\r\n"); //wifi name & password 00038 wifi.Join("WIFI_SSID","WIFI_PASS"); 00039 wifi.RcvReply(rcv, 1000); //receive response by ESP 00040 pc.printf("%s\n",rcv); //print the response 00041 wait(8); //waiting for the response from ESP 00042 pc.printf("Getting IP\r\n"); //getting the IP address from the connected AP 00043 wifi.GetIP(rcv); //IP address received from AP 00044 pc.printf("%s\n",rcv); //print response on TeraTerm 00045 pc.printf("Initializing the Wi-Fi\r\n"); 00046 00047 00048 //Loop to read distance, scale and print 00049 while(1) 00050 { 00051 trigger=1; 00052 RED=1; 00053 GREEN=0; 00054 pulsetime.reset(); 00055 wait_us(10); 00056 trigger=0; 00057 RED=0; 00058 while(echo==0){}; 00059 GREEN=echo; 00060 pulsetime.start(); 00061 while(echo==1){}; 00062 pulsetime.stop(); 00063 pulsedur = pulsetime.read_us(); 00064 distance= (pulsedur*343)/20000; 00065 GREEN=0; 00066 pc.printf("Distance is %d cm\n\r" ,distance); 00067 pc.printf("Uploading status to the cloud\n\r"); 00068 //wait before sending another ping since echo(s) may return 00069 wait(10); 00070 wifi_send(); 00071 }; 00072 } 00073 //End of main program 00074 //Routing data to cloud 00075 void wifi_send(void) 00076 { 00077 unsigned int distance; 00078 pc.printf("Uploading Wi-Fi Data"); 00079 pc.printf("\r\nSet Wi-Fi into Single Channel Mode\r\n"); 00080 strcpy(snd,"AT+CIPMUX=0"); //set wi-fi into single channel mode 00081 wifi.SendCMD(snd); 00082 pc.printf(snd); 00083 wifi.RcvReply(rcv,1000); 00084 pc.printf("\r\nMode status is %s",rcv); 00085 //Connecting to Thingspeak server 00086 pc.printf("\r\nConnecting to Thingspeak server\r\n"); 00087 strcpy(snd,"AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",80"); 00088 pc.printf("\r\nSending data: %s",snd); 00089 wifi.SendCMD(snd); 00090 wifi.RcvReply(rcv,1000); 00091 pc.printf("\r\nServer status: %s",rcv); 00092 //Deliver Data 00093 pc.printf("\r\nDelivering Characters"); 00094 strcpy(snd,"AT+CIPSEND=4,47\n"); 00095 wifi.SendCMD(snd); 00096 wifi.RcvReply(rcv,1000); 00097 pc.printf("\r\nStatus sent: %s",rcv); 00098 //Upload values to cloud website 00099 pc.printf("Uploading data to Thingspeak\r\n"); 00100 sprintf(snd,"GET https://api.thingspeak.com/update?key=QSL7YVXB73QHN2EK\n",distance); 00101 pc.printf("%s",snd); 00102 wifi.SendCMD(snd); 00103 wait(2); 00104 wifi.RcvReply(rcv,1000); 00105 pc.printf("\r\nStatus sent: %s",rcv); 00106 wait(2); 00107 //Closing connection 00108 pc.printf("\r\nClose the connection"); 00109 strcpy(snd,"AT+CIPCLOSE");//Closing connection to the server 00110 wifi.SendCMD(snd); 00111 wifi.RcvReply(rcv,1000); 00112 pc.printf("%s",rcv); 00113 } 00114 00115 00116
Generated on Fri Aug 12 2022 15:54:54 by 1.7.2