Interrupt doesn't work after loading
Posted: Sun Oct 25, 2015 7:22 pm
Hi, I am building wifi light switches to use with OpenRemote IoT smart home. Each light switch has a manual switch for local operation. The only way I could find to do this was with an interrupt to change the light to the opposite state. Using ESPlorer and running the program from snippets everything works fine. When I copy it into script file and save it to disc or load and run it, the interrupts don't work at all. I have tried many variations but always the same result.
-- init.lua --
--Wifi Remote light on/off switch with local switch
--for use with Openremote
------------------------------------------------
-- Global Variables (Modify for your network) --
------------------------------------------------
ssid = "*********"
pass = "***************"
port=2323--set port for each ESP
light=3 --GPIO0
switch=4 --GPIO2
lightstate="off"
sw=0
inInt=false
-- Configure Wireless Internet
wifi.setmode(wifi.STATION)
print('set mode=STATION (mode='..wifi.getmode()..')\n')
print('MAC Address: ',wifi.sta.getmac())
print('Chip ID: ',node.chipid())
print('Heap Size: ',node.heap(),'\n')
-- wifi config start
wifi.sta.config(ssid,pass)
-- wifi config end
---GPIO Setup---
----------------
gpio.mode(light, gpio.OUTPUT)
gpio.write(light, gpio.HIGH)-- high = off
-----mode & trig for interrupt by manual switch--
gpio.mode(switch,gpio.INT)
gpio.trig(switch, "both",swINT)
-- create a server
-- 10s time out for a inactive client
sv=net.createServer(net.TCP, 10)
-- server listen on port,
sv:listen(port,function(conn)
conn:on("receive", function(conn, pl)
if string.find(pl,"on") then
print(pl)
gpio.write(light, gpio.LOW)
lightstate="on"
end
if string.find(pl,"off") then
print(pl)
gpio.write(light, gpio.HIGH)
lightstate="off"
end
if string.find(pl,"sen") then
conn:send (lightstate)
end
end)
end)
---interrupt function
------inInt ensures interrupt not interrupted
---------timer to cover contact bounce
swINT(level)
if inInt then
return
else
inInt=true
end
tmr.delay(10000)
if (lightstate=="on") then
gpio.write(light, gpio.HIGH)
lightstate="off"
else
gpio.write(light, gpio.LOW)
lightstate="on"
end
inInt=false
end
-- init.lua --
--Wifi Remote light on/off switch with local switch
--for use with Openremote
------------------------------------------------
-- Global Variables (Modify for your network) --
------------------------------------------------
ssid = "*********"
pass = "***************"
port=2323--set port for each ESP
light=3 --GPIO0
switch=4 --GPIO2
lightstate="off"
sw=0
inInt=false
-- Configure Wireless Internet
wifi.setmode(wifi.STATION)
print('set mode=STATION (mode='..wifi.getmode()..')\n')
print('MAC Address: ',wifi.sta.getmac())
print('Chip ID: ',node.chipid())
print('Heap Size: ',node.heap(),'\n')
-- wifi config start
wifi.sta.config(ssid,pass)
-- wifi config end
---GPIO Setup---
----------------
gpio.mode(light, gpio.OUTPUT)
gpio.write(light, gpio.HIGH)-- high = off
-----mode & trig for interrupt by manual switch--
gpio.mode(switch,gpio.INT)
gpio.trig(switch, "both",swINT)
-- create a server
-- 10s time out for a inactive client
sv=net.createServer(net.TCP, 10)
-- server listen on port,
sv:listen(port,function(conn)
conn:on("receive", function(conn, pl)
if string.find(pl,"on") then
print(pl)
gpio.write(light, gpio.LOW)
lightstate="on"
end
if string.find(pl,"off") then
print(pl)
gpio.write(light, gpio.HIGH)
lightstate="off"
end
if string.find(pl,"sen") then
conn:send (lightstate)
end
end)
end)
---interrupt function
------inInt ensures interrupt not interrupted
---------timer to cover contact bounce
swINT(level)
if inInt then
return
else
inInt=true
end
tmr.delay(10000)
if (lightstate=="on") then
gpio.write(light, gpio.HIGH)
lightstate="off"
else
gpio.write(light, gpio.LOW)
lightstate="on"
end
inInt=false
end