add pinMode(self.pin, 'input_pullup'); to function DHT11(pin) before setting the watch. In my opinion not having this is the reason that the first bit is zero in the first run, and one in the consecutive runs that you observed (line 15).
remove pinMode(self.pin); (line 40). The signal should be pulled all time, line 39 is sufficient.
Remove the 10 consecutive measurements, already done.
fix // Two first bytes are header ... to bits (line 63).
add err: false in the callback for a successful measurement (line 79).
change to temp: undefined, rh: undefined in the callback for a failed measurement (line 89). Because -1 is a valid temperature it's not an optimal return value for a failed measurement, and also not good for further processing (you can store -1 for temp and rh in a database, if you don't test)
Test t, tf, rh, rhf to be numbers before calling parseFloat. If self.d is less than 42 bits the parseInt will eventually return "NaN", so I would suggest to test the length of self.d in line 63.
5, 6, 7 and 8:
setTimeout(function() {
let error = true;
let temp = undefined;
let relh = undefined;
if (self.d.length === 42) { //Only if we got 42 bits
// Two first bits are header. First bit is 0 on first, 1 on consecutive. Second bit is always 1.
let h = self.d.substr(1,1); // Header, always "1" - we'll include this in checksum test
let rh = parseInt(self.d.substr(2,8),2); // Relative humidity
let rhf = parseInt(self.d.substr(10,8),2); // Relative humidity fraction
let t = parseInt(self.d.substr(18,8),2); // Temperature
let tf = parseInt(self.d.substr(26,8),2); // Temperature fraction
let csum = parseInt(self.d.substr(34,8),2); // Checksum
// Calculate checksum
let cks = rh + rhf + t + tf;
// Check checksum:
// - It is not zero (temp and humidity exactly zero?)
// - Sum all data, compare last byte to checksum byte
if (h == "1" && cks && ((cks & 0xFF) == csum)) {
error = false;
temp = parseFloat(t + "." + tf);
relh = parseFloat(rh + "." + rhf);
}
}
// Callback
cb({ err: error, raw: self.d, temp: temp, rh: relh });
}, 30);
I did't try the code not having a DHT11, watch out for typos and other errors. But I guess you'll get the idea and catch them.
Anyway, you write the module so do what you need. Others can adjust the code to their needs, as it is now much cleaner and easier to understand compared to the original module.
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.
Nice rewrite, I really like it. Maybe you could
pinMode(self.pin, 'input_pullup');
tofunction DHT11(pin)
before setting the watch. In my opinion not having this is the reason that the first bit is zero in the first run, and one in the consecutive runs that you observed (line 15).self.d += 0 | (t.time - t.lastTime > 0.00005);
withself.d += Number(t.time - t.lastTime > 0.00005);
(line 19).pinMode(self.pin);
(line 40). The signal should be pulled all time, line 39 is sufficient.// Two first bytes are header ...
tobits
(line 63).err: false
in the callback for a successful measurement (line 79).temp: undefined, rh: undefined
in the callback for a failed measurement (line 89). Because -1 is a valid temperature it's not an optimal return value for a failed measurement, and also not good for further processing (you can store -1 for temp and rh in a database, if you don't test)5, 6, 7 and 8:
I did't try the code not having a DHT11, watch out for typos and other errors. But I guess you'll get the idea and catch them.
Anyway, you write the module so do what you need. Others can adjust the code to their needs, as it is now much cleaner and easier to understand compared to the original module.