Hello
i wrote a script, that meassure all 500ms the distance via a HR-S04.
I got sometimes a wrong distance. So i want to meassure the distance all 200ms and calculate the sum all 800ms (that will be 4 measurements).
how can i create a global variable, to safe the value and how can i send only the measurement?
here is my current code
void loop() {
//HC-SR04#####################################################
//###########################################################
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
// Convert the time into a distance
cm = (duration/2) / 29.1;
distance = duration/58;
if (distance > SENSOR_MAX_RANGE || distance <= 0){
cm = 900;
Serial.println("out of range: " + String(cm) + " cm");
} else {
Serial.println("Distance to object: " + String(cm) + " cm");
}
Serial.println(cm);
//HC-SR04 ENDE ################################################
//############################################################
// SENDE HC-SR04 HTTP#####################################################
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
String url = "http://10.0.0.142/terminal/includes/helpers/HC-SR04.php";
String ultraschall_url_part = "?cm=";
String serverurl = url + ultraschall_url_part + cm;
Serial.println(serverurl);
// Your Domain name with URL path or IP address with path
http.begin(client, serverurl);
// Send HTTP GET request
int httpResponseCode = http.GET();
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
//SENDE Temp_innen und Luftdruck ENDE##############################################################
// Gib dem ESP8266 Zeit, die Nachricht zu senden, bevor er in den Deep-Sleep-Modus geht
delay(500);
}//Void loop Ende