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: HC_SR04_Ultrasonic_Library mbed
Fork of Nucleo_UltrasonicHelloWorld by
main.cpp
00001 #include "mbed.h" 00002 #include "ultrasonic.h" 00003 00004 Serial pc(USBTX, USBRX); 00005 Serial esp(PC_10, PC_11); // tx, rx 00006 DigitalOut myled(LED1); 00007 00008 Timer t; 00009 00010 int count,ended,timeout; 00011 float globaldist; 00012 char buf[1024]; 00013 char sendbuf[255]; 00014 char snd[255]; 00015 00016 float dist0 = 11.0; // Abstand Ultraschallsensor zur vollen Oberfläche [cm] 00017 float iLeng = 147.0; // Länge des Wassertanks [cm] 00018 float iBrei = 67.0; // Breite des Wassertanks [cm] 00019 float iHoeh = 140.0; // Höhe des Wassertanks [cm] 00020 int maximumRange = 200; // Maximum Messbereich 200 cm 00021 int minimumRange = 0; // Minimum Messbereich 0 cm 00022 unsigned int numsamples = 0, avgsamples=99; // Mittelung über 100 Abstandsmessungen 00023 int avgsamplesarr[100]; 00024 00025 // Volumen Füllstand [l] des vollen Tanks 00026 float volmax = iLeng * iBrei * iHoeh / 1000.0; 00027 float volakt; // aktueller Füllstand [m3] 00028 00029 void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate(); 00030 float median(int n, int x[]); 00031 00032 void dist(int distance) 00033 { 00034 //put code here to happen when the distance is changed 00035 00036 if (numsamples < avgsamples) { 00037 avgsamplesarr[numsamples] = distance/10; 00038 printf("Distance changed to %dmm\r\n", distance); 00039 numsamples++; 00040 } 00041 00042 /* 00043 globaldist = distance/10; // convert distance from mm to cm 00044 printf("Distance changed to %dcm\r\n", globaldist); 00045 00046 if ((globaldist<(iHoeh+dist0)) && (globaldist >= dist0)) { 00047 // gemessene Entfernung ist kleiner als Abstand zum Boden des Tanks 00048 // und gemessene Entfernung ist größer als Überlauf 00049 volakt = (iHoeh + dist0 - globaldist) * iLeng * iBrei / 1000.0; 00050 } else { 00051 volakt = -1; 00052 } 00053 */ 00054 00055 } 00056 00057 ultrasonic mu(PC_0, PC_1, .1, 1, &dist); //Set the trigger pin to PC_0 and the echo pin to PC_1 00058 //have updates every .1 seconds and a timeout after 1 00059 //second, and call dist when the distance changes 00060 00061 int main() 00062 { 00063 pc.baud(115200); 00064 esp.baud(115200); // change this to the new ESP8266 baudrate if it is changed at any time. 00065 00066 while(1) 00067 { 00068 mu.startUpdates();//start mesuring the distance 00069 while(numsamples<avgsamples) 00070 { 00071 mu.checkDistance(); //call checkDistance() as much as possible, as this is where 00072 //the class checks if dist needs to be called. 00073 wait(0.01); 00074 } 00075 //mu.pauseUpdates(); //stop measure 00076 00077 globaldist = median(100,avgsamplesarr); 00078 pc.printf("Median ist %f\r\n",globaldist); 00079 00080 if ((globaldist<(iHoeh+dist0)) && (globaldist >= dist0)) { 00081 // gemessene Entfernung ist kleiner als Abstand zum Boden des Tanks 00082 // und gemessene Entfernung ist größer als Überlauf 00083 volakt = (iHoeh + dist0 - globaldist) * iLeng * iBrei / 1000.0; 00084 } else { 00085 volakt = -1; 00086 } 00087 00088 numsamples = 0; 00089 00090 strcpy(snd,"AT+CIPSTART=\"TCP\",\"192.168.3.60\",7072\r\n"); 00091 SendCMD(); 00092 timeout=2; 00093 getreply(); 00094 pc.printf(buf); 00095 00096 //AT+CIPSEND=<length>; 00097 //sprintf(buf,"setreading wassertank state %d\r\n",globaldist); 00098 sprintf(sendbuf,"setreading wassertank state D:%fcm V:%fm3\r\n\0",globaldist,volakt); 00099 00100 sprintf(snd,"AT+CIPSEND=%d\r\n",strlen(sendbuf)); 00101 //pc.printf(snd); 00102 SendCMD(); 00103 timeout=2; 00104 getreply(); 00105 pc.printf(buf); 00106 00107 strcpy(snd,sendbuf); 00108 pc.printf(snd); 00109 SendCMD(); 00110 timeout=2; 00111 getreply(); 00112 pc.printf(buf); 00113 00114 strcpy(snd,"AT+CIPCLOSE\r\n"); 00115 SendCMD(); 00116 timeout=2; 00117 getreply(); 00118 pc.printf(buf); 00119 00120 wait(10); 00121 } 00122 } 00123 00124 00125 void SendCMD() 00126 { 00127 esp.printf("%s", snd); 00128 } 00129 00130 void getreply() 00131 { 00132 memset(buf, '\0', sizeof(buf)); 00133 t.start(); 00134 ended=0;count=0; 00135 while(!ended) { 00136 if(esp.readable()) { 00137 buf[count] = esp.getc();count++; 00138 } 00139 if(t.read() > timeout) { 00140 ended = 1;t.stop();t.reset(); 00141 } 00142 } 00143 } 00144 00145 float median(int n, int x[]) { 00146 float temp; 00147 int i, j; 00148 // the following two loops sort the array x in ascending order 00149 for(i=0; i<n-1; i++) { 00150 for(j=i+1; j<n; j++) { 00151 if(x[j] < x[i]) { 00152 // swap elements 00153 temp = x[i]; 00154 x[i] = x[j]; 00155 x[j] = temp; 00156 } 00157 } 00158 } 00159 00160 if(n%2==0) { 00161 // if there is an even number of elements, return mean of the two elements in the middle 00162 return((x[n/2] + x[n/2 - 1]) / 2.0); 00163 } else { 00164 // else return the element in the middle 00165 return x[n/2]; 00166 } 00167 }
Generated on Tue Jul 26 2022 05:52:52 by
1.7.2
