DHT11 / clearWatch error weird behavior #7154
Replies: 1 comment
-
Posted at 2019-09-01 by maze1980 In my opinion a bug in the dht11.js software module. You are not the first to have issues, see also: Posted at 2019-09-01 by th 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: | |_ ___ ___ _ ||___ ___ DHT11 pre read Posted at 2019-09-01 by maze1980 Sorry, edited my post. I wanted to say, the DHT11.js software module has most likely a bug, not the hardware. Posted at 2019-09-01 by Robin Sun 2019.09.01 Hi @th to assist in locating where in the code the memory leak is occurring:
See #3 "New interpreter error: MEMORY" Posted at 2019-09-01 by th 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.
Posted at 2019-09-01 by maze1980 To fix the out-of-memory error, replace these lines (refering to post #3)
with these lines
It would be better to set the watch only once (as well as some other setup tasks), instead of setting it in every run (as it is done in this .js module). Posted at 2019-09-01 by AkosLukacs Edit: ok, it's posted in the ESP32 forum. Stupid me... Posted at 2019-09-01 by th @maze1980 Thanks, will try that! @AkosLukacs ESP32 (dev board), yes. Posted at 2019-09-01 by maze1980 One thing you could try to identify the bug in the DHT11.js module: Print the read "raw" value, using your code in #1. Posted at 2019-09-01 by th 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.
Posted at 2019-09-01 by maze1980 Looks good, just a lil' bit hacked ( Maybe a disconnect function would be nice, to remove the watch when no longer needed. But memory is limited, and I don't think that anyone would ever use it. Unless trying to connect different devices to the same pin, which would be a bad design, better using I2C instead. Posted at 2019-09-01 by maze1980 You should set the pin mode in the connect function to And you could replace a code of code as follows, mainly for better comments, from
to
Posted at 2019-09-01 by @allObjects You invoke clearWatch(handle) with invalid handle. Behavior is absolutely correct. Best pattern for dealing with setWatch() and clearWatch() is:
If your watch is a single shot, you also have to clear the watch handle...
(EDITED) Before a recent release, The other challenge you may face in your code is asynchronous behavior: some code issues a command to a (peripheral) device to prepare some data, then you have to wait for sending another command to receive - pull / get - the prepared data and then process that data. Even pulling the prepared data involve the pattern of repeated, asynchronous pulling of pieces of data until the data gram is complete. The basic solution technique is callbacks, and in elegant form it is Promises. Trying to delay is always a lottery in regard to delay enough... and if it works, it slows overall application flow unnecessarily down. Another safe approach I choose is to start active code within the Posted at 2019-09-01 by th 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. :) Posted at 2019-09-01 by AkosLukacs Re:
Posted at 2019-09-01 by th Ended up rewriting a bit today. Will finish later, but so far:
https://github.com/tedd/Espruino_DHT11/blob/master/DHT11_new.js Posted at 2019-09-02 by @allObjects @th, sorry for the confusion - and thanks @AkosLukacs to point it out (I remembered it kind of reverse...).... Before clearWatch(undefined) did the same as clearWatch()... which was bad... (To be clear, I'll edit my previous post w/ remark). The change to prevent defaulting undefined to no argument also fixed issue of clearing an already cleared watch / interval / timeout again with the same - defined - handle. Before it complained, now it just ignores. Even though cleared handles get at one point in time reused it is of no real concern (when coding properly). Posted at 2019-09-02 by maze1980 Nice rewrite, I really like it. Maybe you could
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. Posted at 2019-09-03 by @allObjects What I do not like about the rewrite is that as soon as the DHT11 instance is created, the setWatch() is activated and will collect "0" and "1" in the instance variable Posted at 2019-09-04 by maze1980
Why, what's the benefit?
No. That is impossible. Or did I miss something? Posted at 2019-09-04 by @allObjects You did not miss anything... I missed missed something: there is no address involved in any of the data grams. Therefore - as you state - only one sensor can be on the data line or pin. Content of data - string variable |
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-09-01 by th
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.
My code:
Beta Was this translation helpful? Give feedback.
All reactions