Compare commits
No commits in common. "43d4b7d82d483ee647bb660672b582cdaf6d8f26" and "414123c87a68cdda2a6eb869a0eacd438ed500c2" have entirely different histories.
43d4b7d82d
...
414123c87a
@ -115,10 +115,9 @@ 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("Sufficient variation on " + desc + ": " + String(val).c_str());
|
Serial.println("New " + 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);
|
||||||
|
@ -18,21 +18,10 @@
|
|||||||
#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
|
||||||
@ -96,36 +85,29 @@ void reconnect() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool checkVariation(float newValue, float prevValue, float maxDiff) {
|
bool checkBound(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 &lastVal, float diff, bool force){
|
bool publish (String desc, char *topic, float val, float &val_r, int diff, bool force){
|
||||||
bool retval = false;
|
if (checkBound(val, val_r, 0.3) or force) {
|
||||||
Serial.println("Sample " + desc + ": " + String(val).c_str());
|
val_r = val;
|
||||||
if (checkVariation(val, lastVal, diff) or force) {
|
Serial.print("New " + desc + ": ");
|
||||||
lastVal = val;
|
Serial.println(String(val).c_str());
|
||||||
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);
|
||||||
retval = true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
Serial.print("** ERROR: Value out of bounds, not published. Last value :");
|
return false;
|
||||||
Serial.println(String(lastVal).c_str());
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return retval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
@ -143,14 +125,24 @@ void loop() {
|
|||||||
lastMsg = now;
|
lastMsg = now;
|
||||||
if (OPERATION_BLINK_EN) {digitalWrite(LED_BUILTIN, HIGH);}
|
if (OPERATION_BLINK_EN) {digitalWrite(LED_BUILTIN, HIGH);}
|
||||||
|
|
||||||
hmid = dht.readHumidity();
|
float h = dht.readHumidity();
|
||||||
temp = dht.readTemperature();
|
float t = dht.readTemperature();
|
||||||
|
|
||||||
if (!isnan(hmid) && !isnan(temp)){
|
if (isnan(h) || isnan(t)) {
|
||||||
abhu = (6.112 * ( pow(2.71828, ((17.67 * temp) / (temp + 243.5))) * hmid * 2.1674)) / (273.15 + temp);
|
Serial.println(F("Failed to read from DHT sensor!"));
|
||||||
heat = dht.computeHeatIndex(temp, hmid, false);
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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"));
|
||||||
@ -158,10 +150,9 @@ void loop() {
|
|||||||
lastSent = 0;
|
lastSent = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
published += publish("temperature", TOPIC_TEMP, temp, temp_r, DIFF_TEMP, forcePublish);
|
published += publish("temperature", TOPIC_TEMP, t, t_r, DIFF_TEMP, forcePublish);
|
||||||
published += publish("humidity", TOPIC_HMID, hmid, hmid_r, DIFF_HMID, forcePublish);
|
published += publish("humidity", TOPIC_HMID, h, h_r, DIFF_HMID, forcePublish);
|
||||||
published += publish("heat index", TOPIC_HEAT, heat, heat_r, DIFF_HEAT, forcePublish);
|
published += publish("heat index", TOPIC_HEAT, hi, hi_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;
|
||||||
|
Loading…
Reference in New Issue
Block a user