[SOLVED] MySensors and DHT22 - difficoulties in getting started



  • [SOLVED: Please look at the bottom post for explanation]

    Unfortunately I’m getting quite some trouble in having a DHT22 (AM2302) Sensor working with MySensors and NodeMCU.

    I have read each and every topic here in the forum, and I’ve downloaded the MySensors “External” library, but still I’m unable to obtain a reading.

    This (https://forum.mysensors.org/topic/4806/air-humidity-sensor/140) is the most comprehensive topic I could find, but I still can’t get a reading from DHT22 while using the last sketch reported there.

    I did try with the MySensors Arduino Examples and Library, but running that example I couldn’t even get an output, just “garbage” in serial monitor. (and yes, baud rate was right)
    I’ve read also this topic (https://forum.mysensors.org/topic/4672/dht-sketch-version-2-0-0-error-solved) but to no avail for me.
    I tried also this library (https://github.com/markruys/arduino-DHT/) but the serial output was always “Nan”.
    Also followed this topic (https://forum.mysensors.org/topic/6355/dht22-verify-issue) but serial monitor reported only “garbage”.
    And of course I followed the official example (https://www.mysensors.org/build/humidity) but to no avail: the compilation failed with warning “no matching function for call to 'DHT::DHT()'”
    Someone has experience with a working sketch for this DHT22 sensor?





  • After a lot of research, trial and error I managed to solve the issue; I’m going to share what I’ve learned, hoping that it might be useful for others.

    NOTE: * If you want the solution “ready to consume” just skip the explaining paragraph and go to the last one.*

    First of all, let me say that I have mixed feelings about this sensor: on paper it should be one of most basic and easy to use but in reality a quick search on the web shows a terrific amount of problems with this sensor.

    From what I could gather It is clear that this sensor (DHT22 – AM2023) IS NOT the best choice in terms of:

    • Usability
    • Accuracy
    • Compatibility

    I compared its readings against a BOSH BME280 and there is always a discrepancy of ~ 2°C and 5% RH.

    One could be tempted to move directly to BME280, and I would agree if not for a relatively big problem: this sensor works with I2C and so it is not usable if you plan on developing a Radio Node / Gateway with MySensors and NodeMCU.

    ( yes, I’m aware of the method to read multiple sensors on I2C by using a pair of Resistance approx. between 3K and 10K – depending on the sensor – but I wasn’t capable of getting the BME280 working with 2.4Ghz Radio on NodeMCU. Each time GW or NODE was trying to access Radio I had a soft reset.)

    Back to the DHT22: another huge problem, from my standpoint, is the incredible mess it has with libraries. Honestly, I could not describe it in another way: there are thousands of libraries “adapted” for the DHT22 and even more users complaining about their usability.

    Now MySensors should have its own Library for DHT22 (and I tried it!) except this library appears to have a problematic approach to NodeMCU ESP8266 and DHT22 Sensors made by “ASAIR”.

    ( And I believe this is something worth being clearly stated in the “Build” page of MySensors; a warning would have been nice for a novice like me, whom bought the “best” DHT22 sensors he could find in order to have a good measurement)

    Another very popular library is the Adafruit one, but it also got problems with ASAIR sensors; moreover it has been “stripped out” of error codes so the only error you might get is a quite meaningless “Can’t connect to DHT22”.

    NodeMCU, MySensors and ASAIR AM2032 DHT22

    So, here we are: how to make this “bloody thing” work with MySensors and ESP8266.

    First of all, you need this specific library: DHT v. 0.1.13 by Rob Tillaart.

    Not the previous, not the updated one: you need EXACTLY the VERSION 0.1.13.

    Secondly, you need to understand that NodeMCU board has a serious issue with PIN Numbering: they probably thought it was funny to rename all ESP8266 Pins differently on the board respect the GPIO ordering.

    (please bear with me for the joke, it took me almost 2 days to figure the issue with pins. Most certainly is because I'm a "n00b" but a little disclaimer would have been a blessing for my headache. 😛 )

    Keep in mind the following picture as reference:

    alt text

    If you have your DHT22 sensor connected to pin “D4” you have to initialize it in the Arduino IDE as “2” because D2 actually corresponds to GPIO2.

    If all this wasn’t enough, wait there is more!

    You have your sketch ready, your sensor wired (eventually with pull-up resistor and stability capacitor, depending on the version you bought. I was lazy and bought the sensor already attached on the micro-board with resistance and capacitor) and still getting “DHT22 Timeout” error in Serial Monitor.

    You don’t have a faulty sensor, do as follows:

    1. Connect the sensor to the board
    2. Load your sketch
    3. Open serial monitor, “DHT22 Timeout” will appear
    4. Disconnect just the sensor, keep the board powered and connected
    5. Reconnect the sensor in this precise order: Ground, VCC, Data
    6. Magically you will start having readings in the Serial Monitor.

    If you cut power to the Node/Gateway you have to repeat steps 4-5-6.

    Conclusions:

    I am happy to have a working sensor, but I’m frustrated about all the time I spent having a such simple sensor working correctly.

    I wish I had this information before, so I could plan a different development.

    I really hope that my experience could help other users.



  • @r-nox Thank you R-Nox! I finally figured it out, and I wrote my conclusions here. I hope it may be useful for other users. 🙂


  • Mod

    Thanks for sharing your experience @neo-mod

    You might be the first one to try the dht sensor with esp8266 and MySensors. Most people use Nanos or Pro Minis for sensor nodes.

    Some comments:
    I am using BME280 with rfm69 without problems and I did not have to add any resistors. Not sure why it works for me or why it didn’t work for you.

    You should be able to use D4 (but not 4 without D in front) for the pin definition. That’s the style I use for my esp8266 nodes.

    In addition to the drawbacks you mention for the DHT sensor, it is also not battery friendly.



  • @mfalkvidd thank you so much for your insights.

    I'm using a NRF24L01+PA+LNA and with BME280 I had an overlapping pin ( I followed the "How to connect the radio" official page). I'll open a new thread asking for advice, quite clearly I'm missing something.

    I will try the PIN Definition you mentioned in the next node, thank you! That will be a huge benefit.



  • @neo-mod said in [SOLVED] MySensors and DHT22 - difficoulties in getting started:

    @r-nox Thank you R-Nox! I finally figured it out, and I wrote my conclusions here. I hope it may be useful for other users. 🙂

    I'm glad you got it working. I too had the hardest time finding the right library. That's why I responded. You're write up should help other in the future too. Thank you.


  • Mod

    @neo-mod said in [SOLVED] MySensors and DHT22 - difficoulties in getting started:

    I'm using a NRF24L01+PA+LNA and with BME280 I had an overlapping pin ( I followed the "How to connect the radio" official page). I'll open a new thread asking for advice, quite clearly I'm missing something.

    I checked my code, and I'm using the BME280 in i2c mode. The i2c pins on Pro Mini are different from the SPI pins so there is no overlap.

    On esp8266, D1 and D2 are used for i2c which means the CE pin must be moved using MY_RF24_CE_PIN but if you do that it should work.


Log in to reply
 

Suggested Topics

16
Online

11.4k
Users

11.1k
Topics

112.7k
Posts