Author Topic: DHT 22 support  (Read 698 times)

travelbug

  • Newbie
  • *
  • Posts: 4
    • View Profile
DHT 22 support
« on: April 28, 2017, 01:34:10 pm »
Has anyone built a library, or can tell me how to interrogate an DHT22 temperature/humidity sensor connected to a mcmodule?

There are tons of libraries for arduino, and particle photon etc, but I haven't been able to find a library/examply code for DHT22.

The reason I want to use this sensor is it is very accurate and very cheap (~$3).  I need to connect 4 or more to a mcmodule, wake them up once an hour, take a temp/humidity reading and try to upload it to the gateway if it is connected, otherwise store it locally (in memory) and power down the sensor and put the mcmodule back to sleep, and when gateway comes into range/comes online, then send the data up to the MQTT server.  Any chance someone has done something like this?

I need to have the gateway battery powered (very remote location), and since I only need hourly readings, I thought it would be possible to wake the gateway daily, poll the mcmodules for the locally collected hourly data,  send it up to the MQTT server, and put it back to sleep/turn it off again until next data upload window.

Any help or direction would be greatly appreciated.

Martin.

Share on Facebook Share on Twitter


Nick_W

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Re: DHT 22 support
« Reply #1 on: May 20, 2017, 01:29:13 pm »
travelbug,

It's very difficult to do this with the mcmodule, because of the timing of the pulses required for the DHT 22.

You'll notice that for all these sort of devices (one wire, like the dallas protocol DS18B20) in the Arduino libraries, the signals are "bit banged" - ie the lines are set high, then low with a specified interval (timing), you then listen on the line for the response from the sensor, and decode this as bits.

With the mc modules, they use an RTOS (Real Time Operating System) - which is how it seems to do many things at the same time. One of the limitations of an RTOS is that you are not able to send signals (ie bit bang) with precise timing. This is because the underlying RTOS can interrupt at any time whatever you are doing to service a routine with a higher priority (say service the radio for instance).

So, although you think you are waiting 100uS between pulses, the actual duration of the pulses can vary, due to other interrupts (which you can't turn off, unlike an Arduino).

Having said that, the timing requirements for these (and dallas) devices isn't that fast or precise, so you might be able to get them to work (even with the timing variability). I haven't tried yet, as they draw quite a bit of power when measuring (and a bit in standby) and there are other options out there (check out the HDC1000 or the Si7021-A20 for instance - Adafruit has break out boards for some of these, and they are quite cheap).

Another option would be for the mc module guys to build in the protocol(s) to the firmware (as they can control the timing/interrupts), but this is probably not a priority for them, especially given the power draw of these devices, which is not really compatible with a low power/battery powered device.

The I2C device's I suggested are only a couple of $ more expensive, but draw less than 10th (or less) of the power. That's 10 to 100 times the battery life!

I2C is supported out of the box by the mc modules, and lends itself handily to your stated objectives, at  fraction of the power 4 DHT22's would draw.

travelbug

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: DHT 22 support
« Reply #2 on: May 20, 2017, 11:55:58 pm »
Thanks for the reply Nick!

Tom from Mcthings mentioned you used multiple Bosch temp/ humidity sensors -bme 280? In some project?  Are they better/ lower power than what you recommended above?

I haven't had any luck finding sensor libraries to do multiple temps energy efficiently. with one mcmodule. Also haven't found a way to store data locally and detect a gateway and then bulk upload once in range... Again any help is greatly appreciated. Seems like such a promising platform but a cookbook is SO needed.  I come from an industrial PLC background and making the jump to microcontrollers and Mqtt and messaging is proving way more difficult than I expected. I just figured there would be much more documentation/examples out there...

Regards,
Martin aka travelbug

Nick_W

  • Full Member
  • ***
  • Posts: 215
    • View Profile
Re: DHT 22 support
« Reply #3 on: May 21, 2017, 12:20:33 am »
Martin,

Yes, the Bosch sensors are nice sensors, they do temp/humidity and air pressure, I wrote a library for them. They are a bit more expensive though (about $20 each). They are low power also.

You can daisy chain I2C modules, if you can select different addresses for each (some let you select the I2C address via jumpers).

You can also define more than one I2C bus, each uses 2 pins, and you can define any pins you want as an I2C interface.

As you say, the micro controller world is different. Ultra Low Power mcu is another world again...

The modules themselves are great, the ecosystem is good (need a better gateway!), but the lack of a cookbook, is, as you say a major problem.

If the modules had a humidity sensor, you could just use 4 modules. Lack of a humidity sensor is the major failing point right now, as knowing temperature without humidity is mostly useless. At least In my applications.

Check out my github site, I have all my code/libraries there, plenty of sensor examples. No data logging yet, but it's a good idea. The modules have 20k ram, so you could store quite a few readings.

Google Nick Waterton github.