I don't know if you understand what I mean, my english is pretty bad.
So I connected stuff like it follows:
nodemcu controls a SSR to switch on/off the light.
schalter = the "pushbutton/switch".
And then there is a switch (like a pushbutton) connected to ground and D1 (gpio.mode(schalter, gpio.INT, gpio.PULLUP))
And this is my code:
--
--Version 1.0: Script is working. UPDATE and REBOOT not sending data before node resets.
--Version 1.1: UPDATE and REBOOT working now. See #730 for fixing idea.
--Version 1.2: Added SSR reacting to interrupt on pin 1 (D1).
--Version 1.3: ChipInfo command + debug messages for connected TCP Client.
--Version 1.4: Send messages to all connected Clients.
--Version 1.5: Stopped garbage collection.
--
version = "1.5"
collectgarbage('stop')
--Pin config goes here
led = 4
ssr = 2 --D4
schalter = 1 --D1
gpio.mode(led, gpio.OUTPUT)
gpio.mode(ssr, gpio.OUTPUT)
gpio.write(led, 1)
gpio.write(ssr, 0)
gpio.mode(schalter, gpio.INT, gpio.PULLUP)
--last time toggle was executed
last_toggle = 0
--variable storing the current ssr state
ssr_state = "OFF"
--variable storing the connection
CONNECTIONS = {}
--for k,v in pairs(CONNECTIONS) do print(v) end
function turnSSR_ON()
gpio.write(led, 0)
gpio.write(ssr, 1)
end
function turnSSR_OFF()
gpio.write(led, 1)
gpio.write(ssr, 0)
end
function notify(msg)
for k,v in pairs(CONNECTIONS) do
print("notify: ")
print(v)
v:send(msg .. "\n\r", nil)
end
end
function connection(conn)
CONNECTIONS[conn] = conn
print("subscribe: ")
print(CONNECTIONS[conn])
conn:send("Version " .. version .. " Light: " .. ssr_state .. "\n\r", nil)
conn:on("receive", receive)
conn:on("disconnection", function()
print("unsubscribe: ")
print(CONNECTIONS[conn])
CONNECTIONS[conn] = nil
end)
end
function receive(conn, payload)
print("< " .. payload) --print recived data
if string.find(payload, "ON") then
if string.find(ssr_state, "OFF") then
ssr_state = "ON"
state = 1
turnSSR_ON()
print("Light on!")
notify("Light: " .. ssr_state)
else
conn:send("Light already on!", nil)
end
elseif string.find(payload, "OFF") then
if string.find(ssr_state, "ON") then
ssr_state = "OFF"
state = 0
turnSSR_OFF()
print("Light off!")
notify("Light: " .. ssr_state)
else
conn:send("Light already off!", function() end)
end
elseif string.find(payload, "UPDATE") then
for k,v in pairs(CONNECTIONS) do
if(v ~= conn) then
v:close()
end
end
conn:send("Rebooting for update...", function(conn)
file.open("getNewFile", "w+")
file.write("1")
file.close()
print("Updating... Rebooting...")
conn:close()
node.restart()
end)
elseif string.find(payload, "REBOOT") then
for k,v in pairs(CONNECTIONS) do
if(v ~= conn) then
v:close()
end
end
conn:send("Rebooting...", function(conn)
conn:close()
print("Rebooting...")
node.restart()
end)
elseif string.find(payload, "CHIPINFO") then
majorVer, minorVer, devVer, chipid, flashid, flashsize, flashmode, flashspeed = node.info()
conn:send("Chip Info: " ..
majorVer .. ", " ..
minorVer .. ", " ..
devVer .. ", " ..
chipid .. ", " ..
flashid .. ", " ..
flashsize .. ", " ..
flashmode .. ", " ..
flashspeed, nil)
else
print("unknown command")
conn:send("Unknown Command!", nil)
end
end
function con_trigger()
--clear trigger
gpio.trig(schalter, nil, nil)
--set trigger
gpio.trig(schalter, "both",
--calback
function(level)
if(last_toggle + 150000 > tmr.now()) then
return
end
print("react to: " .. level)
last_toggle = tmr.now()
if(ssr_state == "OFF") then
ssr_state = "ON"
turnSSR_ON()
elseif(ssr_state == "ON") then
ssr_state = "OFF"
turnSSR_OFF()
end
notify("Light: " .. ssr_state)
state = level
end
--callback end
)
print("connected trigger")
end
--Main
--if not tmr.alarm(1, 120000, tmr.ALARM_AUTO, con_trigger) then
-- print("tmr not armed")
--end
con_trigger()
srv = net.createServer(net.TCP, 28799)
print("server started")
srv:listen(5001, connection)
What I tried to fix the Problem:
use a timer (see the tmr.alarm in the code) to "reconnect" the GPIO.trig() function -> not working.
stop lua garbage collection -> not working.
change the GPIO pin mode to INPUT and leave PULLUP away (every possiblity) -> not working.
I have no idea what I could try, perhaps you know what makes my code fail.