forked from alankrantas/pxt-DHT11_DHT22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dht11_dht22.ts
140 lines (122 loc) · 4.8 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
/**
* MakeCode editor extension for DHT11 and DHT22 humidity/temperature sensors
* by Alan Wang
*/
//% block="DHT11/DHT22" weight=100 color=#ff8f3f icon="\uf043"
namespace dht11_dht22 {
let _temperature: number = -999.0
let _humidity: number = -999.0
let _readSuccessful: 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[] = []
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
startTime = input.runningTimeMicros()
//request data
pins.digitalWritePin(dataPin, 0) //begin protocol
basic.pause(18)
if (pullUp) pins.setPull(dataPin, PinPullMode.PullUp) //pull up data pin if needed
pins.digitalReadPin(dataPin)
control.waitMicros(20)
while (pins.digitalReadPin(dataPin) == 1);
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 pull up data pin for more than 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
}
}
//serial output
if (serialOtput) {
let DHTstr: string = ""
if (DHT == DHTtype.DHT11) DHTstr = "DHT11"
else DHTstr = "DHT22"
serial.writeLine(DHTstr + " query completed in " + (endTime - startTime) + " microseconds")
if (_readSuccessful) {
serial.writeLine("Checksum ok")
serial.writeLine("Humidity: " + _humidity + " %")
serial.writeLine("Temperature: " + _temperature + " *C")
} 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
}
/**
* Determind if last query is successful (checksum ok)
*/
//% block="Last query successful?"
export function readDataSuccessful(): boolean {
return _readSuccessful
}
}
enum DHTtype {
//% block="DHT11"
DHT11,
//% block="DHT22"
DHT22,
}
enum dataType {
//% block="humidity"
humidity,
//% block="temperature"
temperature,
}