-->
Page 1 of 1

tmr.alarm() in callback = reboot loop?

PostPosted: Sun Mar 29, 2015 3:51 am
by olebowle
Hi everyone,

I'm new to nodemcu, but I got to say developing for a µC with an interpreted language is a breeze. I'm using "NodeMCU 0.9.5 build 20150213 powered by Lua 5.1.4" (0x00000.bin, 0x10000.bin from https://github.com/nodemcu/nodemcu-flasher/tree/master/Resources/Binaries).

I have downloaded two file onto the esp8266 filesystem: init.lua and webap_toggle_pin.lua. The last one is basically the net.createServer(net.TCP) from the nodemcu-firmware repo. But instead of gpio.write(...) I call illuminate(...), which is implemented in time.lua.

init.lua:
Code: Select allwifi.setmode(wifi.STATION)
wifi.sta.config("ssid","password")
wifi.sta.setip({ip="192.168.178.10",netmask="255.255.255.0",gateway="192.168.178.1"})
wifi.sta.connect()
gpio.mode(4, gpio.OUTPUT)

dofile("webap_toggle_pin.lua")
dofile("time.lua")


time.lua:
Code: Select allilluminate = function (enable)
   local on
   -- we don't need to ask for sunset/sunrise if lights should be turned off
   if(not enable) then
      gpio.write(4, gpio.LOW)
      tmr.stop(1)
      return
   end
   -- ask for sunset/sunrise and act accordingly on disconnect
   conn=net.createConnection(net.TCP, 0)
      conn:on("receive", function(conn, payload)
         for k, v in string.gmatch(payload, "(%w+)=(%w+)") do
            if(k == "on") then
               on=v
            end
         end
      end)
      conn:on("disconnection", function(conn, payload)
         if(on == "1") then
            -- enable light for 60 seconds
            gpio.write(4, gpio.HIGH)
            tmr.stop(1)
            --tmr.alarm(1, 60000, 0, function() gpio.write(4, gpio.LOW) end )
         elseif(on == "0") then
            gpio.write(4, gpio.LOW)
         end
      end)
      conn:connect(1500,"192.168.178.30")
      conn:send("GET / HTTP/1.1\r\n\r\n")
end

As long as tmr.alarm(...) is commented out, I can download time.lua and everything is fine. But as soon as I download the uncommented version and reset the node, it is in a reset loop. What's going on?

Am I not supposed to call tmr.alarm() in a callback function? Am I missing something?