dht11.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 
00004 from __future__ import absolute_import, division, print_function, unicode_literals
00005 
00006 import time
00007 import pigpio
00008 
00009 
00010 class DHT11(object):
00011     """
00012     The DHT11 class is a stripped version of the DHT22 sensor code by joan2937.
00013     You can find the initial implementation here:
00014     - https://github.com/srounet/pigpio/tree/master/EXAMPLES/Python/DHT22_AM2302_SENSOR
00015 
00016     example code:
00017     >>> pi = pigpio.pi()
00018     >>> sensor = DHT11(pi, 4) # 4 is the data GPIO pin connected to your sensor
00019     >>> for response in sensor:
00020     ....    print("Temperature: {}".format(response['temperature']))
00021     ....    print("Humidity: {}".format(response['humidity']))
00022     """
00023 
00024     def __init__(self, pi, gpio):
00025         """
00026         pi (pigpio): an instance of pigpio
00027         gpio (int): gpio pin number
00028         """
00029         self.pi = pi
00030         self.gpio = gpio
00031         self.high_tick = 0
00032         self.bit = 40
00033         self.temperature = 0
00034         self.humidity = 0
00035         self.either_edge_cb = None
00036         self.setup()
00037 
00038     def setup(self):
00039         """
00040         Clears the internal gpio pull-up/down resistor.
00041         Kills any watchdogs.
00042         """
00043         self.pi.set_pull_up_down(self.gpio, pigpio.PUD_OFF)
00044         self.pi.set_watchdog(self.gpio, 0)
00045         self.register_callbacks()
00046 
00047     def register_callbacks(self):
00048         """
00049         Monitors RISING_EDGE changes using callback.
00050         """
00051         self.either_edge_cb = self.pi.callback(
00052             self.gpio,
00053             pigpio.EITHER_EDGE,
00054             self.either_edge_callback
00055         )
00056 
00057     def either_edge_callback(self, gpio, level, tick):
00058         """
00059         Either Edge callbacks, called each time the gpio edge changes.
00060         Accumulate the 40 data bits from the dht11 sensor.
00061         """
00062         level_handlers = {
00063             pigpio.FALLING_EDGE: self._edge_FALL,
00064             pigpio.RISING_EDGE: self._edge_RISE,
00065             pigpio.EITHER_EDGE: self._edge_EITHER
00066         }
00067         handler = level_handlers[level]
00068         diff = pigpio.tickDiff(self.high_tick, tick)
00069         handler(tick, diff)
00070 
00071     def _edge_RISE(self, tick, diff):
00072         """
00073         Handle Rise signal.
00074         """
00075         val = 0
00076         if diff >= 50:
00077             val = 1
00078         if diff >= 200: # Bad bit?
00079             self.checksum = 256 # Force bad checksum
00080 
00081         if self.bit >= 40: # Message complete
00082             self.bit = 40
00083         elif self.bit >= 32: # In checksum byte
00084             self.checksum = (self.checksum << 1) + val
00085             if self.bit == 39:
00086                 # 40th bit received
00087                 self.pi.set_watchdog(self.gpio, 0)
00088                 total = self.humidity + self.temperature
00089                 # is checksum ok ?
00090                 if not (total & 255) == self.checksum:
00091                     raise
00092         elif 16 <= self.bit < 24: # in temperature byte
00093             self.temperature = (self.temperature << 1) + val
00094         elif 0 <= self.bit < 8: # in humidity byte
00095             self.humidity = (self.humidity << 1) + val
00096         else: # skip header bits
00097             pass
00098         self.bit += 1
00099 
00100     def _edge_FALL(self, tick, diff):
00101         """
00102         Handle Fall signal.
00103         """
00104         self.high_tick = tick
00105         if diff <= 250000:
00106             return
00107         self.bit = -2
00108         self.checksum = 0
00109         self.temperature = 0
00110         self.humidity = 0
00111 
00112     def _edge_EITHER(self, tick, diff):
00113         """
00114         Handle Either signal.
00115         """
00116         self.pi.set_watchdog(self.gpio, 0)
00117 
00118     def read(self):
00119         """
00120         Start reading over DHT11 sensor.
00121         """
00122         self.pi.write(self.gpio, pigpio.LOW)
00123         time.sleep(0.017) # 17 ms
00124         self.pi.set_mode(self.gpio, pigpio.INPUT)
00125         self.pi.set_watchdog(self.gpio, 200)
00126         time.sleep(0.2)
00127 
00128     def close(self):
00129         """
00130         Stop reading sensor, remove callbacks.
00131         """
00132         self.pi.set_watchdog(self.gpio, 0)
00133         if self.either_edge_cb:
00134             self.either_edge_cb.cancel()
00135             self.either_edge_cb = None
00136 
00137     def __iter__(self):
00138         """
00139         Support the iterator protocol.
00140         """
00141         return self
00142 
00143     def next(self):
00144         """
00145         Call the read method and return temperature and humidity informations.
00146         """
00147         self.read()
00148         response =  {
00149             'humidity': self.humidity,
00150             'temperature': self.temperature
00151         }
00152         return response
00153 
00154 
00155 if __name__ == '__main__':
00156     pi = pigpio.pi()
00157     sensor = DHT11(pi, 4)
00158     for d in sensor:
00159         print("temperature: {}".format(d['temperature']))
00160         print("humidity: {}".format(d['humidity']))
00161         time.sleep(1)
00162     sensor.close()
00163 


cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Thu Jun 6 2019 20:43:57