DHT22 stops reading after few hours script change

Python and python framework

Moderator: leecollings

Post Reply
badabustyhh
Posts: 7
Joined: Monday 06 July 2020 14:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

DHT22 stops reading after few hours script change

Post by badabustyhh »

Hey guys,

i got the dht22 running with a switch in domoticz, works fine but after 6 hours it stops working and data goes to 0
i googled and found a solution (at least thats whatt i think) but im not able to conect the scripts :?: This seems to be the solution
As fas as I am aware this is a fairly well known problem with the DHT22 (models such as the DHT11 do not seem to be affected).

After a random amount of time which can vary between seconds and months they stop responding. The only solution I am aware of is a power cycle.

A solution I have used is to power the DHT22 from a Pi GPIO. The GPIO can supply the limited current needed and is within the device specs of 3 to 5 volts. If I detect a number of consecutive timeouts I switch the GPIO off then on again a few seconds later.

An example of such code is my pigpio Python module DHT22.
https://raspberrypi.stackexchange.com/q ... uple-hours

------------------
This is my current script

Code: Select all

#!/usr/bin/python
import sys
import Adafruit_DHT
import urllib
#from urllib import request

# parameters
DHT_type    = 22
OneWire_pin = 4 
sensor_idx  = 1
url_json    = "http://192.168.0.10:8080/json.htm?type=command&param=udevice&idx="
verbose     = 1  # set to 1 to print out information to the console 

# read dht11 temperature and humidity
humidity, temperature = Adafruit_DHT.read_retry(DHT_type, OneWire_pin)

# use Domoticz JSON url to update
cmd = url_json  + str(sensor_idx) + "&nvalue=0&svalue=" + str(temperature) + ";" + str(humidity) + ";0"
hf = urllib.urlopen(cmd)
if verbose > 0:
  print( 'Sensor data: temperature={0:0.1f}C, humidity={1:0.1f}%'.format(temperature, humidity))
  print( 'Uploaded to Pi: ' + cmd)
hf.close
and this the one where it resets the power after false readings

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()


I dont know if its easy for somebody to connect it i tried for 2 days but im pretty lost, Thanks in advance for any help or further info.

pace and luv
badabustyhh
Posts: 7
Joined: Monday 06 July 2020 14:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DHT22 stops reading after few hours script change

Post by badabustyhh »

sorry i think this is the scipt im using to update the data

Code: Select all

#!/usr/bin/python
# coding: utf-8
import sys
import Adafruit_DHT
import urllib
#from urllib import request

# parameters
DHT_type      = 22
OneWire_pin   = 4
room_temp_idx = 1
url_json      = "http://192.168.0.10:8080/json.htm?type=command&param=udevice&idx="
verbose       = 1 # 1 to print out information to the console, 0 for silence

# read and report dht11 temperature and humidity
humidity, temperature = Adafruit_DHT.read_retry(DHT_type, OneWire_pin)
cmd = url_json  + str(room_temp_idx) + "&nvalue=0&svalue=" + str(temperature) + ";" + str(humidity) + ";0"
hf = urllib.urlopen(cmd)
if verbose > 0:
  print( 'Sensor data: temperature {0:0.1f}°C, humidiy {1:0.1f}%'.format(temperature, humidity))
  print( 'URL JSON pour Domoticz: ' + cmd)
hf.close
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest