function to compare floats, should fix #3

This commit is contained in:
Ronald Schaten 2016-02-23 23:31:29 +01:00
parent 6813ec03b9
commit be6944d831

View File

@ -52,6 +52,10 @@ void configModeCallback (WiFiManager *myWiFiManager) {
ticker.attach(0.1, toggle_led); ticker.attach(0.1, toggle_led);
} }
bool isEqual(float a, float b, float epsilon=0.001) {
return fabs(a - b) <= epsilon * fabs(a);
}
void read_sensor() { void read_sensor() {
// wait at least 2 seconds seconds between measurements // wait at least 2 seconds seconds between measurements
unsigned long currentMillis = millis(); unsigned long currentMillis = millis();
@ -78,13 +82,13 @@ void read_sensor() {
dtostrf(temperature, 1, 2, str_temperature); dtostrf(temperature, 1, 2, str_temperature);
dtostrf(heatindex, 1, 2, str_heatindex); dtostrf(heatindex, 1, 2, str_heatindex);
if (humidity != previousHumidity) { if (!isEqual(humidity, previousHumidity)) {
client.publish("nodemcu/humidity", str_humidity); client.publish("nodemcu/humidity", str_humidity);
} }
if (temperature != previousTemperature) { if (!isEqual(temperature, previousTemperature)) {
client.publish("nodemcu/temperature", str_temperature); client.publish("nodemcu/temperature", str_temperature);
} }
if (heatindex != previousHeatindex) { if (!isEqual(heatindex, previousHeatindex)) {
client.publish("nodemcu/heatindex", str_heatindex); client.publish("nodemcu/heatindex", str_heatindex);
} }