Original Code Repo: https://os.mbed.com/users/priyank12p/code/Electronically-Connected-Intelligent-She/ Modified for Fall 2021 students.
Dependencies: mbed mbed-http ESP8266
main.cpp@10:e4b6bc6d9b07, 2020-08-19 (annotated)
- Committer:
- jamiedillion2019
- Date:
- Wed Aug 19 18:51:26 2020 +0000
- Revision:
- 10:e4b6bc6d9b07
- Parent:
- 9:607ee6e92552
- Child:
- 11:756321f0b0cd
Updated Original Code Repo from another account;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jamiedillion2019 | 10:e4b6bc6d9b07 | 1 | /** |
jamiedillion2019 | 10:e4b6bc6d9b07 | 2 | Electronically Controlled Intelligent Shelves |
jamiedillion2019 | 10:e4b6bc6d9b07 | 3 | Developed by: Priyank Kalgaonkar |
jakobjohn949 | 9:607ee6e92552 | 4 | **/ |
jamiedillion2019 | 10:e4b6bc6d9b07 | 5 | |
andcor02 | 8:25138f7b9309 | 6 | #include "mbed.h" |
andcor02 | 8:25138f7b9309 | 7 | #include "hcsr04.h" |
jamiedillion2019 | 10:e4b6bc6d9b07 | 8 | #include "ESP8266.h" |
jamiedillion2019 | 10:e4b6bc6d9b07 | 9 | #include "math.h" |
jamiedillion2019 | 10:e4b6bc6d9b07 | 10 | #define CloudIP "184.106.153.149" //Raw IP Address of ThingSpeak Cloud Server |
andcor02 | 8:25138f7b9309 | 11 | |
jamiedillion2019 | 10:e4b6bc6d9b07 | 12 | DigitalOut RLed(LED1); //Onboard Red LED = Shelf Out of Stock |
jamiedillion2019 | 10:e4b6bc6d9b07 | 13 | DigitalOut GLed(LED2); //Onboard Green LED = All OK |
jamiedillion2019 | 10:e4b6bc6d9b07 | 14 | DigitalOut BLed(LED3); //Onboard Blue LED for Wifi Tx Indication |
jamiedillion2019 | 10:e4b6bc6d9b07 | 15 | HCSR04 usensor1(D8,D9); //ECHO Pin=D9, TRIG Pin=D8 |
jamiedillion2019 | 10:e4b6bc6d9b07 | 16 | HCSR04 usensor2(D7,D6); //ECHO Pin=D7, TRIG Pin=D6 |
jamiedillion2019 | 10:e4b6bc6d9b07 | 17 | Serial pc(USBTX,USBRX); //Serial Communication with PC |
jamiedillion2019 | 10:e4b6bc6d9b07 | 18 | ESP8266 wifi(PTC17, PTC16, 115200); //Tx Pin:PTC17; Rx Pin:PTC17; Baud rate:115200 |
jamiedillion2019 | 10:e4b6bc6d9b07 | 19 | |
jamiedillion2019 | 10:e4b6bc6d9b07 | 20 | void wifi_send(void);; //Connect and Push Data Channel to Cloud Server |
jamiedillion2019 | 10:e4b6bc6d9b07 | 21 | |
jamiedillion2019 | 10:e4b6bc6d9b07 | 22 | int num = 0; |
jamiedillion2019 | 10:e4b6bc6d9b07 | 23 | int distance1, distance2; |
jamiedillion2019 | 10:e4b6bc6d9b07 | 24 | float dist_remaining1, dist_percent1, dist_remaining2, dist_percent2; |
jamiedillion2019 | 10:e4b6bc6d9b07 | 25 | char snd[255],rcv[1000]; //snd: send command to ESP8266 |
jamiedillion2019 | 10:e4b6bc6d9b07 | 26 | //rcv: receive response from ESP8266 |
jakobjohn949 | 9:607ee6e92552 | 27 | |
andcor02 | 8:25138f7b9309 | 28 | int main() |
andcor02 | 8:25138f7b9309 | 29 | { |
jamiedillion2019 | 10:e4b6bc6d9b07 | 30 | pc.baud(115200); //Baud Rate of 115200 for Tera Term |
jamiedillion2019 | 10:e4b6bc6d9b07 | 31 | |
jamiedillion2019 | 10:e4b6bc6d9b07 | 32 | pc.printf("######## ###### #### ###### ###### ## ## ###### ######## ######## ## ##\n\r"); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 33 | pc.printf("## ## ## ## ## ## ## ## ## ## ## ## ## ## ### ###\n\r"); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 34 | pc.printf("## ## ## ## ## #### ## ## ## #### ####\n\r"); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 35 | pc.printf("###### ## ## ###### ###### ## ###### ## ###### ## ### ##\n\r"); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 36 | pc.printf("## ## ## ## ## ## ## ## ## ## ##\n\r"); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 37 | pc.printf("## ## ## ## ## ## ## ## ## ## ## ## ## ## ##\n\r"); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 38 | pc.printf("######## ###### #### ###### ###### ## ###### ## ######## ## ##\n\r"); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 39 | pc.printf("-----------------------------------------------------------------------------------------\n\r"); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 40 | pc.printf("Developed By: Priyank Kalgaonkar\n\r"); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 41 | pc.printf("-----------------------------------------------------------------------------------------\n\r\n\r"); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 42 | |
jamiedillion2019 | 10:e4b6bc6d9b07 | 43 | pc.printf("Initial Setup\r\n"); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 44 | wifi.SetMode(1); //Set ESP mode to 1 |
jamiedillion2019 | 10:e4b6bc6d9b07 | 45 | wifi.RcvReply(rcv, 1000); //Receive a response from ESP |
jamiedillion2019 | 10:e4b6bc6d9b07 | 46 | pc.printf("%s\r", rcv); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 47 | |
jamiedillion2019 | 10:e4b6bc6d9b07 | 48 | pc.printf("Connecting to WiFi\r\n"); //AP Setup Initialization |
jamiedillion2019 | 10:e4b6bc6d9b07 | 49 | wifi.Join("Z", "12345678");//Put your Wifi SSID followed by Password WiFi_SSID WiFi_Password |
jamiedillion2019 | 10:e4b6bc6d9b07 | 50 | wifi.RcvReply(rcv, 1000); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 51 | pc.printf("%s\n", rcv); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 52 | wait(8); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 53 | |
jamiedillion2019 | 10:e4b6bc6d9b07 | 54 | wifi.GetIP(rcv); //Obtains an IP address from the AP |
jamiedillion2019 | 10:e4b6bc6d9b07 | 55 | |
jamiedillion2019 | 10:e4b6bc6d9b07 | 56 | while (1) |
jamiedillion2019 | 10:e4b6bc6d9b07 | 57 | { |
jamiedillion2019 | 10:e4b6bc6d9b07 | 58 | wifi_send(); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 59 | |
jamiedillion2019 | 10:e4b6bc6d9b07 | 60 | RLed = 1; |
jamiedillion2019 | 10:e4b6bc6d9b07 | 61 | GLed = 1; |
jamiedillion2019 | 10:e4b6bc6d9b07 | 62 | BLed = 0; |
jamiedillion2019 | 10:e4b6bc6d9b07 | 63 | wait(2.0f); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 64 | } |
jamiedillion2019 | 10:e4b6bc6d9b07 | 65 | } |
jamiedillion2019 | 10:e4b6bc6d9b07 | 66 | |
jamiedillion2019 | 10:e4b6bc6d9b07 | 67 | void wifi_send(void) |
jamiedillion2019 | 10:e4b6bc6d9b07 | 68 | { |
jamiedillion2019 | 10:e4b6bc6d9b07 | 69 | while(num<1000000000000) |
jamiedillion2019 | 10:e4b6bc6d9b07 | 70 | { |
jamiedillion2019 | 10:e4b6bc6d9b07 | 71 | num=num+1; |
jamiedillion2019 | 10:e4b6bc6d9b07 | 72 | pc.printf("\nCloud Sync Instance #: %d\n\r", num); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 73 | pc.printf("Syncing Data with Cloud, Please Wait.\n\r"); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 74 | |
jamiedillion2019 | 10:e4b6bc6d9b07 | 75 | //Ultrasound Sensor (HC-SR04) #1 Initialization |
jamiedillion2019 | 10:e4b6bc6d9b07 | 76 | int a = 30; |
jakobjohn949 | 9:607ee6e92552 | 77 | usensor1.start(); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 78 | wait_ms(500); |
jakobjohn949 | 9:607ee6e92552 | 79 | |
jamiedillion2019 | 10:e4b6bc6d9b07 | 80 | //Calculating Distance Percentage Remaining for Sensor # 1 |
jamiedillion2019 | 10:e4b6bc6d9b07 | 81 | distance1 = usensor1.get_dist_cm(); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 82 | dist_remaining1 = a-distance1; |
jamiedillion2019 | 10:e4b6bc6d9b07 | 83 | dist_percent1 = (dist_remaining1/30)*100; |
jamiedillion2019 | 10:e4b6bc6d9b07 | 84 | |
jamiedillion2019 | 10:e4b6bc6d9b07 | 85 | //LED and Tera Term Output |
jamiedillion2019 | 10:e4b6bc6d9b07 | 86 | if (distance1<30 && distance2<30) { |
jakobjohn949 | 9:607ee6e92552 | 87 | RLed = 1; |
jakobjohn949 | 9:607ee6e92552 | 88 | BLed = 1; |
jakobjohn949 | 9:607ee6e92552 | 89 | GLed = 0; |
jamiedillion2019 | 10:e4b6bc6d9b07 | 90 | //printf("Percent remaining: %f\r", dist_percent1 && dist_percent2); |
jakobjohn949 | 9:607ee6e92552 | 91 | } else { |
jakobjohn949 | 9:607ee6e92552 | 92 | GLed = 1; |
jakobjohn949 | 9:607ee6e92552 | 93 | BLed = 1; |
jakobjohn949 | 9:607ee6e92552 | 94 | RLed = 0; |
jamiedillion2019 | 10:e4b6bc6d9b07 | 95 | printf("Shelves Empty! Replenish Stock.\n\r"); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 96 | } |
jamiedillion2019 | 10:e4b6bc6d9b07 | 97 | |
jamiedillion2019 | 10:e4b6bc6d9b07 | 98 | //Sending Data to the Cloud Server via ESP8266 WiFi Module |
jamiedillion2019 | 10:e4b6bc6d9b07 | 99 | strcpy(snd,"AT+CIPMUX=0\n\r"); //AT+CIPMUX: Enabling Single Channel Mode |
jamiedillion2019 | 10:e4b6bc6d9b07 | 100 | wifi.SendCMD(snd); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 101 | wait(1); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 102 | wifi.RcvReply(rcv, 1000); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 103 | wait(1); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 104 | |
jamiedillion2019 | 10:e4b6bc6d9b07 | 105 | //Establish TCP connection w/ Cloud Server |
jamiedillion2019 | 10:e4b6bc6d9b07 | 106 | sprintf(snd,"AT+CIPSTART=4,\"TCP\",\"%s\",80\n",CloudIP); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 107 | wait(1); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 108 | wifi.RcvReply(rcv, 1000); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 109 | wait(1); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 110 | |
jamiedillion2019 | 10:e4b6bc6d9b07 | 111 | //Set length of the data that will be sent |
jamiedillion2019 | 10:e4b6bc6d9b07 | 112 | strcpy(snd,"AT+CIPSEND=100\n\r"); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 113 | wifi.SendCMD(snd); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 114 | pc.printf("%s\r", rcv); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 115 | wait(1); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 116 | wifi.RcvReply(rcv, 1000); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 117 | pc.printf("%s\r", rcv); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 118 | wait(1); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 119 | |
jamiedillion2019 | 10:e4b6bc6d9b07 | 120 | //Pushing the data acquired from HC-SR04 Ultrasonic Sensor to Cloud Server via API |
jamiedillion2019 | 10:e4b6bc6d9b07 | 121 | pc.printf("Product X - Sensor 1: \n\r"); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 122 | sprintf(snd,"\rhttps://api.thingspeak.com/update?api_key=O59NSRSQZCJ2G6WK&field1=%f\r", dist_percent1); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 123 | printf("Percent Stock X Remaining: %f\n\r", dist_percent1); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 124 | wifi.SendCMD(snd); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 125 | pc.printf("%s\r",snd); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 126 | wait(0.5); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 127 | wifi.RcvReply(rcv, 1000); |
jamiedillion2019 | 10:e4b6bc6d9b07 | 128 | pc.printf("%s\r", rcv); |
andcor02 | 8:25138f7b9309 | 129 | } |
andcor02 | 8:25138f7b9309 | 130 | } |