
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.
Dependencies: mbed mbed-http TextLCD ESP8266 ULN2003_StepperDriver
main.cpp@12:701ea1b1d513, 2021-12-13 (annotated)
- Committer:
- 27e08e1e-4785-4bb4-b751-624c9c99ee9f
- Date:
- Mon Dec 13 15:14:02 2021 +0000
- Revision:
- 12:701ea1b1d513
- Parent:
- 11:756321f0b0cd
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.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 1 | /* |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 2 | Perimeter detection |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 3 | John Emberson/Tercio Junker |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 4 | |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 5 | The purpose of this code is to rotate an HC-SR04 and measure the perimeter of a room. |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 6 | The measurements will then be sent to ThingSpeak to record the data. |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 7 | |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 8 | */ |
jamiedillion2019 | 10:e4b6bc6d9b07 | 9 | |
andcor02 | 8:25138f7b9309 | 10 | #include "mbed.h" |
andcor02 | 8:25138f7b9309 | 11 | #include "hcsr04.h" |
jamiedillion2019 | 10:e4b6bc6d9b07 | 12 | #include "ESP8266.h" |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 13 | #include "ULN2003.h" |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 14 | #include "TextLCD.h" |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 15 | |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 16 | |
andcor02 | 8:25138f7b9309 | 17 | |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 18 | #define IP "184.106.153.149" |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 19 | #define spr 4096 |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 20 | |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 21 | #define SSID "IU PublicNet" |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 22 | #define PW "" |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 23 | |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 24 | |
jamiedillion2019 | 10:e4b6bc6d9b07 | 25 | Serial pc(USBTX,USBRX); //Serial Communication with PC |
jamiedillion2019 | 10:e4b6bc6d9b07 | 26 | ESP8266 wifi(PTC17, PTC16, 115200); //Tx Pin:PTC17; Rx Pin:PTC17; Baud rate:115200 |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 27 | ULN2003 sm(D13,D12,D11,D10,spr); //SM motor (In1,In2,In3,In4,StepsPerRotation=4096) |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 28 | HCSR04 us(D7,D6); //Trig:D7 Echo:D6 |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 29 | TextLCD lcd(D9,D8,D2,D3,D4,D5); //Rs,E,D4,D5,D6,D7 |
jamiedillion2019 | 10:e4b6bc6d9b07 | 30 | |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 31 | char snd[255]; //snd: send command to ESP8266 |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 32 | char resp[1000]; //resp: receive response from ESP8266 |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 33 | |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 34 | int dist[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 35 | |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 36 | char* API = "Q34TJD14E68FMA7A"; //API key |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 37 | |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 38 | char comm[300]; //WIFI command |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 39 | int timeout = 8000; //timeout for wifi commands |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 40 | |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 41 | |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 42 | int mp=16; //The amount of measurements per rotation |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 43 | int hmp=mp/2; //Half a rotation |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 44 | int delta=spr/mp; //setting up 16 points to take a measurment This is the amount of steps per measurement |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 45 | int speed=500; //speed in steps per second |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 46 | int delay=(delta/speed)/1000; //(steps % (steps per second))/1000 = milliseconds |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 47 | |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 48 | void MotorFront(){ //Move motor forward half a rotation |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 49 | for (int i=1;i<(hmp+1);i++){ |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 50 | sm.moveForward(delta,speed); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 51 | wait_ms(delay); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 52 | us.start(); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 53 | wait_ms(500); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 54 | dist[i]=us.get_dist_cm(); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 55 | pc.printf("dist%d=%d\r\n",i,dist[i]); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 56 | } |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 57 | } |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 58 | void MotorBack(){ //Move motor reverse half a rotations |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 59 | for (int i=mp;i>(hmp);i--){ |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 60 | sm.moveForward(delta,speed); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 61 | wait_ms(delay); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 62 | us.start(); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 63 | wait_ms(500); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 64 | dist[i]=us.get_dist_cm(); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 65 | pc.printf("dist%d=%d\r\n",i,dist[i]); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 66 | } |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 67 | } |
jamiedillion2019 | 10:e4b6bc6d9b07 | 68 | |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 69 | void ConnWIFI(){ //Connect to wifi |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 70 | wifi.SetMode(1); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 71 | wifi.SendCMD(snd); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 72 | wifi.RcvReply(resp, 5000); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 73 | wait(1); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 74 | pc.printf("%s\r", resp); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 75 | wifi.Join(SSID,PW); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 76 | wifi.RcvReply(resp, 5000); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 77 | wait(1); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 78 | pc.printf("%s\r\n", resp); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 79 | pc.printf("Connecting to WIFI..."); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 80 | lcd.cls(); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 81 | lcd.printf("Connecting to WIFI"); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 82 | wifi.setTransparent(); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 83 | wait(1); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 84 | pc.printf("..."); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 85 | lcd.printf("..."); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 86 | wifi.SetSingle(); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 87 | wifi.RcvReply(resp, timeout); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 88 | wait(1); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 89 | } |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 90 | |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 91 | void ConnTS1(){ //Connect to ThingSpeak |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 92 | pc.printf("\r\nConnecting to ThingSpeak\r\n"); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 93 | lcd.cls(); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 94 | lcd.printf("Connecting to ThingSpreak"); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 95 | wifi.startTCPConn(IP,80); //cipstart |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 96 | wifi.RcvReply(resp, timeout); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 97 | wait(1); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 98 | 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]); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 99 | pc.printf("Sending data to ThingSpeak\r\n"); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 100 | lcd.cls(); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 101 | lcd.printf("Sending data to ThingSpeak"); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 102 | wifi.sendURL(snd, comm); //cipsend and get command |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 103 | if (wifi.RcvReply(resp, timeout)) |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 104 | pc.printf("%s",resp); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 105 | else |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 106 | pc.printf("No response while sending URL \r\n"); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 107 | wait_ms(5000); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 108 | } |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 109 | |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 110 | void ConnTS2(){ //Connect to ThingSpeak for second back of data |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 111 | pc.printf("\r\nSending second batch of data..."); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 112 | lcd.cls(); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 113 | lcd.printf("Sending second batch of data"); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 114 | wifi.startTCPConn(IP,80); //cipstart |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 115 | wifi.RcvReply(resp, timeout); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 116 | wait(1); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 117 | 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]); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 118 | pc.printf("...\r\n"); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 119 | lcd.printf("..."); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 120 | wifi.sendURL(snd, comm); //cipsend and get command |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 121 | if (wifi.RcvReply(resp, timeout)) |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 122 | pc.printf("%s",resp); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 123 | else |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 124 | pc.printf("No response while sending URL \r\n"); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 125 | } |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 126 | |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 127 | |
andcor02 | 8:25138f7b9309 | 128 | int main() |
andcor02 | 8:25138f7b9309 | 129 | { |
jamiedillion2019 | 10:e4b6bc6d9b07 | 130 | pc.baud(115200); //Baud Rate of 115200 for Tera Term |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 131 | pc.printf("Scanning measurments of room"); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 132 | lcd.cls(); //Clear LCD Screen |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 133 | lcd.printf("Scanning measurments of room"); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 134 | wait_ms(2000); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 135 | MotorFront(); //Scan first 8 steps |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 136 | sm.moveReverse(spr,600); //Rotates camera opposite to prevent wire tangle |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 137 | wait_ms(250); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 138 | MotorBack(); //Scan last 8 steps |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 139 | ConnWIFI(); //Connecting to WIFI |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 140 | ConnTS1(); //Sending first 8 steps to ThingSpeak |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 141 | ConnTS2(); //Sending last 8 steps to ThingSpeak |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 142 | pc.printf("Done"); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 143 | lcd.cls(); |
27e08e1e-4785-4bb4-b751-624c9c99ee9f | 12:701ea1b1d513 | 144 | lcd.printf("Done"); |
andcor02 | 8:25138f7b9309 | 145 | } |