MQTT assistance

First, you need to figure out if it’s the sketch or HA that is at fault. Easiest way is to run mosquitto_sub and subscribe to the topic “#” which will print all messages to any topic as they arrive.

The sketch looks like it’s supposed to wake up every sleepTimeS seconds - the ESP.deepSleep function takes a parameter in microseconds, hence the multiplication of sleepTimeS by 1000000 (one second being a million microseconds). So if you set sleepTimeS equal to 300, it should wake up every 5 mins and send readings.

The function isn’t commented out, only the comment “Sleep” which I suppose the author is using to signal to the reader that what follows affects the device sleeping.

My understanding is that the max deepsleep time that can be specified is approx 70 mins (the function uses an unsigned int or unsigned long which can hold at maximum a number up to about 4.2 billion, which if you do the time-math (convert microseconds to minutes) comes out to about 71).

Also keep in mind that HA won’t “update” or show a changed value if the number it receives is the same. There is a config parameter that affects this, I can’t remember which at the moment. Seems unlikely that the temp and humidity wouldn’t fluctuate but hey you never know.

And also if you disconnect the wire to RST it won’t wake up from deep sleep - so maybe that’s it? Are you sure you’ve wired it correctly? RST should be connected to D0 (GPIO 16).

All good, seems my wire from RST to D0 was not connected as well as I though.

image

Getting regular readings now and even in that long stretch of flat at the end it has taken the same reading all morning which is believable.

Thanks again. Received a new NodeMCU yesterday and DHT22 so off to build another one for another room

1 Like

I know you already have MQTT up and running, but can highly recommend following Ben’s (@bruhautomation) 2 videos on MQTT and making HA compatible DIY sensors, if you have not already. I followed both of these and have a self-hosted MQTT that works flawlessly with a number of Multi-Sensors from the 2nd video. I just adapted his sketch where necessary.

I originally used a DHT11, and changed to a DHT22 - far more accurate. Can highly recommend this change.

Yes I will be changing to a DHT22 when it arrives although now I have this one sensor setup I may leave it as the project I want it for can be a touch off if need be. The next sensor will definitely be using a 22 for higher accuracy.

This all started because of Ben’s $15 multi-sensor video.

1 Like

2 final things I’d like to ask.

  1. Is it the limitations of the DHT11 or somewhere in the sketch (or even in the HA config) but the readings for temp and humidity only come out as whole numbers for example 21.00 or 17.00 or 55.00%. Where does one get decimal place accuracy or is this not possible with this sensor (DHT22 should arrive this week so will update that part soon anyway but would like to know where this might be adjusted).

  2. Again this might be the sensor, but I’m still getting ‘nan’ readings in between proper readings. For example, when the sleep time was set to 300 seconds, the device would fire off every 5 minutes but on the 3rd firing it would give ‘nan’ as the result. 5 minutes later (and then for 10 minutes) I get proper readings. Sure enough when I lower the sleep time to 30 seconds, I get good readings for 60 seconds (2 cycles as I watch the NodeMCU light up twice) but then a 30 second cycle with ‘nan’ again. A bit painful only for the fact that I want a Sonoff switch to go on if the temp is below a certain figure at a certain time. Unsure what will happen if ‘nan’ is the reading at that time.

Any ideas or should I just wait for the DHT22 to see if it’ll fix itself completely?

DHT11 is not so accurate. It provides only whole numbers. DHT22 is more precise and provides decimal part. Some times you will get nan values because sensor needs some time to start and perform measurements. It’s better to add code block to handle such values and perform another shot to get values in a couple of seconds.

Thanks for the info. Thought as much regarding the DHT11 limitations

Awsome job! Do you got more information about the components used and your script? :slight_smile:

To everyone who has offered all their assistance throughout this post’s history, I’d just like to say the DHT22 is now working perfectly and much more accurately. Thanks so much for your valuable advice and lessons.

Have repurposed the original DHT11 onto another ESP8266 for a much less accurate job but after first sketch upload getting a temp of 516 degrees so will spend some time sorting out that now that I know what I know thanks to you all.

Can’t say how much I appreciate the assistance given on this forum. Keep up the great work people.

2 Likes