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 "172.16.0.125"
#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() {
  Serial.begin(9600);     
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  dht.setup(4, DHTesp::DHT22);
  pinMode(2,OUTPUT);
}

void setup_wifi() {
  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(wifi_ssid);

  WiFi.begin(wifi_ssid, wifi_password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

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

void reconnect() {

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

void loop() {
  if (!client.connected()) {
    reconnect();
   }
  client.loop();

  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(t);
      Serial.print(" | Humidity : ");
      Serial.println(h);
    }
    digitalWrite(2,HIGH); 
    client.publish(temperature_topic, String(t).c_str(), true);   // Publish temperature
    client.publish(humidity_topic, String(h).c_str(), true);      // Publish humidity
    delay(2000);
    digitalWrite(2,LOW);
  }
}