Let me restate the problem.
I pulled the DHT22.js module and inserted a console.log for illustration.
Here is where the console.log was inserted
delete ht.watch;
var cks =
parseInt(d.substr(2,8),2)+
parseInt(d.substr(10,8),2)+
parseInt(d.substr(18,8),2)+
parseInt(d.substr(26,8),2);
console.log('n=',n,'len=',d.length,' cks=',cks&0xFF,' chsum=',parseInt(d.substr(34,8),2));
if (cks&&((cks&0xFF)==parseInt(d.substr(34,8),2))) {
The example code
var dht=new DHT22(dpin);
setInterval(function () {
dht.read(function (a) {
F=a.temp;
H=a.rh;
console.log("Temp is "+F.toString()+" and RH is "+H.toString());
});
}, 2000);
Produces the following output:
n= 3 len= 42 cks= 201 chsum= 201
Temp is 23.2 and RH is 48
n= 10 len= 0 cks= 0 chsum= NaN
n= 6 len= 0 cks= 0 chsum= NaN
n= 9 len= 42 cks= 201 chsum= 201
Temp is 23.2 and RH is 48
n= 5 len= 0 cks= 0 chsum= NaN
n= 4 len= 42 cks= 202 chsum= 202
Temp is 23.2 and RH is 48.1
n= 10 len= 42 cks= 202 chsum= 202
Temp is 23.2 and RH is 48.1
n= 10 len= 42 cks= 202 chsum= 202
Temp is 23.2 and RH is 48.1
n= 10 len= 42 cks= 201 chsum= 201
Temp is 23.2 and RH is 48
n= 10 len= 42 cks= 201 chsum= 201
Temp is 23.2 and RH is 48
n= 10 len= 40 cks= 200 chsum= 50
n= 9 len= 41 cks= 200 chsum= 100
n= 8 len= 41 cks= 200 chsum= 100
n= 7 len= 42 cks= 201 chsum= 201
Temp is 23.2 and RH is 48
n= 10 len= 0 cks= 0 chsum= NaN
n= 9 len= 42 cks= 201 chsum= 201
Temp is 23.2 and RH is 48
n= 10 len= 42 cks= 202 chsum= 202
Temp is 23.2 and RH is 48.1
n= 10 len= 42 cks= 201 chsum= 201
Temp is 23.2 and RH is 48
Notice that the module code produces a lot of retries n. 1st try n=10, 2nd try n=9...
By changing the example code to
var dht=new DHT22(dpin);
setInterval(function () {
dht.read(function (a) {});
dht.read(function (a) {
F=a.temp;
H=a.rh;
console.log("Temp is "+F.toString()+" and RH is "+H.toString());
});
}, 2000);
Which gives to following output
>echo(0);
=undefined
n= 10 len= 43 cks= 201 chsum= 201
n= 10 len= 43 cks= 201 chsum= 201
Temp is 23.2 and RH is 48
n= 10 len= 43 cks= 199 chsum= 199
n= 10 len= 43 cks= 199 chsum= 199
Temp is 23.2 and RH is 47.8
n= 10 len= 43 cks= 198 chsum= 198
n= 10 len= 43 cks= 198 chsum= 198
Temp is 23.2 and RH is 47.7
n= 10 len= 43 cks= 198 chsum= 198
n= 10 len= 43 cks= 198 chsum= 198
Temp is 23.2 and RH is 47.7
n= 10 len= 43 cks= 198 chsum= 198
n= 10 len= 43 cks= 198 chsum= 198
Temp is 23.2 and RH is 47.7
n= 10 len= 43 cks= 199 chsum= 199
n= 10 len= 43 cks= 199 chsum= 199
Temp is 23.2 and RH is 47.8
n= 10 len= 43 cks= 199 chsum= 199
n= 10 len= 43 cks= 199 chsum= 199
Temp is 23.2 and RH is 47.8
n= 10 len= 43 cks= 199 chsum= 199
n= 10 len= 43 cks= 199 chsum= 199
Temp is 23.2 and RH is 47.8
This gives correct temperature and humidity readings without all the retries in the module code. From a programming perspective it is troubling as Gordon outlined above.
Even with the setInterval =1000.
And finally changing the example code one more time so that the one dht.read completes before the 2nd one starts.
//var dht = require("DHT22").connect(dpin);
var dht=new DHT22(dpin);
setInterval(function () {
dht.read(function (a) {//});
dht.read(function (a) {
F=a.temp;
// F=(a.temp+40)*9/5-40;
H=a.rh;
console.log("Temp is "+F.toString()+" and RH is "+H.toString());
});
});
}, 2000);
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Let me restate the problem.
I pulled the DHT22.js module and inserted a console.log for illustration.
Here is where the console.log was inserted
The example code
Produces the following output:
Notice that the module code produces a lot of retries n. 1st try n=10, 2nd try n=9...
By changing the example code to
Which gives to following output
This gives correct temperature and humidity readings without all the retries in the module code. From a programming perspective it is troubling as Gordon outlined above.
Even with the setInterval =1000.
And finally changing the example code one more time so that the one dht.read completes before the 2nd one starts.
This gives;
Changing the set interval from 1000 to 2000 helps.
On a scope the entire message takes just under 6ms.