As the title says... Chat on...

User avatar
By TheSwitchGuy
#56232 So, the problem I have is: I created a lua script to control a lightswitch with commands send to a tcp-server running on the nodemcu or by pressing the normal lightswitch. But after a while the nodemcu is not reacting to any input on the GPIO pin.
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:
Code: Select all--
--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.
User avatar
By devsaurus
#56272 Your code uses tmr.now() in the callback for gpio.trig(). As far as I understood that's done to implement some sort of low-pass filtering. But this will break when the internal system counter wraps around after 2^31µs (see the note in http://nodemcu.readthedocs.io/en/dev/en ... mr/#tmrnow).
Maybe this is your problem?
User avatar
By TheSwitchGuy
#56280 As you can see there are two '-' in front of the line. This means that they are commented out so this has no impact. I tried to "reset" the callback with this method (disabling it and then re-enabling), but this also didnt work out. I am just completely clueless.
There was the same problem with the webserver in earlier versions of nodeMCU, it also stopped responding, but this was fixed with lua 1.5.4 i think. And perhaps they broke the gpio.trig()-function with the fix, who knows?