If it is the DHT11 then it seems weird that it works once clearWatch() causes error. Also if I copy in the DHT11 code (slightly modified so it runs) then it consistently gets correct data from the start. So I doubt it is the DHT11 hardware. (Though now it crashes later on due to "New interpreter error: MEMORY")
var ht={pin: D27};
function DHT11read(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);},20);
// 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,8),2),
temp : parseInt(d.substr(18,8),2)
});
} else {
//if (n>1) setTimeout(function() {ht.read(cb,--n);},500);
if (n>1) setTimeout(function() {DHT11read(cb,--n);},500);
else cb({err:true, checksumError:cks>0, raw:d, temp:-1, rh:-1});
}
}, 50);
}
var displayReady=false;
//var dht = require("DHT11").connect(D27);
var spi = new SPI();
spi.setup({mosi:D23 /* sda */, sck:D22 /* scl */});
var g = require("ILI9163").connect(spi, D21 /* DC */, D18 /* CE */, D19 /* RST */, function() {
displayReady = true; });
require("Font8x12").add(Graphics);
var temp = "N/A";
var rh = "N/A";
function updateDisplay() {
return;
if (!displayReady)
return;
console.log("ILI9163");
g.clear();
g.setColor(1,1,1);
//g.setFont8x12();
g.setFontVector(8);
g.setRotation(1);
g.drawString("Mensa Norge Landstreff FTW!",0,0);
g.setFontVector(20);
g.setColor(1,0.5,1);
g.drawString("Temp: " + temp.toString(),0,10);
g.setColor(0,1,1);
g.drawString("Humid: " + rh.toString(),0,40);
}
function readSensor() {
console.log("DHT11 pre read");
//dht.read(
DHT11read(
function (a) {
console.log("DHT11 post read");
temp = a.temp;
rh = a.rh;
console.log(a);
updateDisplay();
});
}
setInterval(function() {
readSensor();
}, 10000);
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.
If it is the DHT11 then it seems weird that it works once clearWatch() causes error. Also if I copy in the DHT11 code (slightly modified so it runs) then it consistently gets correct data from the start. So I doubt it is the DHT11 hardware. (Though now it crashes later on due to "New interpreter error: MEMORY")
Result:
| |_ ___ ___ _ ||___ ___
| |_ -| . | _| | | | | . |
||_| || |_|||_|_|
2v04 (c) 2019 G.Williams
Espruino is Open Source. Our work is supported
only by sales of official boards and donations:
http://espruino.com/Donate