Compare commits

..

2 Commits

Author SHA1 Message Date
bzi
43d4b7d82d Merge absolute humidity and plausibility checks into espsens 2019-03-01 09:15:17 +01:00
bzi
2f4ae597f5 Improve debug output in envisens 2019-03-01 09:14:41 +01:00
2 changed files with 40 additions and 30 deletions

View File

@ -115,9 +115,10 @@ bool forcePublish = false;
bool publish (String desc, char *topic, float val, float &lastVal, float diff, bool force){ bool publish (String desc, char *topic, float val, float &lastVal, float diff, bool force){
bool retval = false; bool retval = false;
Serial.println("Sample " + desc + ": " + String(val).c_str());
if (checkVariation(val, lastVal, diff) or force) { if (checkVariation(val, lastVal, diff) or force) {
lastVal = val; lastVal = val;
Serial.println("New " + desc + ": " + String(val).c_str()); Serial.println("Sufficient variation on " + desc + ": " + String(val).c_str());
if (checkPlausibility(val, lastVal, diff) or force) { if (checkPlausibility(val, lastVal, diff) or force) {
Serial.println("Value published"); Serial.println("Value published");
client.publish(topic, String(val).c_str(), true); client.publish(topic, String(val).c_str(), true);

View File

@ -18,10 +18,21 @@
#define TOPIC_TEMP TOPIC_BASE "temperature" #define TOPIC_TEMP TOPIC_BASE "temperature"
#define TOPIC_HMID TOPIC_BASE "humidity" #define TOPIC_HMID TOPIC_BASE "humidity"
#define TOPIC_HEAT TOPIC_BASE "heat_index" #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_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_HMID 2.5 // humidity doesn't change that much anyways
#define DIFF_HEAT 0.2 // this one's actually quite active #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 DHTPIN 4 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
@ -85,30 +96,37 @@ void reconnect() {
} }
} }
bool checkBound(float newValue, float prevValue, float maxDiff) { bool checkVariation(float newValue, float prevValue, float maxDiff) {
return !isnan(newValue) && return !isnan(newValue) &&
(newValue < prevValue - maxDiff || newValue > prevValue + maxDiff); (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 lastMsg = 0;
float t_r = 0.0;
float h_r = 0.0;
float hi_r = 0.0;
long lastSent = 0; long lastSent = 0;
bool published = false; bool published = false;
bool forcePublish = false; bool forcePublish = false;
bool publish (String desc, char *topic, float val, float &val_r, int diff, bool force){ bool publish (String desc, char *topic, float val, float &lastVal, float diff, bool force){
if (checkBound(val, val_r, 0.3) or force) { bool retval = false;
val_r = val; Serial.println("Sample " + desc + ": " + String(val).c_str());
Serial.print("New " + desc + ": "); if (checkVariation(val, lastVal, diff) or force) {
Serial.println(String(val).c_str()); 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); client.publish(topic, String(val).c_str(), true);
return true; retval = true;
} else { } else {
return false; Serial.print("** ERROR: Value out of bounds, not published. Last value :");
Serial.println(String(lastVal).c_str());
} }
} }
return retval;
}
void loop() { void loop() {
if (!client.connected()) { if (!client.connected()) {
@ -125,24 +143,14 @@ void loop() {
lastMsg = now; lastMsg = now;
if (OPERATION_BLINK_EN) {digitalWrite(LED_BUILTIN, HIGH);} if (OPERATION_BLINK_EN) {digitalWrite(LED_BUILTIN, HIGH);}
float h = dht.readHumidity(); hmid = dht.readHumidity();
float t = dht.readTemperature(); temp = dht.readTemperature();
if (isnan(h) || isnan(t)) { if (!isnan(hmid) && !isnan(temp)){
Serial.println(F("Failed to read from DHT sensor!")); abhu = (6.112 * ( pow(2.71828, ((17.67 * temp) / (temp + 243.5))) * hmid * 2.1674)) / (273.15 + temp);
return; 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; published = false;
if (lastSent > FORCE_PERIOD) { if (lastSent > FORCE_PERIOD) {
Serial.println(F("Forcing a publish of all values")); Serial.println(F("Forcing a publish of all values"));
@ -150,9 +158,10 @@ void loop() {
lastSent = 0; lastSent = 0;
} }
published += publish("temperature", TOPIC_TEMP, t, t_r, DIFF_TEMP, forcePublish); published += publish("temperature", TOPIC_TEMP, temp, temp_r, DIFF_TEMP, forcePublish);
published += publish("humidity", TOPIC_HMID, h, h_r, DIFF_HMID, forcePublish); published += publish("humidity", TOPIC_HMID, hmid, hmid_r, DIFF_HMID, forcePublish);
published += publish("heat index", TOPIC_HEAT, hi, hi_r, DIFF_HEAT, 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) { if (published == false) {
lastSent = lastSent + 1; lastSent = lastSent + 1;