This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdht11_dht22.ts
181 lines (152 loc) · 6.07 KB
/
dht11_dht22.ts
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
178
179
180
181
/**
* MakeCode editor extension for DHT11 and DHT22 humidity/temperature sensors
* by Alan Wang
*/
enum DHTtype {
//% block="DHT11"
DHT11,
//% block="DHT22"
DHT22,
}
enum dataType {
//% block="humidity"
humidity,
//% block="temperature"
temperature,
}
enum tempType {
//% block="Celsius (*C)"
celsius,
//% block="Fahrenheit (*F)"
fahrenheit,
}
//% block="DHT11/DHT22" weight=100 color=#ff8f3f icon="\uf043"
namespace dht11_dht22 {
let _temperature: number = -999.0
let _humidity: number = -999.0
let _temptype: tempType = tempType.celsius
let _readSuccessful: boolean = false
let _sensorresponding: boolean = false
/**
* Query data from DHT11/DHT22 sensor. If you are using 4 pins/no PCB board versions, you'll need to pull up the data pin.
* It is also recommended to wait 1 (DHT11) or 2 (DHT22) seconds between each query.
*/
//% block="Query $DHT|Data pin $dataPin|Pin pull up $pullUp|Serial output $serialOtput|Wait 2 sec after query $wait"
//% pullUp.defl=true
//% serialOtput.defl=false
//% wait.defl=true
//% blockExternalInputs=true
export function queryData(DHT: DHTtype, dataPin: DigitalPin, pullUp: boolean, serialOtput: boolean, wait: boolean) {
//initialize
let startTime: number = 0
let endTime: number = 0
let checksum: number = 0
let checksumTmp: number = 0
let dataArray: boolean[] = []
let resultArray: number[] = []
let DHTstr: string = (DHT == DHTtype.DHT11) ? "DHT11" : "DHT22"
for (let index = 0; index < 40; index++) dataArray.push(false)
for (let index = 0; index < 5; index++) resultArray.push(0)
_humidity = -999.0
_temperature = -999.0
_readSuccessful = false
_sensorresponding = false
startTime = input.runningTimeMicros()
//request data
pins.digitalWritePin(dataPin, 0) //begin protocol, pull down pin
basic.pause(18)
if (pullUp) pins.setPull(dataPin, PinPullMode.PullUp) //pull up data pin if needed
pins.digitalReadPin(dataPin) //pull up pin
control.waitMicros(40)
if (pins.digitalReadPin(dataPin) == 1) {
if (serialOtput) {
serial.writeLine(DHTstr + " not responding!")
serial.writeLine("----------------------------------------")
}
} else {
_sensorresponding = true
while (pins.digitalReadPin(dataPin) == 0); //sensor response
while (pins.digitalReadPin(dataPin) == 1); //sensor response
//read data (5 bytes)
for (let index = 0; index < 40; index++) {
while (pins.digitalReadPin(dataPin) == 1);
while (pins.digitalReadPin(dataPin) == 0);
control.waitMicros(28)
//if sensor still pull up data pin after 28 us it means 1, otherwise 0
if (pins.digitalReadPin(dataPin) == 1) dataArray[index] = true
}
endTime = input.runningTimeMicros()
//convert byte number array to integer
for (let index = 0; index < 5; index++)
for (let index2 = 0; index2 < 8; index2++)
if (dataArray[8 * index + index2]) resultArray[index] += 2 ** (7 - index2)
//verify checksum
checksumTmp = resultArray[0] + resultArray[1] + resultArray[2] + resultArray[3]
checksum = resultArray[4]
if (checksumTmp >= 512) checksumTmp -= 512
if (checksumTmp >= 256) checksumTmp -= 256
if (checksum == checksumTmp) _readSuccessful = true
//read data if checksum ok
if (_readSuccessful) {
if (DHT == DHTtype.DHT11) {
//DHT11
_humidity = resultArray[0] + resultArray[1] / 100
_temperature = resultArray[2] + resultArray[3] / 100
} else {
//DHT22
let temp_sign: number = 1
if (resultArray[2] >= 128) {
resultArray[2] -= 128
temp_sign = -1
}
_humidity = (resultArray[0] * 256 + resultArray[1]) / 10
_temperature = (resultArray[2] * 256 + resultArray[3]) / 10 * temp_sign
}
if (_temptype == tempType.fahrenheit)
_temperature = _temperature * 9 / 5 + 32
}
//serial output
if (serialOtput) {
serial.writeLine(DHTstr + " query completed in " + (endTime - startTime) + " microseconds")
if (_readSuccessful) {
serial.writeLine("Checksum ok")
serial.writeLine("Humidity: " + _humidity + " %")
serial.writeLine("Temperature: " + _temperature + (_temptype == tempType.celsius ? " *C" : " *F"))
} else {
serial.writeLine("Checksum error")
}
serial.writeLine("----------------------------------------")
}
}
//wait 2 sec after query if needed
if (wait) basic.pause(2000)
}
/**
* Read humidity/temperature data from lastest query of DHT11/DHT22
*/
//% block="Read $data"
export function readData(data: dataType): number {
return data == dataType.humidity ? _humidity : _temperature
}
/**
* Select temperature type (Celsius/Fahrenheit)"
*/
//% block="Temperature type: $temp" advanced=true
export function selectTempType(temp: tempType) {
_temptype = temp
}
/**
* Determind if last query is successful (checksum ok)
*/
//% block="Last query successful?"
export function readDataSuccessful(): boolean {
return _readSuccessful
}
/**
* Determind if sensor responded successfully (not disconnected, etc) in last query
*/
//% block="Last query sensor responding?" advanced=true
export function sensorrResponding(): boolean {
return _sensorresponding
}
}