Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed mbed-http TextLCD ESP8266 ULN2003_StepperDriver
Revision 12:701ea1b1d513, committed 2021-12-13
- Comitter:
- 27e08e1e-4785-4bb4-b751-624c9c99ee9f
- Date:
- Mon Dec 13 15:14:02 2021 +0000
- Parent:
- 11:756321f0b0cd
- Commit message:
- The purpose of this code is to rotate an HC-SR04 and measure the perimeter of a room.; The measurements will then be sent to ThingSpeak to record the data.
Changed in this revision
diff -r 756321f0b0cd -r 701ea1b1d513 TextLCD.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextLCD.lib Mon Dec 13 15:14:02 2021 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/simon/code/TextLCD/#308d188a2d3a
diff -r 756321f0b0cd -r 701ea1b1d513 ULN2003_StepperDriver.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ULN2003_StepperDriver.lib Mon Dec 13 15:14:02 2021 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/27e08e1e-4785-4bb4-b751-624c9c99ee9f/code/ULN2003_StepperDriver/#28a05cf07dee
diff -r 756321f0b0cd -r 701ea1b1d513 main.cpp
--- a/main.cpp Sat Aug 28 22:17:23 2021 +0000
+++ b/main.cpp Mon Dec 13 15:14:02 2021 +0000
@@ -1,131 +1,145 @@
-/**
-Electronically Controlled Intelligent Shelves
-Developed by: Priyank Kalgaonkar
-**/
+/*
+Perimeter detection
+John Emberson/Tercio Junker
+
+The purpose of this code is to rotate an HC-SR04 and measure the perimeter of a room.
+The measurements will then be sent to ThingSpeak to record the data.
+
+*/
#include "mbed.h"
#include "hcsr04.h"
#include "ESP8266.h"
-#include "math.h"
-#define CloudIP "184.106.153.149" //Raw IP Address of ThingSpeak Cloud Server
+#include "ULN2003.h"
+#include "TextLCD.h"
+
+
-DigitalOut RLed(LED1); //Onboard Red LED = Shelf Out of Stock
-DigitalOut GLed(LED2); //Onboard Green LED = All OK
-DigitalOut BLed(LED3); //Onboard Blue LED for Wifi Tx Indication
-HCSR04 usensor1(D8,D9); //ECHO Pin=D9, TRIG Pin=D8
-HCSR04 usensor2(D7,D6); //ECHO Pin=D7, TRIG Pin=D6
+#define IP "184.106.153.149"
+#define spr 4096
+
+#define SSID "IU PublicNet"
+#define PW ""
+
+
Serial pc(USBTX,USBRX); //Serial Communication with PC
ESP8266 wifi(PTC17, PTC16, 115200); //Tx Pin:PTC17; Rx Pin:PTC17; Baud rate:115200
+ULN2003 sm(D13,D12,D11,D10,spr); //SM motor (In1,In2,In3,In4,StepsPerRotation=4096)
+HCSR04 us(D7,D6); //Trig:D7 Echo:D6
+TextLCD lcd(D9,D8,D2,D3,D4,D5); //Rs,E,D4,D5,D6,D7
-void wifi_send(void);; //Connect and Push Data Channel to Cloud Server
+char snd[255]; //snd: send command to ESP8266
+char resp[1000]; //resp: receive response from ESP8266
+
+int dist[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
+
+char* API = "Q34TJD14E68FMA7A"; //API key
+
+char comm[300]; //WIFI command
+int timeout = 8000; //timeout for wifi commands
+
+
+int mp=16; //The amount of measurements per rotation
+int hmp=mp/2; //Half a rotation
+int delta=spr/mp; //setting up 16 points to take a measurment This is the amount of steps per measurement
+int speed=500; //speed in steps per second
+int delay=(delta/speed)/1000; //(steps % (steps per second))/1000 = milliseconds
+
+void MotorFront(){ //Move motor forward half a rotation
+ for (int i=1;i<(hmp+1);i++){
+ sm.moveForward(delta,speed);
+ wait_ms(delay);
+ us.start();
+ wait_ms(500);
+ dist[i]=us.get_dist_cm();
+ pc.printf("dist%d=%d\r\n",i,dist[i]);
+ }
+ }
+ void MotorBack(){ //Move motor reverse half a rotations
+ for (int i=mp;i>(hmp);i--){
+ sm.moveForward(delta,speed);
+ wait_ms(delay);
+ us.start();
+ wait_ms(500);
+ dist[i]=us.get_dist_cm();
+ pc.printf("dist%d=%d\r\n",i,dist[i]);
+ }
+ }
-int num = 0;
-int distance1, distance2;
-float dist_remaining1, dist_percent1, dist_remaining2, dist_percent2;
-char snd[255],rcv[1000]; //snd: send command to ESP8266
- //rcv: receive response from ESP8266
-
+void ConnWIFI(){ //Connect to wifi
+ wifi.SetMode(1);
+ wifi.SendCMD(snd);
+ wifi.RcvReply(resp, 5000);
+ wait(1);
+ pc.printf("%s\r", resp);
+ wifi.Join(SSID,PW);
+ wifi.RcvReply(resp, 5000);
+ wait(1);
+ pc.printf("%s\r\n", resp);
+ pc.printf("Connecting to WIFI...");
+ lcd.cls();
+ lcd.printf("Connecting to WIFI");
+ wifi.setTransparent();
+ wait(1);
+ pc.printf("...");
+ lcd.printf("...");
+ wifi.SetSingle();
+ wifi.RcvReply(resp, timeout);
+ wait(1);
+ }
+
+void ConnTS1(){ //Connect to ThingSpeak
+ pc.printf("\r\nConnecting to ThingSpeak\r\n");
+ lcd.cls();
+ lcd.printf("Connecting to ThingSpreak");
+ wifi.startTCPConn(IP,80); //cipstart
+ wifi.RcvReply(resp, timeout);
+ wait(1);
+ sprintf(snd,"https://api.thingspeak.com/update?api_key=%s&field1=%d&field2=%d&field3=%d&field4=%d&field5=%d&field6=%d&field7=%d&field8=%d\r\n",API,dist[1],dist[2],dist[3],dist[4],dist[5],dist[6],dist[7],dist[8]);
+ pc.printf("Sending data to ThingSpeak\r\n");
+ lcd.cls();
+ lcd.printf("Sending data to ThingSpeak");
+ wifi.sendURL(snd, comm); //cipsend and get command
+ if (wifi.RcvReply(resp, timeout))
+ pc.printf("%s",resp);
+ else
+ pc.printf("No response while sending URL \r\n");
+ wait_ms(5000);
+ }
+
+void ConnTS2(){ //Connect to ThingSpeak for second back of data
+ pc.printf("\r\nSending second batch of data...");
+ lcd.cls();
+ lcd.printf("Sending second batch of data");
+ wifi.startTCPConn(IP,80); //cipstart
+ wifi.RcvReply(resp, timeout);
+ wait(1);
+ sprintf(snd,"https://api.thingspeak.com/update?api_key=%s&field1=%d&field2=%d&field3=%d&field4=%d&field5=%d&field6=%d&field7=%d&field8=%d\r\n",API,dist[9],dist[10],dist[11],dist[12],dist[13],dist[14],dist[15],dist[16]);
+ pc.printf("...\r\n");
+ lcd.printf("...");
+ wifi.sendURL(snd, comm); //cipsend and get command
+ if (wifi.RcvReply(resp, timeout))
+ pc.printf("%s",resp);
+ else
+ pc.printf("No response while sending URL \r\n");
+}
+
+
int main()
{
pc.baud(115200); //Baud Rate of 115200 for Tera Term
-
- pc.printf("######## ###### #### ###### ###### ## ## ###### ######## ######## ## ##\n\r");
- pc.printf("## ## ## ## ## ## ## ## ## ## ## ## ## ## ### ###\n\r");
- pc.printf("## ## ## ## ## #### ## ## ## #### ####\n\r");
- pc.printf("###### ## ## ###### ###### ## ###### ## ###### ## ### ##\n\r");
- pc.printf("## ## ## ## ## ## ## ## ## ## ##\n\r");
- pc.printf("## ## ## ## ## ## ## ## ## ## ## ## ## ## ##\n\r");
- pc.printf("######## ###### #### ###### ###### ## ###### ## ######## ## ##\n\r");
- pc.printf("-----------------------------------------------------------------------------------------\n\r");
- pc.printf("Developed By: Priyank Kalgaonkar\n\r");
- pc.printf("-----------------------------------------------------------------------------------------\n\r\n\r");
-
- pc.printf("Initial Setup\r\n");
- wifi.SetMode(1); //Set ESP mode to 1
- wifi.RcvReply(rcv, 1000); //Receive a response from ESP
- pc.printf("%s\r", rcv);
-
- pc.printf("Connecting to WiFi\r\n"); //AP Setup Initialization
- wifi.Join("Z", "12345678");//Put your Wifi SSID followed by Password WiFi_SSID WiFi_Password
- wifi.RcvReply(rcv, 1000);
- pc.printf("%s\n", rcv);
- wait(8);
-
- wifi.GetIP(rcv); //Obtains an IP address from the AP
-
- while (1)
- {
- wifi_send();
-
- RLed = 1;
- GLed = 1;
- BLed = 0;
- wait(2.0f);
- }
-}
-
-void wifi_send(void)
-{
- while(num<1000000000000)
- {
- num=num+1;
- pc.printf("\nCloud Sync Instance #: %d\n\r", num);
- pc.printf("Syncing Data with Cloud, Please Wait.\n\r");
-
- //Ultrasound Sensor (HC-SR04) #1 Initialization
- int a = 30;
- usensor1.start();
- wait_ms(500);
-
- //Calculating Distance Percentage Remaining for Sensor # 1
- distance1 = usensor1.get_dist_cm();
- dist_remaining1 = a-distance1;
- dist_percent1 = (dist_remaining1/30)*100;
-
- //LED and Tera Term Output
- if (distance1<30 && distance2<30) {
- RLed = 1;
- BLed = 1;
- GLed = 0;
- //printf("Percent remaining: %f\r", dist_percent1 && dist_percent2);
- } else {
- GLed = 1;
- BLed = 1;
- RLed = 0;
- printf("Shelves Empty! Replenish Stock.\n\r");
- }
-
- //Sending Data to the Cloud Server via ESP8266 WiFi Module
- strcpy(snd,"AT+CIPMUX=0\n\r"); //AT+CIPMUX: Enabling Single Channel Mode
- wifi.SendCMD(snd);
- wait(1);
- wifi.RcvReply(rcv, 1000);
- wait(1);
-
- //Establish TCP connection w/ Cloud Server
- sprintf(snd,"AT+CIPSTART=4,\"TCP\",\"%s\",80\n",CloudIP);
- wait(1);
- wifi.RcvReply(rcv, 1000);
- wait(1);
-
- //Set length of the data that will be sent
- strcpy(snd,"AT+CIPSEND=100\n\r");
- wifi.SendCMD(snd);
- pc.printf("%s\r", rcv);
- wait(1);
- wifi.RcvReply(rcv, 1000);
- pc.printf("%s\r", rcv);
- wait(1);
-
- //Pushing the data acquired from HC-SR04 Ultrasonic Sensor to Cloud Server via API
- //Replace with your own API Request - Write a Channel Feed below
- pc.printf("Product X - Sensor 1: \n\r");
- sprintf(snd,"\r ***Replace with your own API Request - Write a Channel Feed below*** \r", dist_percent1);
- printf("Percent Stock X Remaining: %f\n\r", dist_percent1);
- wifi.SendCMD(snd);
- pc.printf("%s\r",snd);
- wait(0.5);
- wifi.RcvReply(rcv, 1000);
- pc.printf("%s\r", rcv);
- }
+ pc.printf("Scanning measurments of room");
+ lcd.cls(); //Clear LCD Screen
+ lcd.printf("Scanning measurments of room");
+ wait_ms(2000);
+ MotorFront(); //Scan first 8 steps
+ sm.moveReverse(spr,600); //Rotates camera opposite to prevent wire tangle
+ wait_ms(250);
+ MotorBack(); //Scan last 8 steps
+ ConnWIFI(); //Connecting to WIFI
+ ConnTS1(); //Sending first 8 steps to ThingSpeak
+ ConnTS2(); //Sending last 8 steps to ThingSpeak
+ pc.printf("Done");
+ lcd.cls();
+ lcd.printf("Done");
}
\ No newline at end of file