добавляет MQTT Discovery

This commit is contained in:
Maxim Slipenko 2022-06-02 22:49:47 +03:00
parent 1997b8c2d9
commit 283016d424
Signed by: Maks1mS
GPG Key ID: 7461AF39A8705FB8
6 changed files with 109 additions and 44 deletions

2
.gitignore vendored
View File

@ -4,4 +4,4 @@
.vscode/launch.json .vscode/launch.json
.vscode/ipch .vscode/ipch
src/config.h include/config.h

View File

@ -11,6 +11,4 @@
#define DHTPIN D4 // pin gpio 2 in sensor #define DHTPIN D4 // pin gpio 2 in sensor
#define DHTTYPE DHT22 // DHT 22 Change this if you have a DHT11 #define DHTTYPE DHT22 // DHT 22 Change this if you have a DHT11
#define CLIENT_ID "CLIENT_ID_HERE" #define HOMEASSISTANT_TOPIC "homeassistant/sensor/sensorDht"
#define HUMIDITY_TOPIC "sensor/humidity"
#define TEMPERATURE_TOPIC "sensor/temperature"

6
include/utils.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef UTILS_H
#define UTILS_H
double round2(double value);
#endif

View File

@ -17,4 +17,5 @@ lib_deps =
jfturcot/SimpleTimer@0.0.0-alpha+sha.b30890b8f7 jfturcot/SimpleTimer@0.0.0-alpha+sha.b30890b8f7
adafruit/Adafruit Unified Sensor@^1.1.5 adafruit/Adafruit Unified Sensor@^1.1.5
knolleary/PubSubClient@^2.8 knolleary/PubSubClient@^2.8
bblanchon/ArduinoJson@^6.19.4
monitor_speed = 115200 monitor_speed = 115200

View File

@ -2,13 +2,74 @@
#include <DHT.h> #include <DHT.h>
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <PubSubClient.h> #include <PubSubClient.h>
#include <ArduinoJson.h>
#include <config.h> #include <config.h>
#include <utils.h>
WiFiClient espClient; WiFiClient espClient;
PubSubClient client(espClient); PubSubClient client(espClient);
DHT dht(DHTPIN, DHTTYPE); 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() { void setup_wifi() {
delay(10); delay(10);
// We start by connecting to a WiFi network // We start by connecting to a WiFi network
@ -48,49 +109,45 @@ void reconnect() {
} }
} }
bool checkBound(float newValue, float prevValue, float maxDiff) { void mqtt_publish_config() {
return !isnan(newValue) && StaticJsonDocument<256> doc;
(newValue < prevValue - maxDiff || newValue > prevValue + maxDiff); 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; void mqtt_publish_state() {
float temp = 0.0; Serial.println("===== Sending Data =====");
float hum = 0.0; StaticJsonDocument<256> doc;
float diff = 1.0; char buffer[256];
void setup() { doc["temperature"] = round2(temp);
Serial.begin(115200); doc["humidity"] = round2(hum);
setup_wifi();
client.setServer(MQTT_SERVER, MQTT_PORT);
dht.begin(); Serial.printf("Temperature: %f\n", round2(temp));
} Serial.printf("Humidity: %f\n", round2(hum));
void loop() { serializeJson(doc, buffer);
if (!client.connected()) {
reconnect(); client.publish(HOMEASSISTANT_TOPIC"/state", buffer, true);
}
client.loop();
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);
}
}
} }

3
src/utils.cpp Normal file
View File

@ -0,0 +1,3 @@
double round2 (double value) {
return (int)(value * 100 + 0.5) / 100.0;
}