What changed from #1 (good temperature readings) to #8 (checksum error)? The length of the raw output changed by one bit. What did you change?
The code in #20 looks non-functional, e.g. parseInt (d.substr (2.8) 2) + can't work.
I copied in the module as reference, with some notes:
/* Copyright (C) 2014 Spence Konde. See the file LICENSE for copying permission. */
/*
This module interfaces with a DHT22 temperature and relative humidity sensor.
Usage (any GPIO pin can be used):
var dht = require("DHT22").connect(C11);
dht.read(function (a) {console.log("Temp is "+a.temp.toString()+" and RH is "+a.rh.toString());});
the return value if no data received: {"temp": -1, "rh": -1, err:true, "checksumError": false}
the return value, if some data is received, but the checksum is invalid: {"temp": -1, "rh": -1, err:true, "checksumError": true}
*/
function DHT22(pin) {
this.pin = pin;
}
DHT22.prototype.read = function (cb, n) {
if (!n) n=10;
var d = "";
var ht = this;
digitalWrite(ht.pin, 0);
pinMode(ht.pin,"output"); // force pin state to output
// start watching for state change
this.watch = setWatch(function(t) {
d+=0|(t.time-t.lastTime>0.00005);
}, ht.pin, {edge:'falling',repeat:true} );
// raise pulse after 1ms
setTimeout(function() {pinMode(ht.pin,'input_pullup');pinMode(ht.pin);},1);
// stop looking after 50ms
setTimeout(function() {
if(ht.watch){ ht.watch = clearWatch(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);
if (cks&&((cks&0xFF)==parseInt(d.substr(34,8),2))) {
cb({
raw : d,
rh : parseInt(d.substr(2,16),2)*0.1,
temp : parseInt(d.substr(19,15),2)*0.2*(0.5-d[18])
});
} else {
if (n>1) setTimeout(function() {ht.read(cb,--n);},500);
else cb({err:true, checksumError:cks>0, raw:d, temp:-1, rh:-1});
}
}, 50);
};
exports.connect = function(pin) {
return new DHT22(pin);
};
From my point of view line 22 is not necessary, or if it is lines 21 and 22 should be swapped.
Line 23 could read // read d based on signal change interval
Lines 27 and 28 could be moved to the end and then rewritten. Since the lines will be only executed once everything is set up there's no need to use a setTimer in this case:
//trigger data transmission
pinMode(ht.pin,'input_pullup');
pinMode(ht.pin);
Line 29 could read // parse the received data d (50ms length)
Line 31 could be changed to clearWatch(ht.watch);, using if is not necessary.
And I'd change line 1 to if (!n) n=1; because one try should be enough if nothing else was specified.
And if your raw data is too long, capturing also the trigger you can add d = d.substr (1); //remove trigger signal to the code. I don't have this sensor, so I can't try.
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.
What changed from #1 (good temperature readings) to #8 (checksum error)? The length of the raw output changed by one bit. What did you change?
The code in #20 looks non-functional, e.g.
parseInt (d.substr (2.8) 2) +
can't work.I copied in the module as reference, with some notes:
From my point of view line 22 is not necessary, or if it is lines 21 and 22 should be swapped.
Line 23 could read
// read d based on signal change interval
Lines 27 and 28 could be moved to the end and then rewritten. Since the lines will be only executed once everything is set up there's no need to use a setTimer in this case:
Line 29 could read
// parse the received data d (50ms length)
Line 31 could be changed to
clearWatch(ht.watch);
, usingif
is not necessary.And I'd change line 1 to
if (!n) n=1;
because one try should be enough if nothing else was specified.And if your raw data is too long, capturing also the trigger you can add
d = d.substr (1); //remove trigger signal
to the code. I don't have this sensor, so I can't try.