The use of the ESP8266 in the world of IoT

User avatar
By JABFreak
#87667 Hi to all,
I have setup a DHT22 on a D1 Mini which reports the information via MQTT to Home Assistant.
I have one issue and one question... Well I guess that makes it 2 questions.

1 - I power up and the temperature is showing 27C and Humidity is 37% which is about right. After a few days it reads a temperature of appr. 7C and humidity of appr 3%. Powering off for a few hours and back on brings back roughly the same bad numbers again. If I reflash the same program, I get the good results. I am hoping someone might be able to take a look at my code (a mixture of code I found here and there on the web as I am not fluent at this) and see if I can apply a fix.

2 - If I disconnect the device Home Assistant is still showing the last results so I have no way of knowing if the device is online or not. I am aware I can use a Last Will on MQTT but I cannot figure out how to go about it as the Will reports to its own topic but I would like if off line to show Offline in the place of the Temperature and Humidity numbers.

Here is my code:
Code: Select all#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "DHTesp.h"
DHTesp dht;

#define wifi_ssid "**"
#define wifi_password "**"

#define mqtt_server ""
#define mqtt_user "**"
#define mqtt_password "**"

#define temperature_topic "sensor/temperature1"  //Office
#define humidity_topic "sensor/humidity1"

char message_buff[100];

long lastMsg = 0;   
long lastRecu = 0;
bool debug = false;  //Display log message if True
WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  client.setServer(mqtt_server, 1883);
  dht.setup(4, DHTesp::DHT22);

void setup_wifi() {
  Serial.print("Connecting to ");

  WiFi.begin(wifi_ssid, wifi_password);

  while (WiFi.status() != WL_CONNECTED) {

  Serial.println("WiFi OK ");
  Serial.print("=> ESP8266 IP address: ");

void reconnect() {

  while (!client.connected()) {
    Serial.print("Connecting to MQTT broker ...");
    if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
    } else {
      Serial.print("Error : ");
      Serial.println(" Wait 5 seconds before retry");

void loop() {
  if (!client.connected()) {

  long now = millis();
  if (now - lastMsg > 1000 * 60) {
    lastMsg = now;
    int h = round(dht.getHumidity());
    int t = round(dht.getTemperature());
    if (debug) {
      Serial.print("Temperature : ");
      Serial.print(" | Humidity : ");
    client.publish(temperature_topic, String(t).c_str(), true);   // Publish temperature
    client.publish(humidity_topic, String(h).c_str(), true);      // Publish humidity
User avatar
By JABFreak
#87802 I resolved my question #2 quite simply actually.
The solution is not related to ESP8266 or D1 MINI but I figured I would post my solution in case someone else have the same issue.

In the configuration.yaml file, sensors have the parameter "expire_after" I programmed my device to send the data every 60 seconds and I added the parameter "expire_after: 120" so if within 120 seconds I didn't get an update, the temperature/humidity values change to "Unknown" which is good enough for me to know the data is not coming in.