MQTT and DHT22

@Dee I used a Mac, but that doesn’t matter, it should work on any platform. Any chance you can post the error message you’re getting? Also, you’ll have to make sure you have the right libraries installed in your IDE. I’ll take a look at mine, and add a link to the required libraries (please give me a bit, still waking up). But the errors should help us figure it out.

This was in the default examples so I was just testing that one. There was others I tested a scan for WiFi, and connect to WiFi, those all worked I did download the DHT22 files from Adafruit I’m thinking it may be a different file because of the error says # include <Adafruit_sensor.h> not # include “DHT.h”
also my board has a D2 not a 2 i think it is because it’s a newer board.

I haven’t tried your sketch yet I seen the says #include <PubSubClient.h> since I’m running Windows I’m not sure where to find this do I have to download MQTT to my PC or just a download to IDE my first thing is to get it to see the sensor.

I have a few Errands to run I will be back in a few hours. Thanks for your help

@xstrex I finally got it working on the test it displays temp and humidity properly
I just quickly tried your code but it faild But I think i need more library’s

You’ll need to add the libraries to the Arduino IDE, here’s a quick quide on how to do that: https://www.arduino.cc/en/Guide/Libraries

You’ll need these installed:
PubSubClient.h
ESP8266WiFi.h
DHT.h

For DHT see this: https://learn.adafruit.com/dht Should have download instructions
For pubsubclient, I was using 1.9, found: https://github.com/knolleary/pubsubclient
I belive the ESP8255WiFi library was added via the “library manager” in Arduino IDE

Hope fully that helps, let me know if you run into any more issues.

@xstrex OK I have a verify working with your code, It verifies and uploads OK. I can’t seem to find the format for the username and password. It is connecting to the network but failing to connect to the broker.

See here for an example on how to use a mqtt user/pass: https://github.com/knolleary/pubsubclient/blob/master/examples/mqtt_auth/mqtt_auth.ino
You’ll have to modify the code a bit, let me know if you can’t get it working.

@xstrex I got it working with out a username and password, I’m just going to leave it like that for now. until I learn more on the code. Thanks again for all your help I’m setting up the 2 I have Probably order more :slight_smile:. I like that they are small and draw very little power, I can put one anywhere I have USB port or use a old phone charge, plus frees up a Raspberry pi. One more question can I use the MQTT Broker in HA or should i use the one i installed on my RPI. Thanks again for the help and letting me know about ESP8266 boards I had no Idea what could be done with them.

@Dee you’re very welcome! I really like the boards for the same reasons. If you’re really feeling adventurous you can design a few cases and have them 3D printed, let me know if you’d like help with that.
As for the broker, you can really do it either way. My strategy is to have 1 broker to handle all sensor nodes, which (at the moment) only handles 3 temp/humidity sensors and is installed on the same server as HA. Because the broker breaks items into topics, it’s really easy to keep things organized. I currently have a single root-level topic per room, with sub-topics for each device/sensor. Also in my testing the mosquito broker uses very little resources, and I have never (in several years) seen it crash, or have memory leak issues. Basically following the K.I.S.S. mentality, if there’s only one broker, there’s only one place to troubleshoot issues, and I’m not left guessing which node is using which broker.

Last but not least, before you call the sensor “done”, I would recommend changing the delay (near the bottom) to something more realistic, like 900,000 (15 minutes). The value is in milliseconds, so 15,000 is 15 seconds. Just think about how often you need to know the updated values. Every 15 seconds is great for testing, but not really practical for production. 15 minutes seems like it would be a better delay to me.

@xstrex I’m just getting back to this everything’s working great but I never changed the delay, But I did today, started with delay 45000 and up but, it doesn’t publish unless I have it set to delay 15000 any thoughts, I looked at the serial monitor and it connects to the broker but only posts first time it connects

Thanks

That’s really weird, the only delay in the code is for the loop. Might want to try different intervals see if you get the same result.

Thank you. I used your code with some minor changes using Adafruit DHT22. It is running on the same old RPI that I have my garage door controller.

1 Like

Hi, debsahu

thanks for posting your script. i have installed a dht22 sensor and I have testet the script, but i get an error. I hope you or others have an idea to what could be wrong :wink:

The error i get:
File “dht22b.py”, line 47
p_temp), temp_humi, abs(lastpubhumi-temp_humi))
^ (the arrow is pointing af the p in temp)
SyntaxError: invalid syntax

i have:

  1. installed the sensor dht22, and testet it with
    sudo ./AdafruitDHT.py 2302 24 and it works fine.
  2. installed pigpio library
  3. updated everything i could think of

I run everything via ssh on a rpi b+

ill be greatfull for any suggestions.

br peter

Comment out or delete that line, it got carried over from previous line. The text wrapping here wrapped that to the next line.

argh! you are a lifesaver, I should have seen that (so easy, now when i know…)

Thanks a lot for getting back to me. Ill give it a go as soon as i get home.

thanks a lot.