-
Notifications
You must be signed in to change notification settings - Fork 93
/
grove_temperature_humidity_sensor.py
178 lines (146 loc) · 5.24 KB
/
grove_temperature_humidity_sensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python
#
# This code is for
# Grove - Temperature & Humidity Sensor (DHT11)
# (https://www.seeedstudio.com/Grove-Temperature-Humidity-Sensor-DHT1-p-745.html)
# Grove - Temperature & Humidity Sensor Pro (AM2302)
# (https://www.seeedstudio.com/Grove-Temperature-Humidity-Sensor-Pro-AM230-p-838.html)
#
# which is consists of a capacitive sensor element used for measuring relative humidity
# and a negative temperature coefficient(NTC) thermistor used for measuring temperature.
#
# This is the library for Grove Base Hat which used to connect grove sensors for raspberry pi.
#
'''
'''
import RPi.GPIO as GPIO
# from grove.helper import *
def set_max_priority(): pass
def set_default_priority(): pass
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
PULSES_CNT = 41
class DHT(object):
DHT_TYPE = {
'DHT11': '11',
'DHT22': '22'
}
MAX_CNT = 320
def __init__(self, dht_type, pin):
self.pin = pin
if dht_type != self.DHT_TYPE['DHT11'] and dht_type != self.DHT_TYPE['DHT22']:
print('ERROR: Please use 11|22 as dht type.')
exit(1)
self.dht_type = dht_type
GPIO.setup(self.pin, GPIO.OUT)
self._last_temp = 0.0
self._last_humi = 0.0
@property
def dht_type(self):
return self._dht_type
@dht_type.setter
def dht_type(self, type):
self._dht_type = type
def _read(self):
# Send Falling signal to trigger sensor output data
# Wait for 20ms to collect 42 bytes data
GPIO.setup(self.pin, GPIO.OUT)
set_max_priority()
GPIO.output(self.pin, 1)
sleep(.2)
GPIO.output(self.pin, 0)
sleep(.018)
GPIO.setup(self.pin, GPIO.IN)
# a short delay needed
for i in range(10):
pass
# pullup by host 20-40 us
count = 0
while GPIO.input(self.pin):
count += 1
if count > self.MAX_CNT:
# print("pullup by host 20-40us failed")
set_default_priority()
return None, "pullup by host 20-40us failed"
pulse_cnt = [0] * (2 * PULSES_CNT)
fix_crc = False
for i in range(0, PULSES_CNT * 2, 2):
while not GPIO.input(self.pin):
pulse_cnt[i] += 1
if pulse_cnt[i] > self.MAX_CNT:
# print("pulldown by DHT timeout %d" % i)
set_default_priority()
return None, "pulldown by DHT timeout %d" % i
while GPIO.input(self.pin):
pulse_cnt[i + 1] += 1
if pulse_cnt[i + 1] > self.MAX_CNT:
# print("pullup by DHT timeout %d" % (i + 1))
if i == (PULSES_CNT - 1) * 2:
# fix_crc = True
# break
pass
set_default_priority()
return None, "pullup by DHT timeout %d" % i
# back to normal priority
set_default_priority()
total_cnt = 0
for i in range(2, 2 * PULSES_CNT, 2):
total_cnt += pulse_cnt[i]
# Low level ( 50 us) average counter
average_cnt = total_cnt / (PULSES_CNT - 1)
# print("low level average loop = %d" % average_cnt)
data = ''
for i in range(3, 2 * PULSES_CNT, 2):
if pulse_cnt[i] > average_cnt:
data += '1'
else:
data += '0'
data0 = int(data[ 0: 8], 2)
data1 = int(data[ 8:16], 2)
data2 = int(data[16:24], 2)
data3 = int(data[24:32], 2)
data4 = int(data[32:40], 2)
if fix_crc and data4 != ((data0 + data1 + data2 + data3) & 0xFF):
data4 = data4 ^ 0x01
data = data[0: PULSES_CNT - 2] + ('1' if data4 & 0x01 else '0')
if data4 == ((data0 + data1 + data2 + data3) & 0xFF):
if self._dht_type == self.DHT_TYPE['DHT11']:
humi = int(data0)
temp = int(data2)
elif self._dht_type == self.DHT_TYPE['DHT22']:
humi = float(int(data[ 0:16], 2)*0.1)
temp = float(int(data[17:32], 2)*0.2*(0.5-int(data[16], 2)))
else:
# print("checksum error!")
return None, "checksum error!"
return humi, temp
def read(self, retries = 15):
for i in range(retries):
humi, temp = self._read()
if not humi is None:
break
if humi is None:
return self._last_humi, self._last_temp
self._last_humi,self._last_temp = humi, temp
return humi, temp
Grove = DHT
def main():
from grove.helper import SlotHelper
sh = SlotHelper(SlotHelper.GPIO)
pin = sh.argv2pin(" [dht_type]")
import sys
typ = '11'
if len(sys.argv) >= 3:
typ = sys.argv[2]
import time
sensor = DHT(typ, pin)
while True:
humi, temp = sensor.read()
if not humi is None:
print('DHT{0}, humidity {1:.1f}%, temperature {2:.1f}*'.format(sensor.dht_type, humi, temp))
else:
print('DHT{0}, humidity & temperature: {1}'.format(sensor.dht_type, temp))
time.sleep(1)
if __name__ == '__main__':
main()