-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDHT11.py
78 lines (68 loc) · 1.76 KB
/
DHT11.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
#!/usr/bin/python3
#-*- coding:utf-8 -*-
import RPi.GPIO as GPIO
import time
DHT11_DATA_LEN = 5
class DHT11(object):
"""docstring for DHT11"""
def __init__(self, datapin=24, powerpin=25):
GPIO.setmode(GPIO.BOARD)
self.pin = datapin
GPIO.setwarnings(False)
count=0
def _read_data(self):
self.data=[]
# reset
GPIO.setup( self.pin , GPIO.OUT)
GPIO.output( self.pin , GPIO.LOW)
time.sleep(0.03) # 必须大于18ms
GPIO.setup( self.pin , GPIO.IN)
count=0
while GPIO.input( self.pin ) == GPIO.HIGH:
continue
while GPIO.input( self.pin ) == GPIO.LOW:
continue
# 固定拉高80us,可用来做基准
while GPIO.input( self.pin ) == GPIO.HIGH:
count += 1
continue
base = count / 2
# Get data
while len(self.data)< DHT11_DATA_LEN*8:
i = 0
# 检测50us以上的低位
while GPIO.input( self.pin ) == GPIO.LOW:
continue
# 此时电平为高,持续26-28us表示0,否则持续70us表示1
while GPIO.input( self.pin ) == GPIO.HIGH:
i += 1
if i > 100: #防止死循环
break
if i < base:
self.data.append(0)
else:
self.data.append(1)
#print("DHT11 get data: ", self.data)
def _cal(self):
res=[]
for i in range( DHT11_DATA_LEN ): # 5个数据分别为 湿度(整数,小数);温度(整数,小数),校验
res.append(0)
for j in range(8):
res[i] += self.data[i*8+j]<<(7-j)
#print("DHT11 res: ", res)
if res[0]+res[2]!=res[4]: # 数据校验
print("DHT11: data check error!")
return -1, -1
else:
# print("humidity: %d, temperature: %d C" %(res[0], res[2]))
return res[0],res[2]
def get(self):
for i in range(5):
self._read_data()
(h, t)=self._cal()
if h>0:
break
time.sleep(0.2)
return h,t
def __exit__(self):
GPIO.cleanup()