diff --git a/.gitignore b/.gitignore index ba357fc..6f54def 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,4 @@ .vscode/launch.json .vscode/ipch -src/config.h \ No newline at end of file +include/config.h \ No newline at end of file diff --git a/src/config.h.tmpl b/include/config.h.tmpl similarity index 74% rename from src/config.h.tmpl rename to include/config.h.tmpl index a73197a..1c56ef3 100644 --- a/src/config.h.tmpl +++ b/include/config.h.tmpl @@ -11,6 +11,4 @@ #define DHTPIN D4 // pin gpio 2 in sensor #define DHTTYPE DHT22 // DHT 22 Change this if you have a DHT11 -#define CLIENT_ID "CLIENT_ID_HERE" -#define HUMIDITY_TOPIC "sensor/humidity" -#define TEMPERATURE_TOPIC "sensor/temperature" +#define HOMEASSISTANT_TOPIC "homeassistant/sensor/sensorDht" diff --git a/include/utils.h b/include/utils.h new file mode 100644 index 0000000..834fccc --- /dev/null +++ b/include/utils.h @@ -0,0 +1,6 @@ +#ifndef UTILS_H +#define UTILS_H + +double round2(double value); + +#endif \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 6ae4155..b360d33 100644 --- a/platformio.ini +++ b/platformio.ini @@ -17,4 +17,5 @@ lib_deps = jfturcot/SimpleTimer@0.0.0-alpha+sha.b30890b8f7 adafruit/Adafruit Unified Sensor@^1.1.5 knolleary/PubSubClient@^2.8 + bblanchon/ArduinoJson@^6.19.4 monitor_speed = 115200 diff --git a/src/main.cpp b/src/main.cpp index b3ae307..cba1c62 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,13 +2,74 @@ #include #include #include - +#include #include +#include WiFiClient espClient; PubSubClient client(espClient); DHT dht(DHTPIN, DHTTYPE); +void setup_wifi(); +void reconnect(); +void mqtt_callback(char* topic, byte* payload, unsigned int length); +void mqtt_publish_config(); +void mqtt_publish_state(); + +float temp; +float hum; +long lastMsg = 0; + +void setup() { + Serial.begin(115200); + setup_wifi(); + + WiFi.setAutoReconnect(true); + WiFi.persistent(true); + + client.setServer(MQTT_SERVER, MQTT_PORT); + client.setCallback(mqtt_callback); + + if (!client.connected()) { + reconnect(); + } + client.subscribe("homeassistant/status"); + + mqtt_publish_config(); + + dht.begin(); + + if (WiFi.status() == WL_CONNECTED) { + temp = dht.readTemperature(); + hum = dht.readHumidity(); + + mqtt_publish_state(); + } +} + +void loop() { + if (!client.connected()) { + reconnect(); + } + client.loop(); + + temp = round2(dht.readTemperature()); + hum = round2(dht.readHumidity()); + mqtt_publish_state(); + + delay(60000); +} + +void mqtt_callback(char* topic, byte* payload, unsigned int length) { + if (strcmp(topic, "homeassistant/status") == 0) { + payload[length] = '\0'; + if (strcmp((char*)payload, "online") == 0) { + Serial.println("Home Assistant is online"); + mqtt_publish_config(); + } + } +} + void setup_wifi() { delay(10); // We start by connecting to a WiFi network @@ -48,49 +109,45 @@ void reconnect() { } } -bool checkBound(float newValue, float prevValue, float maxDiff) { - return !isnan(newValue) && - (newValue < prevValue - maxDiff || newValue > prevValue + maxDiff); +void mqtt_publish_config() { + StaticJsonDocument<256> doc; + char buffer[256]; + + doc["device_class"] = "temperature"; + doc["name"] = "Temperature"; + doc["unit_of_meas"] = "°C"; + doc["state_topic"] = HOMEASSISTANT_TOPIC"/state"; + doc["value_template"] = "{{value_json.temperature}}"; + doc["unique_id"] = "t"; + + serializeJson(doc, buffer); + + client.publish(HOMEASSISTANT_TOPIC"/t/config", buffer, true); + + doc["device_class"] = "humidity"; + doc["name"] = "Humidity"; + doc["unit_of_meas"] = "%"; + doc["state_topic"] = HOMEASSISTANT_TOPIC"/state"; + doc["value_template"] = "{{value_json.humidity}}"; + doc["unique_id"] = "h"; + + serializeJson(doc, buffer); + + client.publish(HOMEASSISTANT_TOPIC"/h/config", buffer, true); } -long lastMsg = 0; -float temp = 0.0; -float hum = 0.0; -float diff = 1.0; +void mqtt_publish_state() { + Serial.println("===== Sending Data ====="); + StaticJsonDocument<256> doc; + char buffer[256]; -void setup() { - Serial.begin(115200); - setup_wifi(); - client.setServer(MQTT_SERVER, MQTT_PORT); + doc["temperature"] = round2(temp); + doc["humidity"] = round2(hum); - dht.begin(); -} + Serial.printf("Temperature: %f\n", round2(temp)); + Serial.printf("Humidity: %f\n", round2(hum)); -void loop() { - if (!client.connected()) { - reconnect(); - } - client.loop(); + serializeJson(doc, buffer); - long now = millis(); - if (now - lastMsg > 10000) { - lastMsg = now; - - float newTemp = dht.readTemperature(); - float newHum = dht.readHumidity(); - - if (checkBound(newTemp, temp, diff)) { - temp = newTemp; - Serial.print("New temperature:"); - Serial.println(String(temp).c_str()); - client.publish(TEMPERATURE_TOPIC, String(temp).c_str(), true); - } - - if (checkBound(newHum, hum, diff)) { - hum = newHum; - Serial.print("New humidity:"); - Serial.println(String(hum).c_str()); - client.publish(HUMIDITY_TOPIC, String(hum).c_str(), true); - } - } + client.publish(HOMEASSISTANT_TOPIC"/state", buffer, true); } \ No newline at end of file diff --git a/src/utils.cpp b/src/utils.cpp new file mode 100644 index 0000000..a8712a2 --- /dev/null +++ b/src/utils.cpp @@ -0,0 +1,3 @@ +double round2 (double value) { + return (int)(value * 100 + 0.5) / 100.0; +} \ No newline at end of file