-
Notifications
You must be signed in to change notification settings - Fork 0
/
dht11.ts
128 lines (113 loc) · 3.68 KB
/
dht11.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
/**
* Use this file to define custom functions and blocks.
* Read more at https://maker.makecode.com/blocks/custom
*/
// type DigitalPin = DigitalInOutPin
class DHT11 {
/* Copyright (C) 2014 Spence Konde. See the file LICENSE for copying permission. */
/*
This module interfaces with a DHT11 temperature and relative humidity sensor.
Usage (any GPIO pin can be used):
var dht = require("DHT11").connect(C11);
dht.read(function (a) {console.log("Temp is "+a.temp.toString()+" and RH is "+a.rh.toString());});
*/
constructor(public pin: DigitalPin) {
}
public setTimeout(callBack: Function, time: number) {
control.runInParallel(function () {
// console.log("pause voor")
pause(time);
// console.log("pause na")
callBack();
// console.log("callback klaar")
});
};
public read(
cb: (data: { raw: number[]; rh: number; t: number; err: boolean }) => void,
n = 10
) {
let d: number[];
let data: string = "";
let ht = this;
this.pin.digitalWrite(false);
this.pin.setWatch(
(t: any) => {
d.push(t > 0.00005 * 1e6 ? 1 : 0);
// console.log("CB Called");
}
);
pause(20);
this.pin.pin.setPull(PinPullMode.PullUp); // force pin state to output
pause(50)
/* this.setTimeout(() => {
this.pin.setPull(PinPullMode.PullUp)
}, 1); */
// this.setTimeout(() => {
this.pin.clearWatch(); // delete this.watch;
let cks =
this.decode(d.slice(2, 8)) +
this.decode(d.slice(10, 8)) +
this.decode(d.slice(18, 8)) +
this.decode(d.slice(26, 8));
if (cks && (cks & 0xff) == this.decode(d.slice(34, 8))) {
cb({
raw: d,
rh: this.decode(d.slice(2, 8)),
t: this.decode(d.slice(18, 8)),
err: false
});
} else {
if (n > 1) {
// this.setTimeout(() => {
pause(500)
this.read(cb, --n);
// }, 500);
} else {
cb({ raw: d, t: -1, rh: -1, err: cks > 0 });
}
}
// }, 50);
}
public decode = (omega: number[]) => {
let max = omega.length;
let result = omega[max - 1] ? 1 : 0;
let temp = 2;
for (let i = max - 2; i > -1; i--) {
result += omega[i] * temp;
temp *= 2;
}
return result;
};
/**
* Computes the famous Fibonacci number sequence!
*/
//% block
public temperature(): { tc: number; tf: number; rh: number } {
this.pin.digitalWrite(false);
pause(18);
let j = this.pin.digitalRead();
this.pin.pin.setPull(PinPullMode.PullUp);
control.delayMicroseconds(40)
// while (this.pin.digitalRead() == true);
while (this.pin.digitalRead() == false);
while (this.pin.digitalRead() == true);
let value = 0;
let counter = 0;
for (let k = 0; k <= 32 - 1; k++) {
while (this.pin.digitalRead() == false);
counter = 0;
while (this.pin.digitalRead() == true) {
counter += 1;
}
if (counter > 4) {
value = value + (1 << (31 - k));
}
}
let data2 = {
tc: (value & 0x0000ff00) >> 8,
tf: (((value & 0x0000ff00) >> 8) * 9) / 5 + 32,
rh: value >> 24
};
return data2;
}
}