#include #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 Serial.println(); Serial.print("Connecting to "); Serial.println(WIFI_SSID); WiFi.begin(WIFI_SSID, WIFI_PASS); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect // If you do not want to use a username and password, change next line to // if (client.connect("ESP8266Client")) { if (client.connect("ESP8266Client", MQTT_LOGIN, MQTT_PASS)) { Serial.println("connected"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } 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); } void mqtt_publish_state() { Serial.println("===== Sending Data ====="); StaticJsonDocument<256> doc; char buffer[256]; doc["temperature"] = round2(temp); doc["humidity"] = round2(hum); Serial.printf("Temperature: %f\n", round2(temp)); Serial.printf("Humidity: %f\n", round2(hum)); serializeJson(doc, buffer); client.publish(HOMEASSISTANT_TOPIC"/state", buffer, true); }