Most recent activity
-
- 22 comments
- 6,975 views
-
Thanks. I checked the DHT11 spec to see what the timings are. Looks like the 50ms is overkill, fail can be determined earlier than 500ms and parsing can be optimized a bit.
It has been running for a few hours and seems to work fine now. I'll fix up the code in a few days from now. Good to get it working since I have ordered 35 ESP32+DHT11+TFT displays for a small intro course I'm holding next month. Would be a bit too fiddly if we had to go for C in Ardunio IDE. :)
-
Thanks @maze1980, that did the trick.
Moved setWatch to ctor, writes to shared buffer. Set a cap on how many readings it will buffer (in case read is never called), and clear buffer upon fresh read. I'll look into making it more efficient later. As long as we have enough data "continously" there is no need for waiting, so it could return immediately.
var dht = require("https://raw.githubusercontent.com/tedd/Espruino_DHT11/master/DHT11_test.js?3").connect(D27); function readSensor() { dht.read( function (a) { console.log(a); setTimeout(readSensor, 5000); }); } readSensor();
-
@maze1980 Thanks, will try that!
@AkosLukacs ESP32 (dev board), yes.
-
So it seems scoping of setWatch affects readings. I'm not familiar with watch, but it seems to be a "special" citizen, defying normal expectations and capable of crashing everything after repeated use.
This works for some time before it fails with MEMORY.
var dht = require("https://raw.githubusercontent.com/tedd/Espruino_DHT11/master/DHT11_test.js").connect(D27); function readSensor() { dht.read( function (a) { console.log(a); setTimeout(readSensor, 5000); }); } readSensor();
-
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);
Result:
| |_ ___ ___ _ ||___ ___
| |_ -| . | _| | | | | . |
||_| || |_|||_|_||_| espruino.com
2v04 (c) 2019 G.Williams
Espruino is Open Source. Our work is supported
only by sales of official boards and donations:
http://espruino.com/DonateDHT11 pre read
DHT11 post read
{
"raw": "010011001000000000000110000000010001001110",
"rh": 50, "temp": 24 }
DHT11 pre read
DHT11 post read
{
"raw": "010011010000000000000101110000100101010100",
"rh": 52, "temp": 23 }
DHT11 pre read
DHT11 post read
{
"raw": "010011001000000000000101110000100101010010",
"rh": 50, "temp": 23 }
DHT11 pre read
DHT11 post read
{
"raw": "010011000100000000000110010000100101010011",
"rh": 49, "temp": 25 }
DHT11 pre read
DHT11 post read
{
"raw": "010011000100000000000110000000000001001001",
"rh": 49, "temp": 24 }
DHT11 pre read
DHT11 post read
{
"raw": "010011000000000000000110000000000001001000",
"rh": 48, "temp": 24 }
DHT11 pre read
DHT11 post read
{
"raw": "010011000000000000000110000000000001001000",
"rh": 48, "temp": 24 }
DHT11 pre read
DHT11 post read
{
"raw": "010011000100000000000110000000000001001001",
"rh": 49, "temp": 24 }
DHT11 pre read
DHT11 post read
{ "err": true, "checksumError": false,
"raw": "0",
"temp": -1, "rh": -1 }
DHT11 pre read
DHT11 post read
{ "err": true, "checksumError": false,
"raw": "0",
"temp": -1, "rh": -1 }
DHT11 pre read
Execution Interrupted during event processing.
New interpreter error: MEMORY
DHT11 pre read
ERROR: Ctrl-C while processing interval - removing it.
Execution Interrupted during event processing.
New interpreter error: CALLBACK -
Hoping someone can help. Having issue with reading DHT11 that I don't understand.
If I never call updateDisplay() then DHT11 returns "temp:-1,rh:-1,err:true,checksumError:false" forever (tried leaving it for days).
If I call updateDisplay() then it returns -1, but after a few iterations I get a clearWatch() error message, then DHT11 temp/rh is read correctly forever.
Compacting Flash...
Calculating Size...
Writing..
Compressed 36800 bytes to 11307
Temp is -1 and RH is -1, Err: true, ChecksumError: false
Temp is -1 and RH is -1, Err: true, ChecksumError: false
Temp is -1 and RH is -1, Err: true, ChecksumError: false
Temp is -1 and RH is -1, Err: true, ChecksumError: false
Temp is -1 and RH is -1, Err: true, ChecksumError: false
Temp is -1 and RH is -1, Err: true, ChecksumError: false
Temp is -1 and RH is -1, Err: true, ChecksumError: false
Temp is -1 and RH is -1, Err: true, ChecksumError: false
Temp is -1 and RH is -1, Err: true, ChecksumError: false
Temp is -1 and RH is -1, Err: true, ChecksumError: false
Temp is -1 and RH is -1, Err: true, ChecksumError: false
Temp is -1 and RH is -1, Err: true, ChecksumError: false
Temp is -1 and RH is -1, Err: true, ChecksumError: false
Temp is -1 and RH is -1, Err: true, ChecksumError: false
Temp is -1 and RH is -1, Err: true, ChecksumError: false
Temp is -1 and RH is -1, Err: true, ChecksumError: false
Uncaught Error: clearWatch(undefined) not allowed. Use clearWatch() instead.
at line 1 col 19
clearWatch(a.watch);delete a.watch;var e=parseInt(b.substr(2...
^
in function called from system
Temp is 25 and RH is 58
Temp is 26 and RH is 55
Temp is 25 and RH is 54
Temp is 25 and RH is 54
Temp is 25 and RH is 54
Temp is 25 and RH is 54
Temp is 26 and RH is 55My code:
var displayReady=false; var dht = require("DHT11").connect(D15); var spi = new SPI(); spi.setup({mosi:D23 /* sda */, sck:D22 /* scl */, baud: 4000000}); var g = require("ILI9163").connect(spi, D21 /* DC */, D18 /* CE */, D19 /* RST */, function() { displayReady = true; console.log("Display ready."); }); require("Font8x12").add(Graphics); var temp = "N/A"; var rh = "N/A"; function updateDisplay() { //return; if (!displayReady) return; g.clear(); g.setColor(1,1,1); g.setFont8x12(); g.setFontVector(8); g.setRotation(1); g.drawString("Test",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() { dht.read(function (a) { temp = a.temp; rh = a.rh; if (a.temp != -1) console.log("Temp is "+a.temp.toString()+" and RH is "+a.rh.toString()); else console.log("Temp is "+a.temp.toString()+" and RH is "+a.rh.toString() + ", Err: " + a.err.toString() + ", ChecksumError: "+ a.checksumError.toString()); updateDisplay(); }); } setInterval(function() { readSensor(); }, 10000);
Ended up rewriting a bit today. Will finish later, but so far:
https://github.com/tedd/Espruino_DHT11/blob/master/DHT11_new.js