Post your best Lua script examples here

User avatar
By xtal
#32632 Attached is a code snip and results.
1-capture / display tmr value = print#1 436 us
2- tmr.alarm issued print#2 noda
3 print#3 noda
4 start for loop print #4 2005 us
5 end for loop print #5 7.5+ seconds
6 print chip info, flash info,heap info, ip=nil, start server
#6 occurs way before it should
# 2 finally prints time 7.8+ seconds
# 2 keeps trying connect and time reflects this
#3 - #2 ends at 10+ seconds
The intent is to start the wifi connection
Wait for connection to get IP
print chip info, flash info, ip info, start server

As can be seen results are no where close to this..
Why does #2 from tmr.alarm occur 7.5 seconds after being issued
I'm totally bamboozled by the way this is working , CAN ANY BODY GIVE A VALID EXPLANATION...


wifi info printed
Code: Select allwifiConfig.ssid = "12345NETGEAR"     -- Name of the WiFi network you want to join
wifiConfig.pwd =  "mikeis12345"    -- Password for the WiFi network
wifi.setmode(wifi.STATION)               -- Set station mode

y=tmr.now()
print("1--->",(tmr.now()-y))
tmr.alarm(0, 1000, 1, function()
   print("2--->",(tmr.now()-y))
   print("Try Connecting:")
   ip, nm, gw=wifi.sta.getip()
  if ip ~= nil then
      print("3--->",(tmr.now()-y))       
      print("\nIP Info: \nIP Address: ",ip,"\n Netmask: ",nm,"\n Gateway: ",gw)         
      tmr.stop(0)
   end
end)

print("4--->",(tmr.now()-y))
 for x=1,22000 do
    if x==2000 or x==4000 or x==6000 or x==8000 or x==10000 then
       tmr.wdclr()
       print("w-",x)
    end   
    if x==12000 or x==14000 or x==16000 or x==18000 or x==20000 then
       tmr.wdclr()
       print("w-",x)     
    end 
    if ip ~= ip then break end     
    if x == 21000 then break end
  end
print("5--->",(tmr.now()-y))   
print('chip : ',node.chipid())
print('Flash: ',node.flashid())
wifiConfig = nil                 -- End WiFi configuration
collectgarbage()
print('heap : ',node.heap())
    print("gotIP",wifi.sta.getip())
    dofile("httpserver.lc")(80)   


Code: Select all
NodeMCU 0.9.6 build 20150704  powered by Lua 5.1.4
1--->   436
4--->   2005
w-   2000
w-   4000
w-   6000
w-   8000
w-   10000
w-   12000
w-   14000
w-   16000
w-   18000
w-   20000
5--->   7558411
chip :    14695596
Flash:    1458400
heap :    26488
gotIP   nil
lua: cannot open httpserver.lc
2--->   7839525
Try Connecting:
2--->   7845113
Try Connecting:
2--->   7849726
Try Connecting:
2--->   7854323
Try Connecting:
2--->   7868942
Try Connecting:
2--->   7902283
Try Connecting:
2--->   7935728
Try Connecting:
> 2--->   8002244
Try Connecting:
2--->   9002323
Try Connecting:
2--->   10002838
Try Connecting:
3--->   10009704

IP Info:
IP Address:    192.168.1.32   
 Netmask:    255.255.255.0   
 Gateway:    192.168.1.1
User avatar
By xtal
#32673 I just looked at he faq... it has a 1 paragraph on tmr.alarm , which says
to use tmr.alarm if tmr.delay needs to be more than 8ms.

My issue is that tmr.alarm in this code appears to start 7.5 seconds after being issued....

From what I have read tmr.alarm should start immediately, and [this code] I should have 1st call back within [1000ms] 1 second , this does not appear to be the case.

so am I doing something wrong ? I tried wifi.sta.eventmonreg() which doesn't work -- says error nil value , maybe that DEV 0.9.6 fw does not support ?
User avatar
By TerryE
#32788 Hi. I looked at your code and had a guess at what the o/p would be and then looked at the o/p -- yes, pretty much as I expected. Look at my node restart example. Tmr and other API calls are async. The for loop code isn't and it takes a long time to run. Only then is any of your timers fired. The only reason you don't panic and reboot is because you keep clearing the watchdog timer.

Keep your action routines short. Less than a few 100 executed instructions if possible. Remember that pumping out a lot of output at 9600 takes a lot of time. 80ms is one or two lines of output.

If you are having to clear the watchdog then you are probably approaching your problem the wrong way.