Compare commits
2 Commits
cc268b5cd5
...
69d76139f2
Author | SHA1 | Date | |
---|---|---|---|
69d76139f2 | |||
9356c436bc |
@ -14,12 +14,18 @@
|
|||||||
#include "DHT.h"
|
#include "DHT.h"
|
||||||
#include "mqtt_credentials.h"
|
#include "mqtt_credentials.h"
|
||||||
|
|
||||||
#define ID_BASE "sensor/window/"
|
#define TOPIC_BASE "sensor/window/"
|
||||||
#define ID_TEMP ID_BASE "/temperature"
|
#define TOPIC_TEMP TOPIC_BASE "/temperature"
|
||||||
#define ID_HUMID ID_BASE "/temperature"
|
#define TOPIC_HMID TOPIC_BASE "/temperature"
|
||||||
#define ID_HEAT ID_BASE "/temperature"
|
#define TOPIC_HEAT TOPIC_BASE "/temperature"
|
||||||
|
|
||||||
#define OPERATION_BLINK_EN true // blink continously if in correct operation
|
#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 OPERATION_BLINK_EN true // blink continuously if in correct operation
|
||||||
|
#define OPERATION_PERIOD 5000 // sensor reading period in milliseconds
|
||||||
|
#define FORCE_PERIOD 12 // sensor reading publication force period in number of readings without publication
|
||||||
|
|
||||||
#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,6 +102,18 @@ 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){
|
||||||
|
if (checkBound(val, val_r, 0.3) or force) {
|
||||||
|
val_r = val;
|
||||||
|
Serial.print("New " + desc + ": ");
|
||||||
|
Serial.println(String(val).c_str());
|
||||||
|
client.publish(topic, String(val).c_str(), true);
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
if (!client.connected()) {
|
if (!client.connected()) {
|
||||||
reconnect();
|
reconnect();
|
||||||
@ -104,10 +122,10 @@ void loop() {
|
|||||||
digitalWrite(LED_BUILTIN, HIGH);
|
digitalWrite(LED_BUILTIN, HIGH);
|
||||||
|
|
||||||
long now = millis();
|
long now = millis();
|
||||||
if (now - lastMsg > 2500) {
|
if (now - lastMsg > OPERATION_PERIOD / 2) {
|
||||||
if (OPERATION_BLINK_EN) {digitalWrite(LED_BUILTIN, LOW);}
|
if (OPERATION_BLINK_EN) {digitalWrite(LED_BUILTIN, LOW);}
|
||||||
}
|
}
|
||||||
if (now - lastMsg > 5000) {
|
if (now - lastMsg > OPERATION_PERIOD) {
|
||||||
lastMsg = now;
|
lastMsg = now;
|
||||||
if (OPERATION_BLINK_EN) {digitalWrite(LED_BUILTIN, HIGH);}
|
if (OPERATION_BLINK_EN) {digitalWrite(LED_BUILTIN, HIGH);}
|
||||||
|
|
||||||
@ -121,7 +139,6 @@ void loop() {
|
|||||||
|
|
||||||
float hi = dht.computeHeatIndex(t, h, false);
|
float hi = dht.computeHeatIndex(t, h, false);
|
||||||
|
|
||||||
|
|
||||||
Serial.print(F("Humidity: "));
|
Serial.print(F("Humidity: "));
|
||||||
Serial.print(h);
|
Serial.print(h);
|
||||||
Serial.print(F("% Temperature: "));
|
Serial.print(F("% Temperature: "));
|
||||||
@ -131,36 +148,15 @@ void loop() {
|
|||||||
Serial.println(F("°C "));
|
Serial.println(F("°C "));
|
||||||
|
|
||||||
published = false;
|
published = false;
|
||||||
if (lastSent > 12) {
|
if (lastSent > FORCE_PERIOD) {
|
||||||
Serial.println(F("Forcing a publish of all values"));
|
Serial.println(F("Forcing a publish of all values"));
|
||||||
forcePublish = true;
|
forcePublish = true;
|
||||||
lastSent = 0;
|
lastSent = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (checkBound(t, t_r, 0.3) or forcePublish) {
|
published += publish("temperature", TOPIC_TEMP, t, t_r, DIFF_TEMP, forcePublish);
|
||||||
t_r = t;
|
published += publish("humidity", TOPIC_HMID, h, h_r, DIFF_HMID, forcePublish);
|
||||||
Serial.print("New temperature:");
|
published += publish("heat index", TOPIC_HEAT, hi, hi_r, DIFF_HEAT, forcePublish);
|
||||||
Serial.println(String(t).c_str());
|
|
||||||
client.publish(ID_TEMP, String(t).c_str(), true);
|
|
||||||
published = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (checkBound(h, h_r, 2.5) or forcePublish) {
|
|
||||||
h_r = h;
|
|
||||||
Serial.print("New humidity:");
|
|
||||||
Serial.println(String(h).c_str());
|
|
||||||
client.publish(ID_HUMID, String(h).c_str(), true);
|
|
||||||
published = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (checkBound(hi, hi_r, 0.2) or forcePublish) {
|
|
||||||
hi_r = hi;
|
|
||||||
Serial.print("New heat index:");
|
|
||||||
Serial.println(String(hi).c_str());
|
|
||||||
client.publish(ID_HEAT, String(hi).c_str(), true);
|
|
||||||
published = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (published == false) {
|
if (published == false) {
|
||||||
lastSent = lastSent + 1;
|
lastSent = lastSent + 1;
|
||||||
|
Loading…
Reference in New Issue
Block a user