dht11.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 from __future__ import absolute_import, division, print_function, unicode_literals
5 
6 import time
7 import pigpio
8 
9 
10 class DHT11(object):
11  """
12  The DHT11 class is a stripped version of the DHT22 sensor code by joan2937.
13  You can find the initial implementation here:
14  - https://github.com/srounet/pigpio/tree/master/EXAMPLES/Python/DHT22_AM2302_SENSOR
15 
16  example code:
17  >>> pi = pigpio.pi()
18  >>> sensor = DHT11(pi, 4) # 4 is the data GPIO pin connected to your sensor
19  >>> for response in sensor:
20  .... print("Temperature: {}".format(response['temperature']))
21  .... print("Humidity: {}".format(response['humidity']))
22  """
23 
24  def __init__(self, pi, gpio):
25  """
26  pi (pigpio): an instance of pigpio
27  gpio (int): gpio pin number
28  """
29  self.pi = pi
30  self.gpio = gpio
31  self.high_tick = 0
32  self.bit = 40
33  self.temperature = 0
34  self.humidity = 0
35  self.either_edge_cb = None
36  self.setup()
37 
38  def setup(self):
39  """
40  Clears the internal gpio pull-up/down resistor.
41  Kills any watchdogs.
42  """
43  self.pi.set_pull_up_down(self.gpio, pigpio.PUD_OFF)
44  self.pi.set_watchdog(self.gpio, 0)
45  self.register_callbacks()
46 
47  def register_callbacks(self):
48  """
49  Monitors RISING_EDGE changes using callback.
50  """
51  self.either_edge_cb = self.pi.callback(
52  self.gpio,
53  pigpio.EITHER_EDGE,
55  )
56 
57  def either_edge_callback(self, gpio, level, tick):
58  """
59  Either Edge callbacks, called each time the gpio edge changes.
60  Accumulate the 40 data bits from the dht11 sensor.
61  """
62  level_handlers = {
63  pigpio.FALLING_EDGE: self._edge_FALL,
64  pigpio.RISING_EDGE: self._edge_RISE,
65  pigpio.EITHER_EDGE: self._edge_EITHER
66  }
67  handler = level_handlers[level]
68  diff = pigpio.tickDiff(self.high_tick, tick)
69  handler(tick, diff)
70 
71  def _edge_RISE(self, tick, diff):
72  """
73  Handle Rise signal.
74  """
75  val = 0
76  if diff >= 50:
77  val = 1
78  if diff >= 200: # Bad bit?
79  self.checksum = 256 # Force bad checksum
80 
81  if self.bit >= 40: # Message complete
82  self.bit = 40
83  elif self.bit >= 32: # In checksum byte
84  self.checksum = (self.checksum << 1) + val
85  if self.bit == 39:
86  # 40th bit received
87  self.pi.set_watchdog(self.gpio, 0)
88  total = self.humidity + self.temperature
89  # is checksum ok ?
90  if not (total & 255) == self.checksum:
91  raise
92  elif 16 <= self.bit < 24: # in temperature byte
93  self.temperature = (self.temperature << 1) + val
94  elif 0 <= self.bit < 8: # in humidity byte
95  self.humidity = (self.humidity << 1) + val
96  else: # skip header bits
97  pass
98  self.bit += 1
99 
100  def _edge_FALL(self, tick, diff):
101  """
102  Handle Fall signal.
103  """
104  self.high_tick = tick
105  if diff <= 250000:
106  return
107  self.bit = -2
108  self.checksum = 0
109  self.temperature = 0
110  self.humidity = 0
111 
112  def _edge_EITHER(self, tick, diff):
113  """
114  Handle Either signal.
115  """
116  self.pi.set_watchdog(self.gpio, 0)
117 
118  def read(self):
119  """
120  Start reading over DHT11 sensor.
121  """
122  self.pi.write(self.gpio, pigpio.LOW)
123  time.sleep(0.017) # 17 ms
124  self.pi.set_mode(self.gpio, pigpio.INPUT)
125  self.pi.set_watchdog(self.gpio, 200)
126  time.sleep(0.2)
127 
128  def close(self):
129  """
130  Stop reading sensor, remove callbacks.
131  """
132  self.pi.set_watchdog(self.gpio, 0)
133  if self.either_edge_cb:
134  self.either_edge_cb.cancel()
135  self.either_edge_cb = None
136 
137  def __iter__(self):
138  """
139  Support the iterator protocol.
140  """
141  return self
142 
143  def next(self):
144  """
145  Call the read method and return temperature and humidity informations.
146  """
147  self.read()
148  response = {
149  'humidity': self.humidity,
150  'temperature': self.temperature
151  }
152  return response
153 
154 
155 if __name__ == '__main__':
156  pi = pigpio.pi()
157  sensor = DHT11(pi, 4)
158  for d in sensor:
159  print("temperature: {}".format(d['temperature']))
160  print("humidity: {}".format(d['humidity']))
161  time.sleep(1)
162  sensor.close()
163 
dht11.DHT11.__init__
def __init__(self, pi, gpio)
Definition: dht11.py:24
dht11.DHT11.close
def close(self)
Definition: dht11.py:128
dht11.DHT11.__iter__
def __iter__(self)
Definition: dht11.py:137
dht11.DHT11.either_edge_callback
def either_edge_callback(self, gpio, level, tick)
Definition: dht11.py:57
dht11.DHT11.humidity
humidity
Definition: dht11.py:34
dht11.DHT11._edge_FALL
def _edge_FALL(self, tick, diff)
Definition: dht11.py:100
pigpio.pi
Definition: pigpio.py:1318
set_mode
int set_mode(unsigned gpio, unsigned mode)
Definition: pigpiod_if.c:568
dht11.DHT11.bit
bit
Definition: dht11.py:32
dht11.DHT11._edge_RISE
def _edge_RISE(self, tick, diff)
Definition: dht11.py:71
dht11.DHT11.next
def next(self)
Definition: dht11.py:143
dht11.DHT11
Definition: dht11.py:10
callback
void callback(uint32_t hash)
Definition: test_ir_hasher.c:23
dht11.DHT11.high_tick
high_tick
Definition: dht11.py:31
pigpio.tickDiff
def tickDiff(t1, t2)
Definition: pigpio.py:911
dht11.DHT11.temperature
temperature
Definition: dht11.py:33
dht11.DHT11.setup
def setup(self)
Definition: dht11.py:38
dht11.DHT11.read
def read(self)
Definition: dht11.py:118
dht11.DHT11.pi
pi
Definition: dht11.py:29
dht11.DHT11._edge_EITHER
def _edge_EITHER(self, tick, diff)
Definition: dht11.py:112
dht11.DHT11.either_edge_cb
either_edge_cb
Definition: dht11.py:35
dht11.DHT11.checksum
checksum
Definition: dht11.py:79
set_watchdog
int set_watchdog(unsigned user_gpio, unsigned timeout)
Definition: pigpiod_if.c:622
dht11.DHT11.register_callbacks
def register_callbacks(self)
Definition: dht11.py:47
set_pull_up_down
int set_pull_up_down(unsigned gpio, unsigned pud)
Definition: pigpiod_if.c:574
dht11.DHT11.gpio
gpio
Definition: dht11.py:30


cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Fri Aug 2 2024 09:40:56