From 7208a6d32435cf05758e0a08c733f7bc44eda90a Mon Sep 17 00:00:00 2001 From: bzi Date: Thu, 28 Feb 2019 20:42:39 +0100 Subject: [PATCH] Implement plausibility check for values before publishing --- envisens/envisens_esp_fw/envisens_esp_fw.ino | 24 +++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/envisens/envisens_esp_fw/envisens_esp_fw.ino b/envisens/envisens_esp_fw/envisens_esp_fw.ino index f253046..8d26b8a 100644 --- a/envisens/envisens_esp_fw/envisens_esp_fw.ino +++ b/envisens/envisens_esp_fw/envisens_esp_fw.ino @@ -22,7 +22,7 @@ #define DIFF_TEMP 0.3 // sensor has only 0.5°C accuracy, triggering on less is ludicrous #define DIFF_HMID 2.5 // humidity doesn't change that much anyways #define DIFF_HEAT 0.2 // this one's actually quite active -#define DIFF_AIRQ 5 +#define DIFF_AIRQ 3 // this one hardly changes float hmid = 0; // humidity float hmid_r = 0; // humidity @@ -95,26 +95,34 @@ void reconnect() { } } -bool checkBound(float newValue, float prevValue, float maxDiff) { +bool checkVariation(float newValue, float prevValue, float maxDiff) { return !isnan(newValue) && (newValue < prevValue - maxDiff || newValue > prevValue + maxDiff); } +bool checkPlausibility(float newValue, float prevValue, float maxDiff) { + return (newValue < prevValue + (maxDiff * 10) || newValue > prevValue - (maxDiff * 10)); +} + long lastMsg = 0; long lastSent = 0; bool published = false; bool forcePublish = false; bool publish (String desc, char *topic, float val, float &val_r, int diff, bool force){ - if (checkBound(val, val_r, 0.3) or force) { - val_r = val; - Serial.print("New " + desc + ": "); - Serial.println(String(val).c_str()); + bool retval = false; + if (checkVariation(val, val_r, diff) or force) { + val_r = val; + Serial.print("New " + desc + ": "); + Serial.println(String(val).c_str()); + if (checkPlausibility(val, val_r, diff) or force) { client.publish(topic, String(val).c_str(), true); - return true; + retval = true; } else { - return false; + Serial.println("** ERROR: Value out of bounds, not published **"); } + } + return retval; } void loop() {