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_BASE "sensor/" SENSOR_ID "/" | ||||||
| #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_ABHU TOPIC_BASE "abs_humidity" | #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_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_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_ABHU 0.2 // small changes mean quite something
 | ||||||
|  | #define DIFF_HEAT 0.3 // this one's actually quite active
 | ||||||
| 
 | 
 | ||||||
| float hmid = 0; // humidity
 | struct Sensor { | ||||||
| float hmid_r = 0; // humidity
 |   String desc; // sensor description
 | ||||||
| float abhu = 0; // absolute humidity
 |   char* topic; // sensor topic
 | ||||||
| float abhu_r = 0; // absolute humidity
 |   float diff; // value difference for an update
 | ||||||
| float temp = 0; // temperature
 |   float value; // current values
 | ||||||
| float temp_r = 0; // temperature
 |   float lastValue; // value at last update
 | ||||||
| float heat = 0; // heat index
 |   int lastUpdate; // number of samples since last update
 | ||||||
| float heat_r = 0; // heat index
 | }; | ||||||
|  | 
 | ||||||
|  | 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 DHTPIN 4     // Digital pin connected to the DHT sensor
 | ||||||
| #define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
 | #define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
 | ||||||
| @ -106,26 +111,37 @@ bool checkPlausibility(float newValue, float prevValue, float maxDiff) { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| long lastMsg = 0; | 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){ | void publish (Sensor &sensor){ | ||||||
|   bool ret = false; |   bool force = false; | ||||||
|   Serial.println("Sample " + desc + ": " + String(val).c_str()); |   Serial.println("Sample " + sensor.desc + ": " + String(sensor.value).c_str()); | ||||||
|   if (checkVariation(val, lastVal, diff) or force) { | 
 | ||||||
|     Serial.println("Sufficient variation on " + desc + ": " + String(val).c_str()); |   // check if first sample, set lastValue and return
 | ||||||
|     if (checkPlausibility(val, lastVal, diff) or force) { |   if (sensor.lastUpdate == -1){  | ||||||
|       Serial.println("Value published"); |     sensor.lastValue = sensor.value; | ||||||
|       client.publish(topic, String(val).c_str(), true); |     return; | ||||||
|       ret = true; |   } | ||||||
|  | 
 | ||||||
|  |   // 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 { |     } else { | ||||||
|       Serial.print("** ERROR: Value out of bounds, not published. Last value :"); |       Serial.print(" ** ERROR: Value out of bounds, not published. Last value :"); | ||||||
|       Serial.println(String(lastVal).c_str()); |       Serial.println(String(sensor.lastValue).c_str()); | ||||||
|     } |     } | ||||||
|     lastVal = val; |     sensor.lastValue = sensor.value; | ||||||
|   } |   } | ||||||
|   return ret; |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void loop() { | void loop() { | ||||||
| @ -143,32 +159,18 @@ 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(); |     hmid.value = dht.readHumidity(); | ||||||
|     temp = dht.readTemperature(); |     temp.value = dht.readTemperature(); | ||||||
|    |    | ||||||
|     if (!isnan(hmid) && !isnan(temp)){ |     if (!isnan(hmid.value) && !isnan(temp.value)){ | ||||||
|       abhu = (6.112 * ( pow(2.71828, ((17.67 * temp) / (temp + 243.5))) * hmid * 2.1674)) / (273.15 + temp); |       abhu.value = (6.112 * ( pow(2.71828, ((17.67 * temp.value) / (temp.value + 243.5))) * hmid.value * 2.1674)) / (273.15 + temp.value); | ||||||
|       heat = dht.computeHeatIndex(temp, hmid, false); |       heat.value = dht.computeHeatIndex(temp.value, hmid.value, false); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     published = false; |     publish(temp); | ||||||
|     if (lastSent > FORCE_PERIOD) { |     publish(hmid); | ||||||
|       Serial.println(F("Forcing a publish of all values")); |     publish(heat); | ||||||
|       forcePublish = true; |     publish(abhu);     | ||||||
|       lastSent = 0; |  | ||||||
|     } |  | ||||||
|     |     | ||||||
|     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; |  | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user