Left here for archival purposes.

User avatar
By Bill..
#37809 I was trying to get a delay in a program. The program kept crashing, so I reduced it to a minimal while loop and just tested that. It crashes every time.

I am running ESP8266 LuaLoader 0.87
NodeMCU 0.9.6 build 20150704 powered by Lua 5.1.4

The "Test.lua" code:============================

print("begin test")
DelayEnd = 0
while (DelayEnd == 0) do end
print("end of test")

The output: ===================

dofile(Test.lua) Sunday, January 03, 2016 12:24:33

dofile("Test.lua")
begin test
?©Fê ¦ôž!9Œ¦HŒbê

NodeMCU 0.9.6 build 20150704 powered by Lua 5.1.4
lua: cannot open init.lua
>
Hard Restart Sunday, January 03, 2016 12:24:38

Anybody have an idea what the problem is?
User avatar
By devsaurus
#37841 The endless loop prevents that the watchdog is serviced in time. Thus it resets the chip after ~5 seconds.
You should generally avoid busy waiting loops with the nodemcu firmware. If you can't avoid them, then feed the watchdog in time.
User avatar
By TerryE
#37843 The correct way to delay your execution is to use a tmr.alarm() callback. Anything else will just trip the watchdog, or cause the WiFi stack to fail.
User avatar
By Bill..
#37846 Thanks, I didn't think of the watchdog timer.
How do I avoid triggering the watchdog timer? I modified the code to put some code into the wait loop, but it still crashes.

print("begin test")
DelayEnd = 0
while (DelayEnd < 10000) do
print(DelayEnd)
DelayEnd = DelayEnd + 1
end
print("end of test")

This counts to 661 before the watchdog timer triggers.
I divided the loop into a 500 count incrementing while followed by a 500 count decrementing while and the watchdog timer still triggers while counting down at 339.

print("begin test")
DelayEnd = 0
while (DelayEnd < 500) do
print(DelayEnd)
DelayEnd = DelayEnd + 1
end
while (DelayEnd > 0) do
print(DelayEnd)
DelayEnd = DelayEnd - 1
end
print("end of test")

I found tmr.wdclr() to clear the watchdog timer, now it will count to 10,000 without problems.

print("begin test")
DelayEnd = 0
while (DelayEnd < 10000) do
print(DelayEnd)
DelayEnd = DelayEnd + 1
tmr.wdclr()
end
print("end of test")

My question now is... What "resets" the watchdog timer in a normal program since I don't call tmr.wdclr()?