Merge absolute humidity and plausibility checks into espsens

This commit is contained in:
bzi 2019-03-01 09:15:17 +01:00
parent 2f4ae597f5
commit 43d4b7d82d

View File

@ -18,10 +18,21 @@
#define TOPIC_TEMP TOPIC_BASE "temperature"
#define TOPIC_HMID TOPIC_BASE "humidity"
#define TOPIC_HEAT TOPIC_BASE "heat_index"
#define TOPIC_ABHU TOPIC_BASE "abs_humidity"
#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_ABHU 0.1 // small changes mean quite something
float hmid = 0; // humidity
float hmid_r = 0; // humidity
float abhu = 0; // absolute humidity
float abhu_r = 0; // absolute humidity
float temp = 0; // temperature
float temp_r = 0; // temperature
float heat = 0; // heat index
float heat_r = 0; // heat index
#define DHTPIN 4 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
@ -85,29 +96,36 @@ 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;
float t_r = 0.0;
float h_r = 0.0;
float hi_r = 0.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 publish (String desc, char *topic, float val, float &lastVal, float diff, bool force){
bool retval = false;
Serial.println("Sample " + desc + ": " + String(val).c_str());
if (checkVariation(val, lastVal, diff) or force) {
lastVal = val;
Serial.println("Sufficient variation on " + desc + ": " + String(val).c_str());
if (checkPlausibility(val, lastVal, diff) or force) {
Serial.println("Value published");
client.publish(topic, String(val).c_str(), true);
return true;
retval = true;
} else {
return false;
Serial.print("** ERROR: Value out of bounds, not published. Last value :");
Serial.println(String(lastVal).c_str());
}
}
return retval;
}
void loop() {
@ -125,24 +143,14 @@ void loop() {
lastMsg = now;
if (OPERATION_BLINK_EN) {digitalWrite(LED_BUILTIN, HIGH);}
float h = dht.readHumidity();
float t = dht.readTemperature();
hmid = dht.readHumidity();
temp = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
if (!isnan(hmid) && !isnan(temp)){
abhu = (6.112 * ( pow(2.71828, ((17.67 * temp) / (temp + 243.5))) * hmid * 2.1674)) / (273.15 + temp);
heat = dht.computeHeatIndex(temp, hmid, false);
}
float hi = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C Heat index: "));
Serial.print(hi);
Serial.println(F("°C "));
published = false;
if (lastSent > FORCE_PERIOD) {
Serial.println(F("Forcing a publish of all values"));
@ -150,9 +158,10 @@ void loop() {
lastSent = 0;
}
published += publish("temperature", TOPIC_TEMP, t, t_r, DIFF_TEMP, forcePublish);
published += publish("humidity", TOPIC_HMID, h, h_r, DIFF_HMID, forcePublish);
published += publish("heat index", TOPIC_HEAT, hi, hi_r, DIFF_HEAT, forcePublish);
published += publish("temperature", TOPIC_TEMP, temp, temp_r, DIFF_TEMP, forcePublish);
published += publish("humidity", TOPIC_HMID, hmid, hmid_r, DIFF_HMID, forcePublish);
published += publish("heat index", TOPIC_HEAT, heat, heat_r, DIFF_HEAT, forcePublish);
published += publish("absolute humidity", TOPIC_ABHU, abhu, abhu_r, DIFF_ABHU, forcePublish);
if (published == false) {
lastSent = lastSent + 1;