-->
Page 1 of 1

D1 Mini with DHT22+MQTT

PostPosted: Fri Jun 26, 2020 11:24 am
by JABFreak
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);
  }
}

Re: D1 Mini with DHT22+MQTT

PostPosted: Sun Jun 28, 2020 3:57 pm
by JABFreak
I added a 100nf cap to the power pins of the DHT22 and this seams to have resolved the issue for question #1.

Re: D1 Mini with DHT22+MQTT

PostPosted: Tue Jul 07, 2020 9:05 pm
by JABFreak
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.