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
main.cpp
00001 /* 00002 Perimeter detection 00003 John Emberson/Tercio Junker 00004 00005 The purpose of this code is to rotate an HC-SR04 and measure the perimeter of a room. 00006 The measurements will then be sent to ThingSpeak to record the data. 00007 00008 */ 00009 00010 #include "mbed.h" 00011 #include "hcsr04.h" 00012 #include "ESP8266.h" 00013 #include "ULN2003.h" 00014 #include "TextLCD.h" 00015 00016 00017 00018 #define IP "184.106.153.149" 00019 #define spr 4096 00020 00021 #define SSID "IU PublicNet" 00022 #define PW "" 00023 00024 00025 Serial pc(USBTX,USBRX); //Serial Communication with PC 00026 ESP8266 wifi(PTC17, PTC16, 115200); //Tx Pin:PTC17; Rx Pin:PTC17; Baud rate:115200 00027 ULN2003 sm(D13,D12,D11,D10,spr); //SM motor (In1,In2,In3,In4,StepsPerRotation=4096) 00028 HCSR04 us(D7,D6); //Trig:D7 Echo:D6 00029 TextLCD lcd(D9,D8,D2,D3,D4,D5); //Rs,E,D4,D5,D6,D7 00030 00031 char snd[255]; //snd: send command to ESP8266 00032 char resp[1000]; //resp: receive response from ESP8266 00033 00034 int dist[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; 00035 00036 char* API = "Q34TJD14E68FMA7A"; //API key 00037 00038 char comm[300]; //WIFI command 00039 int timeout = 8000; //timeout for wifi commands 00040 00041 00042 int mp=16; //The amount of measurements per rotation 00043 int hmp=mp/2; //Half a rotation 00044 int delta=spr/mp; //setting up 16 points to take a measurment This is the amount of steps per measurement 00045 int speed=500; //speed in steps per second 00046 int delay=(delta/speed)/1000; //(steps % (steps per second))/1000 = milliseconds 00047 00048 void MotorFront(){ //Move motor forward half a rotation 00049 for (int i=1;i<(hmp+1);i++){ 00050 sm.moveForward(delta,speed); 00051 wait_ms(delay); 00052 us.start(); 00053 wait_ms(500); 00054 dist[i]=us.get_dist_cm(); 00055 pc.printf("dist%d=%d\r\n",i,dist[i]); 00056 } 00057 } 00058 void MotorBack(){ //Move motor reverse half a rotations 00059 for (int i=mp;i>(hmp);i--){ 00060 sm.moveForward(delta,speed); 00061 wait_ms(delay); 00062 us.start(); 00063 wait_ms(500); 00064 dist[i]=us.get_dist_cm(); 00065 pc.printf("dist%d=%d\r\n",i,dist[i]); 00066 } 00067 } 00068 00069 void ConnWIFI(){ //Connect to wifi 00070 wifi.SetMode(1); 00071 wifi.SendCMD(snd); 00072 wifi.RcvReply(resp, 5000); 00073 wait(1); 00074 pc.printf("%s\r", resp); 00075 wifi.Join(SSID,PW); 00076 wifi.RcvReply(resp, 5000); 00077 wait(1); 00078 pc.printf("%s\r\n", resp); 00079 pc.printf("Connecting to WIFI..."); 00080 lcd.cls(); 00081 lcd.printf("Connecting to WIFI"); 00082 wifi.setTransparent(); 00083 wait(1); 00084 pc.printf("..."); 00085 lcd.printf("..."); 00086 wifi.SetSingle(); 00087 wifi.RcvReply(resp, timeout); 00088 wait(1); 00089 } 00090 00091 void ConnTS1(){ //Connect to ThingSpeak 00092 pc.printf("\r\nConnecting to ThingSpeak\r\n"); 00093 lcd.cls(); 00094 lcd.printf("Connecting to ThingSpreak"); 00095 wifi.startTCPConn(IP,80); //cipstart 00096 wifi.RcvReply(resp, timeout); 00097 wait(1); 00098 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]); 00099 pc.printf("Sending data to ThingSpeak\r\n"); 00100 lcd.cls(); 00101 lcd.printf("Sending data to ThingSpeak"); 00102 wifi.sendURL(snd, comm); //cipsend and get command 00103 if (wifi.RcvReply(resp, timeout)) 00104 pc.printf("%s",resp); 00105 else 00106 pc.printf("No response while sending URL \r\n"); 00107 wait_ms(5000); 00108 } 00109 00110 void ConnTS2(){ //Connect to ThingSpeak for second back of data 00111 pc.printf("\r\nSending second batch of data..."); 00112 lcd.cls(); 00113 lcd.printf("Sending second batch of data"); 00114 wifi.startTCPConn(IP,80); //cipstart 00115 wifi.RcvReply(resp, timeout); 00116 wait(1); 00117 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]); 00118 pc.printf("...\r\n"); 00119 lcd.printf("..."); 00120 wifi.sendURL(snd, comm); //cipsend and get command 00121 if (wifi.RcvReply(resp, timeout)) 00122 pc.printf("%s",resp); 00123 else 00124 pc.printf("No response while sending URL \r\n"); 00125 } 00126 00127 00128 int main() 00129 { 00130 pc.baud(115200); //Baud Rate of 115200 for Tera Term 00131 pc.printf("Scanning measurments of room"); 00132 lcd.cls(); //Clear LCD Screen 00133 lcd.printf("Scanning measurments of room"); 00134 wait_ms(2000); 00135 MotorFront(); //Scan first 8 steps 00136 sm.moveReverse(spr,600); //Rotates camera opposite to prevent wire tangle 00137 wait_ms(250); 00138 MotorBack(); //Scan last 8 steps 00139 ConnWIFI(); //Connecting to WIFI 00140 ConnTS1(); //Sending first 8 steps to ThingSpeak 00141 ConnTS2(); //Sending last 8 steps to ThingSpeak 00142 pc.printf("Done"); 00143 lcd.cls(); 00144 lcd.printf("Done"); 00145 }
Generated on Tue Oct 10 2023 04:06:24 by
1.7.2