danjperron
Posts: 3072
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Yet more DHT22 sensor code

Sat Jun 28, 2014 12:28 pm

Well my sensor hang time to time and I din't have any big appliances around it and the DHT22 was on a breadboard , connected to the RPi with 3 inches wire ,using his own regulator (LE33CZ). I check that problem and that wasn't for me.


B.T.W.

A 20 feet wire could pick up electric field and create noise in the signal.

Don't forget you have a 3.3V signal. It doesn't take much to induce external voltage on it.

To minimize noise you could use twist wire, shielded will be better, and but 2 terminators pull up resistor to minimize echo in the system, one at each end of the signal wire.

When you twisted wires, the external electric field tend to accumulate equally on each wire and it will be neutralize.

The twist pair should be the signal and the ground. For the power use the 104 capacitor which is 0.01 µF and a 10 µF or more capacitor. The reason is that the 10 µF capacitor has a inductance, create by the way the aluminium foil is wrapped, and won't be good for high frequency signal. This is why a capacitor like the 0.01µF(103) or a 0.1 µF(104) is always in parallel with it on most design.

Since you twist the wire together, this will tend to increase the capacitive effect and then you may have to decrease the pull-up resistor value.


B.T.W A ferrite will be a good thing to add on both side of the wires. This will really kill high frequency.

Daniel

rgrbic
Posts: 128
Joined: Thu Jun 12, 2014 1:07 pm
Contact: Website

Re: Yet more DHT22 sensor code

Sun Jun 29, 2014 9:15 am

Did you find out what is causing dht22 to stop responding?
At 127.0.0.1
Twitter: @rgrbic
IoT-projects.com

User avatar
joan
Posts: 13578
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Yet more DHT22 sensor code

Sun Jun 29, 2014 9:58 am

rgrbic wrote:Did you find out what is causing dht22 to stop responding?
I haven't, and as far as I know, judging by the www, no one else has either.

However I have discovered my current problems are self induced. I periodically tidy up breadboards by taking everything off and reinstalling/rewiring.

I noticed that I still had a voltage divider connected to the DHT22 output line, even though I have been powering from 3.3V for some time. My recent instability was possibly down to having a 2.2V high signal. I haven't had any problems since removing the voltage divider. Currently 45641 error free samples (sample every 2 seconds).

Massi
Posts: 1679
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Yet more DHT22 sensor code

Tue Jul 01, 2014 8:10 am

Hi all,
my DHT22 finally arrived from dx, but it's not the "bulk" sensor, and it's not the AM2032 version, it's something in the middle :)
it's this

http://www.dx.com/p/arduino-dht11-digit ... 7Jr95R_tQE

I can't find a datasheet of this specific "board", but I can see a resistor on the back side, so can i assume it's an embedded pull up? can i avoid an external pullup?
Last edited by Massi on Tue Jul 01, 2014 8:26 am, edited 1 time in total.

User avatar
joan
Posts: 13578
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Yet more DHT22 sensor code

Tue Jul 01, 2014 8:20 am

Connect + to 3V3 and - to ground. Connect OUT to a gpio. Set the gpio as read with pull-up/downs off. If the gpio reads high you may not need a pull-up.

Can you read the resistor value. I think about 5k is specified for the pull-up.

Massi
Posts: 1679
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Yet more DHT22 sensor code

Tue Jul 01, 2014 8:26 am

joan wrote:Connect + to 3V3 and - to ground. Connect OUT to a gpio. Set the gpio as read with pull-up/downs off. If the gpio reads high you may not need a pull-up.

Can you read the resistor value. I think about 5k is specified for the pull-up.
it seems to me that there's a "512" written on the embedded resistor, but it's far too small :)

tonight i'll try your code for reading..

rgrbic
Posts: 128
Joined: Thu Jun 12, 2014 1:07 pm
Contact: Website

Re: Yet more DHT22 sensor code

Tue Jul 01, 2014 11:12 am

pattagghiu wrote:
joan wrote:Connect + to 3V3 and - to ground. Connect OUT to a gpio. Set the gpio as read with pull-up/downs off. If the gpio reads high you may not need a pull-up.

Can you read the resistor value. I think about 5k is specified for the pull-up.
it seems to me that there's a "512" written on the embedded resistor, but it's far too small :)

tonight i'll try your code for reading..
it is 5.1k smd resistor
At 127.0.0.1
Twitter: @rgrbic
IoT-projects.com

Massi
Posts: 1679
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Yet more DHT22 sensor code

Tue Jul 01, 2014 11:16 am

i don't think there can be a different reason to be there if not to be a pull up resistor, isn't it? :)
and what about the other smd thing? there's nothing written on it.. can it be a fuse? i can't see where it's connected :(

rgrbic
Posts: 128
Joined: Thu Jun 12, 2014 1:07 pm
Contact: Website

Re: Yet more DHT22 sensor code

Tue Jul 01, 2014 11:18 am

pattagghiu wrote:i don't think there can be a different reason to be there if not to be a pull up resistor, isn't it? :)
and what about the other smd thing? there's nothing written on it.. can it be a fuse? i can't see where it's connected :(
probably capacitor between supply pins
At 127.0.0.1
Twitter: @rgrbic
IoT-projects.com

Massi
Posts: 1679
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Yet more DHT22 sensor code

Tue Jul 01, 2014 11:19 am

rgrbic wrote:
pattagghiu wrote:i don't think there can be a different reason to be there if not to be a pull up resistor, isn't it? :)
and what about the other smd thing? there's nothing written on it.. can it be a fuse? i can't see where it's connected :(
probably capacitor between supply pins
this is good :) i had never seen a capacitor in that format, really really noob speaking..
Thanks

rgrbic
Posts: 128
Joined: Thu Jun 12, 2014 1:07 pm
Contact: Website

Re: Yet more DHT22 sensor code

Tue Jul 01, 2014 3:04 pm

pattagghiu wrote:
rgrbic wrote:
pattagghiu wrote:i don't think there can be a different reason to be there if not to be a pull up resistor, isn't it? :)
and what about the other smd thing? there's nothing written on it.. can it be a fuse? i can't see where it's connected :(
probably capacitor between supply pins
this is good :) i had never seen a capacitor in that format, really really noob speaking..
Thanks
I believe you saw it but you didn't recognize it. Nowadays, surface mount technology largely replaced traditional through-hole technology.
At 127.0.0.1
Twitter: @rgrbic
IoT-projects.com

Massi
Posts: 1679
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Yet more DHT22 sensor code

Tue Jul 01, 2014 7:15 pm

rgrbic wrote:I believe you saw it but you didn't recognize it.
probably i never looked at that :)
btw, my dht22 is up and running with joan's code.
After a thousand readings no errors and no timeouts. Keep running..

john44
Posts: 2
Joined: Mon Jul 28, 2014 1:07 am

Re: Yet more DHT22 sensor code

Mon Jul 28, 2014 1:16 am

Hi

I have tried implementing Joan's python code at the beginning of this thread to read the DHT22 sensor but I get a python error:

ImportError: No module named DHT22

which corresponds to line 178: import DHT22

It seems that python needs the DHT22 library. Does anyone know where to find it?

Thanks

John

User avatar
joan
Posts: 13578
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Yet more DHT22 sensor code

Mon Jul 28, 2014 7:51 am

john44 wrote: ...
Hi

I have tried implementing Joan's python code at the beginning of this thread to read the DHT22 sensor but I get a python error:

ImportError: No module named DHT22

which corresponds to line 178: import DHT22

It seems that python needs the DHT22 library. Does anyone know where to find it?

Thanks

John
You must name the Python file DHT22.py. The file contains the Python module as well as the test script (the __main__ part).

I suggest you change the polling time to 3 seconds minimum rather than 2 seconds. After doing that I have not seen sensor hangs.

I do have an updated version of the code which corrects an error to do with message framing.

I will post the updated code in the next day or so.

Dartmouth
Posts: 3
Joined: Mon Jul 28, 2014 4:51 pm

Re: Yet more DHT22 sensor code

Mon Jul 28, 2014 6:03 pm

Hi,

I also have several DHT22's around. to read them using python I use the Adafruit python DHT22 library. I used an Adafruit tutorial to get my system set up, and the specific page for software installation is here https://learn.adafruit.com/dht-humidity ... ll-updated. It works really well, but I also recommend decreasing the poll interval to 3 or even 5 seconds, as the DHT22 often has small read errors when polling too frequently.

-Dartmouth

john44
Posts: 2
Joined: Mon Jul 28, 2014 1:07 am

Re: Yet more DHT22 sensor code

Tue Jul 29, 2014 1:07 am

Hi Joan,

Sorry, I am new to python! I renamed the file per your instruction and the DHT22 sensor is working very well now. I changed the polling frequency to be much greater than 2s since for my application a slower refresh rate is perfectly fine. Thanks for your help!

John

Massi
Posts: 1679
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Yet more DHT22 sensor code

Wed Jul 30, 2014 8:50 pm

using joan's code, it's not clear to me how to catch exceptions raised in _cb function.
It seems to me that it's threaded (i think it's something in pigpio callback..) so i can't catch those exception if i put a "try" out of the class trying to read the values.
Is there a way to do that?

I don't need to make reading every 2 seconds, i only need the script to give me the value when i call it. So my script is only

Code: Select all

	try:
		pi = pigpio.pi()
		s = DHT22_sensor(pi, 22)
		s.trigger()
		time.sleep(0.2)
		
	        print('{:3.2f}'.format(s.humidity() / 1.))
		print('{:3.2f}'.format(s.temperature() / 1.))
		
		s.cancel()
		pi.stop()
and as i was saying, if a exception is raised in the _cb, it will not be catched here.

User avatar
joan
Posts: 13578
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Yet more DHT22 sensor code

Sat Aug 02, 2014 1:20 pm

Okay.

My problems were mainly down to stupidity on my part.

1) don't try to use <2.2V as the input.
2) don't try to take a reading faster than 2 times a second.

Updated code in the OP. Unless you understand what you are doing set power and LED parameters to None.

7+ up days
DHT22_ 2014-08-02.png
DHT22_ 2014-08-02.png (28.09 KiB) Viewed 4835 times

paulv
Posts: 546
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: Yet more DHT22 sensor code

Tue Aug 05, 2014 7:12 am

Joan,

This may be blasphemy for asking, but do you think it is possible to use the Python GPIO interface rather than your PIGPIO?
I would like to embed the DHT22 in an already large program, to get humidity data.
The sensor may also replace one of my 3 DS18B20's for temperature.
My program is over 3500 lines of code and is littered with GPIO inputs/outputs, callbacks, threads etc. and I don't want to change everything since it's working fine.

I am currently experimenting with the DHT22 and the SPI interface, but I'm getting weird IOerror (file limit exceeded) exceptions when I run a test.

Tks, Paul

User avatar
joan
Posts: 13578
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Yet more DHT22 sensor code

Tue Aug 05, 2014 7:42 am

Are you using danjperron's SPI technique? That does work and provides, I think, lower error rates than pigpio (pigpio averages one checksum error in 40,000 reads). Might be best to find the root cause of the SPI IO error. It sounds like it indicates a problem possibly somewhere else in your program.

It should be possible to use other Python modules but you'd have to use a different technique, i.e. don't try to use the same methods as the C solutions.

You'd likely have to make several readings to get a valid temperature/humidity but I assume that doesn't matter for your application.

For Python
  1. call out to a C program to provide the reading.
  2. duplicate the SPI technique of triggering the reading and then doing a busy loop sampling the gpio 500 times. That should get a sample every 8 microseconds or so. Then trawl through the samples to find the level changes. Don't try to use time.sleep(0.00001) or such like. They will not work.

danjperron
Posts: 3072
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Yet more DHT22 sensor code

Tue Aug 05, 2014 4:56 pm

Hi Paul,

What is the size of your SPI data packet.

By default it is limited to 4096 bytes. If you want bigger you need to change the bufsiz parameters.

check this post

http://www.raspberrypi.org/forums/viewt ... 82#p309582


This maybe a clue on your exceeded file size limit.


Daniel

paulv
Posts: 546
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: Yet more DHT22 sensor code

Thu Aug 07, 2014 7:38 pm

Hi danjperron and joan,

I tried danjperron's SPI code (the latest class version), and that gave me the "IO error : [Errno 24] Too many open files" error.

To counter this error message, I used

Code: Select all

cmd_ret = subprocess.call(["ulimit -Sn 4096"], shell=True)
to raise the level from 1024 to 4096 but that didn't help.

I was at a loss, so I went back to the Adafruit code. One reason was that I'm almost out of GPIO ports, so using just one vs the two needed for the SPI interface helped me a lot.

First I tried to run the Adafruit code in a tight loop hammering the DHT22 every 4 seconds and logging the results. I used the same method for the SPI version. The SPI version would show the IOError in about 4-6 hours, the Adafruit code ran for more than a day without a single hick-up. Happy with that, I embedded the code into my complete test-bed.
Here's a piece of my test program:

Code: Select all

def write_log(temp, hum):
    try:
        tstamp = strftime("%H:%M:%S")
        # open the log file and append results
        with open("dht_test.csv", "a") as fout:
                fout.write (str(tstamp)+"\t"+str(temp)+"\t"+str(hum)+"\n")
    except IOError as e:
            # *** IO error :  [Errno 24] Too many open files: 'dht_test.csv'
            print "*** IO error : ", str(e)
            pass

    except Exception as e:
        subprocess.call(['logger "Exception in DHT22 write_log"'], shell=True)
        return


def main():

    # raise the limit to the number of open files
    cmd_ret = subprocess.call(["ulimit -Sn 4096"], shell=True)
    if cmd_ret <> 0 : print cmd_ret

    try:
        while True:
            tstamp = strftime("%H:%M:%S")
            temp, hum = dht22spi()
            print tstamp, temp, hum
            write_log(temp, hum)
            sleep(4)

    except Exception as e:
        print "Python exception", str(e)
        # capture the traceback information causing the Python termination
        try:
            with open("dht_errlog.txt", "a") as fout:
                fout.write ("\n---- "+str(strftime("%d-%m-%Y %H:%M:%S", localtime()))+"\n")
        except IOError as e:
            print "*** Python exception + IOerror : ", str(e)
            traceback.print_exc(file=open("dht_errlog.txt","a"))
            pass
My program ( a web based thermostat) calls the DHT three times with a 3 second delay in between, and I average the readings before passing them on. The BangBang controller loop (a thread) repeats this just about every 30 seconds.
The Adafruit code has been running embedded in my program for three days now and I have not seen a single problem. (I keep track of bad readings in a log file)

When I have some more time, I will use the suggestion from danjperron and up the bufsize parameter. I will let you know because I'm puzzled, I'm not using the SPI module for anything else. BTW, my testbed runs on a Rasperry Model A, but the real thing on a Model B. I have installed the latest kernel and all the updates/upgrades.

User avatar
joan
Posts: 13578
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Yet more DHT22 sensor code

Thu Aug 07, 2014 7:59 pm

For whatever reason you are opening a file but not closing it. As it seems to happen only when you use the SPI code that is the obvious place to look for the error.

By the way the Adafruit code (at least the versions I've looked at) will have had lots of errors, it just masks them by retrying until it gets a successful read. Fortunately that doesn't matter to most users.

paulv
Posts: 546
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: Yet more DHT22 sensor code

Fri Aug 08, 2014 2:04 pm

Hi joan,

I only use the "with open" in the loop once, to log the data, and that should close the file automatically. (I even tried to force-close the file with no result) I do not have any (user) file activity on the Pi myself, and I run the test code on a clean version of the OS. No other user programs run.

I'm wondering if the "IO error : [Errno 24] Too many open files" is not pointing us in the wrong direction. It may very well be the point where Python finally notices the exception, but the root cause could be somewhere else. Unfortunately, I do not have the insight knowledge to take this much further.

Thanks for the inputs though!

Paul

sloughin
Posts: 4
Joined: Sat Aug 16, 2014 5:27 pm

Re: Yet more DHT22 sensor code

Sat Aug 16, 2014 9:21 pm

You say you connect the sensor to GPIO.8 however, gpio readall doesn't seem to show a GPIO.8. I tried it connected to physical pin 7 (thinking perhaps you start labeling your GPIO pins at 1 instead of 0) and using 3.3V with a 5K pull up resistor, and I got -999 for my readings. Noticing that your gpio argument is 22, and the gpio readall has BCM 22 on physical pin 15 which is GPIO.3, I tried that instead.

Code: Select all

s = DHT22.sensor(pi, 22, LED=16, power=8) # as code was supplied

Code: Select all

+-----+-----+---------+------+---+-Model B2-+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 |   IN | 1 |  3 || 4  |   |      | 5V      |     |     |
 |   3 |   9 |   SCL.1 |   IN | 1 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 1 | ALT0 | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 1 | ALT0 | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 1 | OUT  | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 0 | IN   | CE1     | 11  | 7   |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |  28 |  17 | GPIO.17 | ALT2 | 0 | 51 || 52 | 0 | ALT2 | GPIO.18 | 18  | 29  |
 |  30 |  19 | GPIO.19 | ALT2 | 0 | 53 || 54 | 0 | ALT2 | GPIO.20 | 20  | 31  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+-Model B2-+---+------+---------+-----+-----+
And that works! Thanks.

Return to “Automation, sensing and robotics”