Original Code Repo: https://os.mbed.com/users/priyank12p/code/Electronically-Connected-Intelligent-She/ Modified for Fall 2021 students.

Dependencies:   mbed mbed-http ESP8266

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002 Electronically Controlled Intelligent Shelves
00003 Developed by: Priyank Kalgaonkar
00004 **/
00005 
00006 #include "mbed.h"
00007 #include "hcsr04.h"
00008 #include "ESP8266.h"
00009 #include "math.h"
00010 #define CloudIP "184.106.153.149"           //Raw IP Address of ThingSpeak Cloud Server
00011 
00012 DigitalOut RLed(LED1);                      //Onboard Red LED = Shelf Out of Stock
00013 DigitalOut GLed(LED2);                      //Onboard Green LED = All OK
00014 DigitalOut BLed(LED3);                      //Onboard Blue LED for Wifi Tx Indication
00015 HCSR04 usensor1(D8,D9);                     //ECHO Pin=D9, TRIG Pin=D8
00016 HCSR04 usensor2(D7,D6);                     //ECHO Pin=D7, TRIG Pin=D6
00017 Serial pc(USBTX,USBRX);                     //Serial Communication with PC
00018 ESP8266 wifi(PTC17, PTC16, 115200);         //Tx Pin:PTC17; Rx Pin:PTC17; Baud rate:115200
00019 
00020 void wifi_send(void);;                      //Connect and Push Data Channel to Cloud Server
00021 
00022 int num = 0;
00023 int distance1, distance2;
00024 float dist_remaining1, dist_percent1, dist_remaining2, dist_percent2;
00025 char snd[255],rcv[1000];                    //snd: send command to ESP8266
00026                                             //rcv: receive response from ESP8266
00027 
00028 int main()
00029 {
00030     pc.baud(115200);                        //Baud Rate of 115200 for Tera Term
00031     
00032     pc.printf("########  ######  ####  ######      ######  ##    ##  ######  ######## ######## ##     ##\n\r");
00033     pc.printf("##       ##    ##  ##  ##    ##    ##    ##  ##  ##  ##    ##    ##    ##       ###   ###\n\r");
00034     pc.printf("##       ##        ##  ##          ##         ####   ##          ##    ##       #### ####\n\r");
00035     pc.printf("######   ##        ##   ######      ######     ##     ######     ##    ######   ## ### ##\n\r");
00036     pc.printf("##       ##        ##        ##          ##    ##          ##    ##    ##       ##     ##\n\r");
00037     pc.printf("##       ##    ##  ##  ##    ##    ##    ##    ##    ##    ##    ##    ##       ##     ##\n\r");
00038     pc.printf("########  ######  ####  ######      ######     ##     ######     ##    ######## ##     ##\n\r");
00039     pc.printf("-----------------------------------------------------------------------------------------\n\r");
00040     pc.printf("Developed By: Priyank Kalgaonkar\n\r");
00041     pc.printf("-----------------------------------------------------------------------------------------\n\r\n\r");
00042     
00043     pc.printf("Initial Setup\r\n");
00044     wifi.SetMode(1);                        //Set ESP mode to 1
00045     wifi.RcvReply(rcv, 1000);               //Receive a response from ESP
00046     pc.printf("%s\r", rcv);
00047 
00048     pc.printf("Connecting to WiFi\r\n");    //AP Setup Initialization
00049     wifi.Join("Z", "12345678");//Put your Wifi SSID followed by Password WiFi_SSID WiFi_Password
00050     wifi.RcvReply(rcv, 1000);
00051     pc.printf("%s\n", rcv);
00052     wait(8);
00053     
00054     wifi.GetIP(rcv);                        //Obtains an IP address from the AP
00055     
00056     while (1) 
00057     {
00058         wifi_send();
00059         
00060         RLed = 1;
00061         GLed = 1;
00062         BLed = 0;
00063         wait(2.0f);
00064     }
00065 }
00066 
00067 void wifi_send(void)
00068 {
00069     while(num<1000000000000)
00070     {
00071         num=num+1;
00072         pc.printf("\nCloud Sync Instance #: %d\n\r", num);
00073         pc.printf("Syncing Data with Cloud, Please Wait.\n\r");
00074         
00075     //Ultrasound Sensor (HC-SR04) #1 Initialization
00076         int a = 30;
00077         usensor1.start();
00078         wait_ms(500);
00079         
00080     //Calculating Distance Percentage Remaining for Sensor # 1
00081         distance1 = usensor1.get_dist_cm();
00082         dist_remaining1 = a-distance1;
00083         dist_percent1 = (dist_remaining1/30)*100;
00084         
00085     //LED and Tera Term Output
00086         if (distance1<30 && distance2<30) {
00087             RLed = 1;
00088             BLed = 1;
00089             GLed = 0;
00090             //printf("Percent remaining: %f\r", dist_percent1 && dist_percent2);
00091         } else {
00092             GLed = 1;
00093             BLed = 1;
00094             RLed = 0;
00095             printf("Shelves Empty! Replenish Stock.\n\r");
00096         }    
00097         
00098     //Sending Data to the Cloud Server via ESP8266 WiFi Module
00099         strcpy(snd,"AT+CIPMUX=0\n\r");        //AT+CIPMUX: Enabling Single Channel Mode
00100         wifi.SendCMD(snd);
00101         wait(1);
00102         wifi.RcvReply(rcv, 1000);
00103         wait(1);
00104         
00105     //Establish TCP connection w/ Cloud Server
00106         sprintf(snd,"AT+CIPSTART=4,\"TCP\",\"%s\",80\n",CloudIP);
00107         wait(1);
00108         wifi.RcvReply(rcv, 1000);
00109         wait(1);
00110         
00111     //Set length of the data that will be sent
00112         strcpy(snd,"AT+CIPSEND=100\n\r");
00113         wifi.SendCMD(snd);
00114         pc.printf("%s\r", rcv);
00115         wait(1);
00116         wifi.RcvReply(rcv, 1000);
00117         pc.printf("%s\r", rcv);
00118         wait(1);
00119         
00120     //Pushing the data acquired from HC-SR04 Ultrasonic Sensor to Cloud Server via API
00121     //Replace with your own API Request - Write a Channel Feed below
00122         pc.printf("Product X - Sensor 1: \n\r");
00123         sprintf(snd,"\r ***Replace with your own API Request - Write a Channel Feed below*** \r", dist_percent1);
00124         printf("Percent Stock X Remaining: %f\n\r", dist_percent1);
00125         wifi.SendCMD(snd);
00126         pc.printf("%s\r",snd);
00127         wait(0.5);
00128         wifi.RcvReply(rcv, 1000);
00129         pc.printf("%s\r", rcv);     
00130     }
00131 }