-
Notifications
You must be signed in to change notification settings - Fork 0
/
dht11.py
112 lines (96 loc) · 3.28 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
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
import time
from machine import Pin
MAXTIMINGS = 85
DHT11 = 11
DHT22 = 22
DHT21 = 21
AM2301 = 21
class DHT(object):
def __init__(self, data_pin,Type=DHT11):
self.Data_pin = data_pin
self.__pinData = Pin(data_pin, Pin.OUT)
self.firstreading = True
self.__pinData.value(1)
self._lastreadtime = 0
self.data=[0]*5
self.temp = 0
self.humid = 0
def read(self):
i=0
j=0
self.__pinData.value(1)
#time.sleep(0.25)
self.data[0] = self.data[1] = self.data[2] = self.data[3] = self.data[4] = 0
# now pull it low for ~20 milliseconds
pinData = Pin(self.Data_pin, Pin.OUT, None)
pinData.value(0)
time.sleep_ms(20)
pinData.value(1)
time.sleep_us(41)
pinData = Pin(self.Data_pin, Pin.IN)
DHT11_TIMEOUT = -1
time_cnt=0
while(0 ==pinData.value()):
time.sleep_us(5)
time_cnt = time_cnt+1
if(time_cnt > 16):
return
#DHT11将总线拉高至少80us,为发送传感器数据做准备。
time_cnt=0
while(1 == pinData.value()):
time.sleep_us(5)
time_cnt = time_cnt+1
if(time_cnt > 16):
return
for j in range(5):
i = 0
result=0
PINC = 1
for i in range(8):
while(not (PINC & pinData.value())): # wait for 50us
pass
#print('wait 50us')
time.sleep_us(25)
if(PINC & pinData.value()):
result |=(1<<(7-i))
while(PINC & pinData.value()): # wait '0' finish
time.sleep_us(5)
time_cnt = time_cnt+1
if(time_cnt > 20):
return
#print('wait 1')
self.data[j] = result
pinData = Pin(self.Data_pin, Pin.OUT, None)
pinData.value(1)
dht11_check_sum = (self.data[0]+self.data[1]+self.data[2]+self.data[3]&0xff)
# check check_sum
if(self.data[4] is not dht11_check_sum):
print("DHT11 checksum error")
#print(self.data)
if ((j >= 4) and ( self.data[4] == dht11_check_sum)):
return True
return False
def readHumidity(self):
if (self.read()):
self.humid = float(self.data[0])
self.humid = self.humid + float(self.data[1]/10)
return self.humid
def readTemperature(self):
if (self.read()):
self.temp = float(self.data[2])
self.temp = self.temp + float(self.data[3]/10)
return self.temp
def readTempHumid(self):
if (self.read()):
self.temp = float(self.data[2])
self.temp = self.temp + float(self.data[3]/10)
self.humid = float(self.data[0])
self.humid = self.humid + float(self.data[1]/10)
return self.temp , self.humid
'''
dht = DHT(6)
for i in range(100):
#dht.read()
dht.readHumidity()
dht.readTemperature()
'''