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

Yet more DHT22 sensor code

Wed Mar 05, 2014 10:24 pm

This code has been updated.

http://abyz.co.uk/rpi/pigpio/code/DHT22_py.zip

The power parameter is only needed if you are sampling every 2 seconds or faster. Omit it otherwise. It specifies the gpio being used to switch power to the sensor.

The LED parameter gives visual feed-back whenever a sample is taken. The default, gpio 16, is the activity LED on Rev.1/2 boards. On Rev.3 boards use gpio 47. Alternatively connect your own LED.

My DHT22 arrived from China this morning. After decoding some of the more bizarre documentation known to man I now seem to have working Python code.

The DHT22 is powered from 3.3V and the output goes direct to gpio8.

If you power from 5V you'd have to muck about with external pull-ups and a resistor divider to get the circuit working properly.

For 5V operation I have done the following

Code: Select all

#!/usr/bin/env python

# 2014-07-11 DHT22.py

import time
import atexit

import pigpio

class sensor:
   """
   A class to read relative humidity and temperature from the
   DHT22 sensor.  The sensor is also known as the AM2302.

   The sensor can be powered from the Pi 3V3 or the Pi 5V rail.

   Powering from the 3V3 rail is simpler and safer.  You may need
   to power from 5V if the sensor is connected via a long cable.

   For 3V3 operation connect pin 1 to 3V3 and pin 4 to ground.

   Connect pin 2 to a gpio.

   For 5V operation connect pin 1 to 5V and pin 4 to ground.

   The following pin 2 connection works for me.  Use at YOUR OWN RISK.

   5V--5K_resistor--+--10K_resistor--Ground
                    |
   DHT22 pin 2 -----+
                    |
   gpio ------------+
   """

   def __init__(self, pi, gpio, LED=None, power=None):
      """
      Instantiate with the Pi and gpio to which the DHT22 output
      pin is connected.

      Optionally a LED may be specified.  This will be blinked for
      each successful reading.

      Optionally a gpio used to power the sensor may be specified.
      This gpio will be set high to power the sensor.  If the sensor
      locks it will be power cycled to restart the readings.

      Taking readings more often than about once every two seconds will
      eventually cause the DHT22 to hang.  A 3 second interval seems OK.
      """

      self.pi = pi
      self.gpio = gpio
      self.LED = LED
      self.power = power

      if power is not None:
         pi.write(power, 1) # Switch sensor on.
         time.sleep(2)

      self.powered = True

      self.cb = None

      atexit.register(self.cancel)

      self.bad_CS = 0 # Bad checksum count.
      self.bad_SM = 0 # Short message count.
      self.bad_MM = 0 # Missing message count.
      self.bad_SR = 0 # Sensor reset count.

      # Power cycle if timeout > MAX_TIMEOUTS.
      self.no_response = 0
      self.MAX_NO_RESPONSE = 2

      self.rhum = -999
      self.temp = -999

      self.tov = None

      self.high_tick = 0
      self.bit = 40

      pi.set_pull_up_down(gpio, pigpio.PUD_OFF)

      pi.set_watchdog(gpio, 0) # Kill any watchdogs.

      self.cb = pi.callback(gpio, pigpio.EITHER_EDGE, self._cb)

   def _cb(self, gpio, level, tick):
      """
      Accumulate the 40 data bits.  Format into 5 bytes, humidity high,
      humidity low, temperature high, temperature low, checksum.
      """
      diff = pigpio.tickDiff(self.high_tick, tick)

      if level == 0:

         # Edge length determines if bit is 1 or 0.

         if diff >= 50:
            val = 1
            if diff >= 200: # Bad bit?
               self.CS = 256 # Force bad checksum.
         else:
            val = 0

         if self.bit >= 40: # Message complete.
            self.bit = 40

         elif self.bit >= 32: # In checksum byte.
            self.CS  = (self.CS<<1)  + val

            if self.bit == 39:

               # 40th bit received.

               self.pi.set_watchdog(self.gpio, 0)

               self.no_response = 0

               total = self.hH + self.hL + self.tH + self.tL

               if (total & 255) == self.CS: # Is checksum ok?

                  self.rhum = ((self.hH<<8) + self.hL) * 0.1

                  if self.tH & 128: # Negative temperature.
                     mult = -0.1
                     self.tH = self.tH & 127
                  else:
                     mult = 0.1

                  self.temp = ((self.tH<<8) + self.tL) * mult

                  self.tov = time.time()

                  if self.LED is not None:
                     self.pi.write(self.LED, 0)

               else:

                  self.bad_CS += 1

         elif self.bit >=24: # in temp low byte
            self.tL = (self.tL<<1) + val

         elif self.bit >=16: # in temp high byte
            self.tH = (self.tH<<1) + val

         elif self.bit >= 8: # in humidity low byte
            self.hL = (self.hL<<1) + val

         elif self.bit >= 0: # in humidity high byte
            self.hH = (self.hH<<1) + val

         else:               # header bits
            pass

         self.bit += 1

      elif level == 1:
         self.high_tick = tick
         if diff > 250000:
            self.bit = -2
            self.hH = 0
            self.hL = 0
            self.tH = 0
            self.tL = 0
            self.CS = 0

      else: # level == pigpio.TIMEOUT:
         self.pi.set_watchdog(self.gpio, 0)
         if self.bit < 8:       # Too few data bits received.
            self.bad_MM += 1    # Bump missing message count.
            self.no_response += 1
            if self.no_response > self.MAX_NO_RESPONSE:
               self.no_response = 0
               self.bad_SR += 1 # Bump sensor reset count.
               if self.power is not None:
                  self.powered = False
                  self.pi.write(self.power, 0)
                  time.sleep(2)
                  self.pi.write(self.power, 1)
                  time.sleep(2)
                  self.powered = True
         elif self.bit < 39:    # Short message receieved.
            self.bad_SM += 1    # Bump short message count.
            self.no_response = 0

         else:                  # Full message received.
            self.no_response = 0

   def temperature(self):
      """Return current temperature."""
      return self.temp

   def humidity(self):
      """Return current relative humidity."""
      return self.rhum

   def staleness(self):
      """Return time since measurement made."""
      if self.tov is not None:
         return time.time() - self.tov
      else:
         return -999

   def bad_checksum(self):
      """Return count of messages received with bad checksums."""
      return self.bad_CS

   def short_message(self):
      """Return count of short messages."""
      return self.bad_SM

   def missing_message(self):
      """Return count of missing messages."""
      return self.bad_MM

   def sensor_resets(self):
      """Return count of power cycles because of sensor hangs."""
      return self.bad_SR

   def trigger(self):
      """Trigger a new relative humidity and temperature reading."""
      if self.powered:
         if self.LED is not None:
            self.pi.write(self.LED, 1)

         self.pi.write(self.gpio, pigpio.LOW)
         time.sleep(0.017) # 17 ms
         self.pi.set_mode(self.gpio, pigpio.INPUT)
         self.pi.set_watchdog(self.gpio, 200)

   def cancel(self):
      """Cancel the DHT22 sensor."""

      self.pi.set_watchdog(self.gpio, 0)

      if self.cb != None:
         self.cb.cancel()
         self.cb = None

if __name__ == "__main__":

   import time

   import pigpio

   import DHT22

   # Intervals of about 2 seconds or less will eventually hang the DHT22.
   INTERVAL=3

   pi = pigpio.pi()

   s = DHT22.sensor(pi, 22, LED=16, power=8)

   r = 0

   next_reading = time.time()

   while True:

      r += 1

      s.trigger()

      time.sleep(0.2)

      print("{} {} {} {:3.2f} {} {} {} {}".format(
         r, s.humidity(), s.temperature(), s.staleness(),
         s.bad_checksum(), s.short_message(), s.missing_message(),
         s.sensor_resets()))

      next_reading += INTERVAL

      time.sleep(next_reading-time.time()) # Overall INTERVAL second polling.

   s.cancel()

   pi.stop()
dht22-a.png
dht22-a.png (29.93 KiB) Viewed 55058 times
dht22-b.png
dht22-b.png (29.78 KiB) Viewed 55058 times
dht22-c.png
dht22-c.png (28.01 KiB) Viewed 55058 times

Code: Select all

...
205 RH=64.6% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
206 RH=64.9% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
207 RH=65.5% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
208 RH=65.0% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
209 RH=64.6% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
210 RH=64.8% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
211 RH=64.9% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
212 RH=64.8% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
213 RH=64.4% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
214 RH=64.2% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
215 RH=64.3% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
216 RH=64.6% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
217 RH=64.8% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
218 RH=64.8% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
219 RH=64.8% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
220 RH=64.4% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
221 RH=64.3% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
222 RH=64.1% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
223 RH=64.5% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
224 RH=64.4% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
225 RH=64.4% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
226 RH=64.4% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
227 RH=64.0% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
228 RH=64.6% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
229 RH=64.5% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
230 RH=64.1% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
231 RH=64.1% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
232 RH=64.3% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
233 RH=64.2% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
234 RH=64.2% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
235 RH=64.6% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
236 RH=64.6% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
237 RH=64.4% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
238 RH=64.3% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
239 RH=64.5% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
240 RH=64.5% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
241 RH=64.4% T=23.5 C staleness=0.03 s bad CS=0 timed out=1
...
Last edited by joan on Sat Aug 02, 2014 1:27 pm, edited 7 times in total.

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

Re: Yet more DHT22 sensor code

Fri Mar 07, 2014 12:21 am

Nice Joan,

I will be able to free the SPI since I was using it to decode the DHT22 and pigpiod was already running.


B.T.W. I create a service with pigpiod.
ref:http://blog.scphillips.com/2013/07/gett ... e-on-boot/

Code: Select all

[email protected] ~ $ cat /etc/init.d/pigpiod
#!/bin/sh
 
### BEGIN INIT INFO
# Provides:          pigpiod
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: PI GPIO service with pwm 
# Description:       PI GPIO service with pwm 
### END INIT INFO
 
# Change the next 3 lines to suit where you install your script and what you want to call it
DIR=/usr/local/bin
DAEMON=$DIR/pigpiod
DAEMON_NAME=pigpiod
 
# This next line determines what user the script runs as.
# Root generally not recommended but necessary if you are using the Raspberry Pi GPIO from Python.
DAEMON_USER=root
 
# The process ID of the script when it runs is stored here:
PIDFILE=/var/run/$DAEMON_NAME.pid
 
. /lib/lsb/init-functions
 
do_start () {
    log_daemon_msg "Starting system $DAEMON_NAME daemon"
    start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON
    log_end_msg $?
}
do_stop () {
    log_daemon_msg "Stopping system $DAEMON_NAME daemon"
    start-stop-daemon --stop --pidfile $PIDFILE --retry 10
    log_end_msg $?
}
 
case "$1" in
 
    start|stop)
        do_${1}
        ;;
 
    restart|reload|force-reload)
        do_stop
        do_start
        ;;
 
    status)
        status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $?
        ;;
    *)
        echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|restart|status}"
        exit 1
        ;;
 
esac
exit 0
I made it executable

Code: Select all

sudo chmod 755 /etc/init.d/pigpiod
I insert it into the services

Code: Select all

cd /etc/init.d
sudo update-rc.d pigpiod defaults 99
I added into the service list

Code: Select all

complete -W "$(ls /etc/init.d/)" service
And finally start the service manually to see if it works.

Code: Select all

sudo service pigpiod start
Thank you ,
Daniel

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

Re: Yet more DHT22 sensor code

Fri Mar 07, 2014 7:51 am

Good work on the start-up script.

A note of caution on the DHT22. Mine has locked up a couple of times after several hours of reading. I couldn't find a programmatic way of resetting the DHT22 so pulled its plug. A Google shows that I'm not alone in having reliability problems. However there is a chance that the way I'm triggering the device reads causes the problem, or indeed my breadboard. I'll be interested in any reliability problems you have compared to SPI.

SPI does seem to have quite a few novel and unexpected uses.

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

Re: Yet more DHT22 sensor code

Fri Mar 07, 2014 7:36 pm

Maybe a pull up problem. I had my SPI system running for 3 weeks with no interruption and no false reading or hang. the system reads it every 5 minutes twice since the first read out is always the last state it was 5 minutes ago. A minimum of 1 second is needed between any new reading.

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

Re: Yet more DHT22 sensor code

Fri Mar 07, 2014 7:57 pm

I've just left it running today taking readings every 2 seconds. Just over 21,000 so far with 5 checksum failures, no timeouts, no freezes.

Yesterday I was mucking about experimenting with 3V3 and 5V and different resistors.
DHT22-log.png
5V (5K+10K resistor) reading every 2 seconds
DHT22-log.png (45.26 KiB) Viewed 54928 times

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

Re: Yet more DHT22 sensor code

Sat Mar 15, 2014 1:13 pm

I left the sensor and breadboard in an undisturbed place and left it running. I thought it had crashed but without thinking I had put a limit of 30000 samples (felt like a lot).

No checksum error, no timeouts, no freezes.
DHT22.py-2s.png
Powered from 3V3. Sample every 2 seconds.
DHT22.py-2s.png (44.86 KiB) Viewed 54728 times

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

Re: Yet more DHT22 sensor code

Sat Jun 21, 2014 1:20 am

Oops, something change in pigpio.

My service script didn't work anymore but I found a workaround.

replace the /etc/init.d/pigpiod by this one

Code: Select all

#!/bin/sh
 
### BEGIN INIT INFO
# Provides:          pigpiod
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: PI GPIO service with pwm 
# Description:       PI GPIO service with pwm 
### END INIT INFO
 
# Change the next 3 lines to suit where you install your script and what you want to call it
DIR=/usr/local/bin
DAEMON=$DIR/pigpiod
DAEMON_NAME=pigpiod
DAEMON_LOCK=pigpio
 
# This next line determines what user the script runs as.
# Root generally not recommended but necessary if you are using the Raspberry Pi GPIO from Python.
DAEMON_USER=root
 
# The process ID of the script when it runs is stored here:
PIDFILE=/var/run/$DAEMON_LOCK.pid
 
. /lib/lsb/init-functions
 
do_start () {
    log_daemon_msg "Starting system $DAEMON_NAME daemon"
#    start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON
    start-stop-daemon --start --background --pidfile $PIDFILE --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON
    log_end_msg $?
}
do_stop () {
    log_daemon_msg "Stopping system $DAEMON_NAME daemon"
    start-stop-daemon --stop --pidfile $PIDFILE --retry 10
    log_end_msg $?
}
 
case "$1" in
 
    start|stop)
        do_${1}
        ;;
 
    restart|reload|force-reload)
        do_stop
        do_start
        ;;
 
    status)
        status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $?
        ;;
    *)
        echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|restart|status}"
        exit 1
        ;;
 
esac
exit 0
This script will use /var/run/pigpio.pid create by /usr/local/bin/pigpiod and will use it to stop the service.

Daniel

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

Re: Yet more DHT22 sensor code

Sat Jun 21, 2014 7:27 am

Do you know what actually changed? I've never knowingly changed anything in this area since the daemon was introduced in version 3.

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

Re: Yet more DHT22 sensor code

Sat Jun 21, 2014 12:26 pm

Maybe it didn't change but the application Id didn't match the service one.
So I delete the lock from service and use the application lock.

And now it is working fine.

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

Re: Yet more DHT22 sensor code

Sat Jun 21, 2014 1:15 pm

As an aside I have a hypothesis about why the DHT22 locks up eventually with my code but not with SPI.

My code drives the line low as part of the trigger sequence and then turns the line into an input which allows it to pull-up to 3.3V. It seems possible that you need to actively drive the line high for a short period (40 micros) rather than just let it pull-up. I don't understand why that would be. Unfortunately this hypothesis can only be disproved.

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

Re: Yet more DHT22 sensor code

Sat Jun 21, 2014 3:40 pm

I notice something when I used pigpio in PWM mode , the R/C servo will glitch (making a small noise of quick movement) after a long period. Is it due to a counter going from 0xffffffff to 0x0? I really don't know. since I didn't look on how you did the pwm , it is just a theory.

I was reading a book and sudendly the R/C servo, hook to my webcam, make a moving sound , just a glitch. I notice it a couple of times. I should check the time interval and this could gives a clue about the problem.

Daniel

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

Re: Yet more DHT22 sensor code

Sat Jun 21, 2014 4:28 pm

danjperron wrote:I notice something when I used pigpio in PWM mode , the R/C servo will glitch (making a small noise of quick movement) after a long period. Is it due to a counter going from 0xffffffff to 0x0? I really don't know. since I didn't look on how you did the pwm , it is just a theory.

I was reading a book and sudendly the R/C servo, hook to my webcam, make a moving sound , just a glitch. I notice it a couple of times. I should check the time interval and this could gives a clue about the problem.

Daniel
It's not something I've noticed, but I rarely leave servos connected for extended periods, Do you have any feel for the periods involved?

There is the "check temperature to calculate RAM refresh rate" glitch twice a second (perhaps 8 micros each time). I have the check disabled but don't remember it causing problems for my servos in the past.

I'll leave a servo connected.

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

Re: Yet more DHT22 sensor code

Sat Jun 21, 2014 6:13 pm

Not really Just notice it when I read because i'm there.

I will try to see if I could create some kind of detector to get the glitch interval.

Daniel

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

Re: Yet more DHT22 sensor code

Sun Jun 22, 2014 2:02 am

B.T.W.

The sequence number when you create a service daemon is not taken in consideration any more. Wheezy use a dependency scheme.

If you want to use the sequence number, you will need to create /etc/init.d/.legacy-bootordering

Sequence number could be important if you create another daemon which use pigpiod. Be sure you put a number higher than the one you put for pigpiod.

Code: Select all

sudo touch /etc/init.d/.legacy-bootordering
update-rc.d pigpiod defaults 50
Sorry about that parentheses about pigpiod . But this is important enough if you want to use autostart pigpiod in service deamon.

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

Re: Yet more DHT22 sensor code

Sun Jun 22, 2014 7:23 am

danjperron wrote:B.T.W.

The sequence number when you create a service daemon is not taken in consideration any more. Wheezy use a dependency scheme.
...
OK, I think I understand now. This is the number which determines the order in which programs are started in /etc/init.d type scripts. So rather than prefix the priority to the script name you now have to use a different solution or use the legacy option.

I wonder how you are meant to fix the ordering in the new scheme of things.

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

Re: Yet more DHT22 sensor code

Sun Jun 22, 2014 8:13 am

You should put pullup resistor in the case of 3.3V supply too.
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 22, 2014 8:29 am

rgrbic wrote:You should put pullup resistor in the case of 3.3V supply too.
Yes, I always pull the output up to 3.3V, through a voltage divider if using 5V, otherwise just through a 5k resistor.

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

Re: Yet more DHT22 sensor code

Sun Jun 22, 2014 11:55 am

About the pull-up resistor,

I test a DHT11 and the slew rate of the rising slope is problematic.

I had to use 1K pull-up resistor for the signal to top high (to VCC) and get valid data.

Otherwise the signal is not going up enough.Looks like a saw wave signal and there is no valid data.

And the falling edge is ok.

I suspect a bad sensor. I will order another one because I don't think is in specification. Looks like it has a big capacitor couple on a base of a open collector transistor.



Daniel

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

Re: Yet more DHT22 sensor code

Fri Jun 27, 2014 9:51 am

I've been looking into preventing the DHT22 hanging for some time. Eventually I decided to power the sensor from a gpio so I could reset it when it stops responding.

By looking at the times I need to power cycle the sensor it has become apparent that there is a strong correlation with the time a 3kW appliance is switched on.

It seems not to be a software problem after all.

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

Re: Yet more DHT22 sensor code

Fri Jun 27, 2014 9:56 am

What kind of cable are you using for DHT22 and is this sensor near this appliance?
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

Fri Jun 27, 2014 10:14 am

rgrbic wrote:What kind of cable are you using for DHT22 and is this sensor near this appliance?
The DHT22 is plugged into breadboard. The appliance is about 20 feet away on a different ring.

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

Re: Yet more DHT22 sensor code

Fri Jun 27, 2014 10:33 am

joan wrote:
rgrbic wrote:What kind of cable are you using for DHT22 and is this sensor near this appliance?
The DHT22 is plugged into breadboard. The appliance is about 20 feet away on a different ring.
Put a capacitor between dht22 supply pins (Vcc and GND), like 0.1 uF
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

Fri Jun 27, 2014 10:47 am

I have a 104 cap between pins 1 and 4.

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

Re: Yet more DHT22 sensor code

Sat Jun 28, 2014 9:32 am

Try to put one more (electrolytic) capacitor between vcc and gnd.
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

Sat Jun 28, 2014 9:57 am

rgrbic wrote:Try to put one more (electrolytic) capacitor between vcc and gnd.
I've previously tried up to a 64 microfarad electrolytic. My main aim is to determine whether the software or the hardware is at fault. I'm 99% sure it's not a software problem.

Return to “Automation, sensing and robotics”