12 A class to read relative humidity and temperature from the 13 DHT22 sensor. The sensor is also known as the AM2302. 15 The sensor can be powered from the Pi 3V3 or the Pi 5V rail. 17 Powering from the 3V3 rail is simpler and safer. You may need 18 to power from 5V if the sensor is connected via a long cable. 20 For 3V3 operation connect pin 1 to 3V3 and pin 4 to ground. 22 Connect pin 2 to a gpio. 24 For 5V operation connect pin 1 to 5V and pin 4 to ground. 26 The following pin 2 connection works for me. Use at YOUR OWN RISK. 28 5V--5K_resistor--+--10K_resistor--Ground 35 def __init__(self, pi, gpio, LED=None, power=None):
37 Instantiate with the Pi and gpio to which the DHT22 output 40 Optionally a LED may be specified. This will be blinked for 41 each successful reading. 43 Optionally a gpio used to power the sensor may be specified. 44 This gpio will be set high to power the sensor. If the sensor 45 locks it will be power cycled to restart the readings. 47 Taking readings more often than about once every two seconds will 48 eventually cause the DHT22 to hang. A 3 second interval seems OK. 64 atexit.register(self.
cancel)
83 pi.set_pull_up_down(gpio, pigpio.PUD_OFF)
85 pi.set_watchdog(gpio, 0)
87 self.
cb = pi.callback(gpio, pigpio.EITHER_EDGE, self.
_cb)
89 def _cb(self, gpio, level, tick):
91 Accumulate the 40 data bits. Format into 5 bytes, humidity high, 92 humidity low, temperature high, temperature low, checksum. 111 self.
CS = (self.
CS << 1) + val
117 self.pi.set_watchdog(self.
gpio, 0)
121 total = self.
hH + self.
hL + self.
tH + self.
tL 123 if (total & 255) == self.
CS:
125 self.
rhum = ((self.
hH << 8) + self.
hL) * 0.1
133 self.
temp = ((self.
tH << 8) + self.
tL) * mult
135 self.
tov = time.time()
137 if self.
LED is not None:
138 self.pi.write(self.
LED, 0)
148 self.
tH = (self.
tH << 1) + val
172 self.pi.set_watchdog(self.
gpio, 0)
179 if self.
power is not None:
181 self.pi.write(self.
power, 0)
183 self.pi.write(self.
power, 1)
194 """Return current temperature.""" 198 """Return current relative humidity.""" 202 """Return time since measurement made.""" 203 if self.
tov is not None:
204 return time.time() - self.
tov 209 """Return count of messages received with bad checksums.""" 213 """Return count of short messages.""" 217 """Return count of missing messages.""" 221 """Return count of power cycles because of sensor hangs.""" 225 """Trigger a new relative humidity and temperature reading.""" 227 if self.
LED is not None:
228 self.pi.write(self.
LED, 1)
230 self.pi.write(self.
gpio, pigpio.LOW)
232 self.pi.set_mode(self.
gpio, pigpio.INPUT)
233 self.pi.set_watchdog(self.
gpio, 200)
236 """Cancel the DHT22 sensor.""" 238 self.pi.set_watchdog(self.
gpio, 0)
240 if self.
cb is not None:
244 if __name__ ==
"__main__":
261 next_reading = time.time()
271 print(
"{} {} {} {:3.2f} {} {} {} {}".format(
272 r, s.humidity(), s.temperature(), s.staleness(),
273 s.bad_checksum(), s.short_message(), s.missing_message(),
276 next_reading += INTERVAL
278 time.sleep(next_reading-time.time())
def missing_message(self)
def __init__(self, pi, gpio, LED=None, power=None)
def _cb(self, gpio, level, tick)