Oversampling on analog input hangs nodeMCU

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
mackowiakp
Normal user
Posts: 95
Joined: 07 Jun 2018, 06:47

Oversampling on analog input hangs nodeMCU

#1 Post by mackowiakp » 15 Aug 2019, 07:35

I do not know if the conclusion contained in the topic title is correct. But this is the result of my almost 2 months of observations and tests.
I use 8 pieces of nodeMCU in a home automation system. In three of them I use the ADC converter input. In all these three devices, I added a resistor so that the voltage at the ADC input - taking into account that the PCB of nodeMCU contains a resistance voltage divider at the input - never exceeds 4.2V. For about 2 months, one of these three devices began to hang. This phenomenon is very rare. Sometimes it occurs twice a day and sometimes the whole week works properly. But keep in mind that I reboot all devices daily from rule. At first I thought it was directly related to WiFi. So I added a rule to reconnect WiFi every 30 minutes just for testing. This, however, did not solve the problem. The connection was not established. What's more, the rules that control the relays and the time-dependent ones don't work either. I am not able to tell if this is due to the device hanging for its own or because "losing" the time previously synchronized with the NTP server. I have been looking for differences in device configuration for a long time (and you know, the easiest thing to find is the hardest). The software versions are the same everywhere. I try to keep them always new.
Well, this hanging device had ADC oversampling enabled. The other two using ADC - no. And these devices do not hang. So I turned off oversampling on this "fatal" device and now it doesn't hang.
In fact, I don't need oversampling for anything. But I think it might be a problem for others. Is it issue or not? If so i move topic to github.

User avatar
ThomasB
Normal user
Posts: 375
Joined: 17 Jun 2018, 20:41
Location: USA

Re: Oversampling on analog input hangs nodeMCU

#2 Post by ThomasB » 15 Aug 2019, 23:27

So I turned off oversampling on this "fatal" device and now it doesn't hang.
In fact, I don't need oversampling for anything. But I think it might be a problem for others. Is it issue or not? If so i move topic to github.
This sounds like it should be reported as a github issue. Especially if you can duplicate the bug in a basic installation (minimal plugins/controllers).

I was curious about the internal ADC plugin's oversampling function (it's not explained in the wiki or readthedocs). A quick review of P002_ADC code showed that the oversampling mode is basic data averaging. The plug-in's Interval value is used both as the sample period and read interval. The oversampling mode causes faster ADC reads (10Hz) and requires float math.

Although I didn't see anything obvious that explained your hang issue, I did find an unrelated but significant bug. If Oversampling is enabled, and the Interval is greater than 109 minutes, then the ADC value will be corrupted. That is because the 16 bit sampling counter (Plugin_002_OversamplingCount) will wrap around 0 which breaks the data averaging math.

I did a test to confirm this. Setup: Oversampling enabled, Interval set to 6605 (~110 minutes), data value formula not used. The expected ADC read result should have been 40. But 27754 is reported.
Log output shows:

Code: Select all

112674760: ADC  : Analog value: 27754 = 27754.199 (97 samples)
112674764: EVENT: ADCINTERNAL#Analog=27754.20
Screenshot of device page:
adc_bug.jpg
adc_bug.jpg (23.43 KiB) Viewed 85 times

If you submit your hang problem to ESPEasy github then it would be helpful to mention this bug too.

- Thomas

mackowiakp
Normal user
Posts: 95
Joined: 07 Jun 2018, 06:47

Re: Oversampling on analog input hangs nodeMCU

#3 Post by mackowiakp » 16 Aug 2019, 05:32

A quick review of P002_ADC code showed that the oversampling mode is basic data averaging
I also looked at the source code and came to the same conclusions.
The error you are showing seems obvious. After all, the ADC converter is 10 bit. So the highest value that can be read from it is 1024. It is therefore not possible for any average value, regardless of how calculated, to take the value of 27754.199. I don't know if the nodeMCU "hanging" and the error You have detected have something common, although it seems to be possible. I will wait for moderators' opinion because I do not want to clutter github unnecessarily.

TD-er
Core team member
Posts: 1339
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Oversampling on analog input hangs nodeMCU

#4 Post by TD-er » 17 Aug 2019, 21:41

On ESP32 this bug is even worse, since that one has 12 bit ADC.
I am using the ADC + oversampling in the unit in my car to keep an eye on the car battery voltage.
It's running like this since January.
But I do read the value every minute.
Oh and a crash can occur if it is dividing by zero.
But I have not yet looked at the code, so no idea if that may happen here.

User avatar
ThomasB
Normal user
Posts: 375
Joined: 17 Jun 2018, 20:41
Location: USA

Re: Oversampling on analog input hangs nodeMCU

#5 Post by ThomasB » 17 Aug 2019, 23:59

On ESP32 this bug is even worse, since that one has 12 bit ADC.
Fortunately the sampled 12bit data is accumulated in a int32, so major disaster is averted.
Oh and a crash can occur if it is dividing by zero. But I have not yet looked at the code, so no idea if that may happen here.
I also saw that a fatal division by zero was possible. But it would only occur if oversampling is enabled and the user has chosen a read interval that is close to 6554 secs (or values that are divisible by that period). I admit it is a fairly unique configuration, but fixing it is preferable to ignoring it.

But at this point it's not known if these observed bugs are the cause of the OP's reported hang that occurs with oversampling enabled. For example, it could be a bug outside the ADC plugin does not play nice with the oversampling function. Some generous developer is needed to volunteer their precious free time to debug this. Wink wink.

- Thomas

mackowiakp
Normal user
Posts: 95
Joined: 07 Jun 2018, 06:47

Re: Oversampling on analog input hangs nodeMCU

#6 Post by mackowiakp » 18 Aug 2019, 09:03

"Dividing by zero" - sounds pretty nice... :shock:

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests