Implement Sensor struct for better data handling and force publication
This commit is contained in:
parent
724af2a3db
commit
828877e0fb
@ -17,22 +17,27 @@
|
||||
#define TOPIC_BASE "sensor/" SENSOR_ID "/"
|
||||
#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 TOPIC_HEAT TOPIC_BASE "heat_index"
|
||||
|
||||
#define DIFF_TEMP 0.3 // sensor has only 0.5°C accuracy, triggering on less is ludicrous
|
||||
#define DIFF_HMID 1.5 // humidity doesn't change that much anyways
|
||||
#define DIFF_HEAT 0.3 // this one's actually quite active
|
||||
#define DIFF_ABHU 0.2 // small changes mean quite something
|
||||
#define DIFF_HEAT 0.3 // this one's actually quite active
|
||||
|
||||
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
|
||||
struct Sensor {
|
||||
String desc; // sensor description
|
||||
char* topic; // sensor topic
|
||||
float diff; // value difference for an update
|
||||
float value; // current values
|
||||
float lastValue; // value at last update
|
||||
int lastUpdate; // number of samples since last update
|
||||
};
|
||||
|
||||
Sensor temp = {"temperature (°C)", TOPIC_TEMP, DIFF_TEMP, 0.0, 0.0, -1}; // temperature
|
||||
Sensor hmid = {"humidity (%)", TOPIC_HMID, DIFF_HMID, 0.0, 0.0, -1}; // humidity
|
||||
Sensor abhu = {"absolute humidity (gr/m³)", TOPIC_ABHU, DIFF_ABHU, 0.0, 0.0, -1}; // absolute humidity
|
||||
Sensor heat = {"heat index (°C)", TOPIC_HEAT, DIFF_HEAT, 0.0, 0.0, -1}; // heat index
|
||||
|
||||
#define DHTPIN 4 // Digital pin connected to the DHT sensor
|
||||
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
|
||||
@ -106,26 +111,37 @@ bool checkPlausibility(float newValue, float prevValue, float maxDiff) {
|
||||
}
|
||||
|
||||
long lastMsg = 0;
|
||||
long lastSent = 0;
|
||||
bool published = false;
|
||||
bool forcePublish = false;
|
||||
|
||||
bool publish (String desc, char *topic, float val, float &lastVal, float diff, bool force){
|
||||
bool ret = false;
|
||||
Serial.println("Sample " + desc + ": " + String(val).c_str());
|
||||
if (checkVariation(val, lastVal, diff) or force) {
|
||||
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);
|
||||
ret = true;
|
||||
} else {
|
||||
Serial.print("** ERROR: Value out of bounds, not published. Last value :");
|
||||
Serial.println(String(lastVal).c_str());
|
||||
}
|
||||
lastVal = val;
|
||||
void publish (Sensor &sensor){
|
||||
bool force = false;
|
||||
Serial.println("Sample " + sensor.desc + ": " + String(sensor.value).c_str());
|
||||
|
||||
// check if first sample, set lastValue and return
|
||||
if (sensor.lastUpdate == -1){
|
||||
sensor.lastValue = sensor.value;
|
||||
return;
|
||||
}
|
||||
|
||||
// force an update if the number of samples without update is exceeded
|
||||
Serial.println(" * Forcing publication if sensor value is plausible");
|
||||
force = sensor.lastUpdate >= FORCE_PERIOD;
|
||||
|
||||
// update the sample counter, will be reset if sample is published
|
||||
sensor.lastUpdate += 1;
|
||||
|
||||
// check if there was sufficient variation on the signal and if the value is plausible
|
||||
if (checkVariation(sensor.value, sensor.lastValue, sensor.diff) or force) {
|
||||
Serial.println(" Sufficient variation on " + sensor.desc + ": " + String(sensor.value).c_str());
|
||||
if (checkPlausibility(sensor.value, sensor.lastValue, sensor.diff)) {
|
||||
Serial.println(" Value published");
|
||||
client.publish(sensor.topic, String(sensor.value).c_str(), true);
|
||||
sensor.lastUpdate = 0;
|
||||
} else {
|
||||
Serial.print(" ** ERROR: Value out of bounds, not published. Last value :");
|
||||
Serial.println(String(sensor.lastValue).c_str());
|
||||
}
|
||||
sensor.lastValue = sensor.value;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void loop() {
|
||||
@ -143,32 +159,18 @@ void loop() {
|
||||
lastMsg = now;
|
||||
if (OPERATION_BLINK_EN) {digitalWrite(LED_BUILTIN, HIGH);}
|
||||
|
||||
hmid = dht.readHumidity();
|
||||
temp = dht.readTemperature();
|
||||
hmid.value = dht.readHumidity();
|
||||
temp.value = dht.readTemperature();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
published = false;
|
||||
if (lastSent > FORCE_PERIOD) {
|
||||
Serial.println(F("Forcing a publish of all values"));
|
||||
forcePublish = true;
|
||||
lastSent = 0;
|
||||
if (!isnan(hmid.value) && !isnan(temp.value)){
|
||||
abhu.value = (6.112 * ( pow(2.71828, ((17.67 * temp.value) / (temp.value + 243.5))) * hmid.value * 2.1674)) / (273.15 + temp.value);
|
||||
heat.value = dht.computeHeatIndex(temp.value, hmid.value, false);
|
||||
}
|
||||
|
||||
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;
|
||||
} else {
|
||||
lastSent = 0;
|
||||
}
|
||||
|
||||
forcePublish = false;
|
||||
publish(temp);
|
||||
publish(hmid);
|
||||
publish(heat);
|
||||
publish(abhu);
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user