Left here for archival purposes.

User avatar
By ALIAS
#13197 So i am pretty new to LUA and i really cannot figure out the logic behind this execution flow.
This script should get a string from my serial split it and then send it by mqtt one word at a time.
All is good until my client loses connection and needs to reconnect to broker. After it finishes reconnecting it somehow returns to the sendit function and starts trying to execute what's inside the publish (function(send)) callback. And only afterwards triggers the reconnected function from my timer alarm.
Note that this does not happen if it didn't publish something before the disconnect. My fix was adding the if message[i] otherwise it would break my script.
Is this normal? I mean wtf :)


Code: Select allm = mqtt.Client("NODEMCU", 120, "", "")

m:on("offline", function(disconnect)
     print("Reconnecting to MQTT...")
     print(node.heap())
     tmr.alarm(1, 10000, 0, function()
          m:connect("192.168.1.99", 1883, function(reconnected)
            print("Reconnected to MQTT.")
            tmr.stop(1)
          end)
     end)
end)

m:on("connect", function(connected)
     print("Connected to MQTT.")
end)

m:connect("192.168.1.99", 1883, 0)

uart.on("data", "\r", function(data)
    print("Received from RX: ", data)
    dosend(data)
    if data=="quit\r" then
        print("Waiting for RX input...")
        uart.on("data")
    end       
end, 0)


function sendit(message,i)
    print("MQTT sending package : " ..message[i])
    m:publish("/data/"..i,message[i],0,0, function(send)
-- PROBLEM HERE
        if message[i] then
            print("MQTT sent package : " ..message[i])
        end
        i=i+1
        if message[i] then
            sendit(message,i)
        end
    end)
end

function mysplit(inputstr, sep)
    if sep == nil then
            sep = "%s"
    end
    local t={} ; i=1
    for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
            t[i] = str
            i = i + 1
    end
    return t
end

function dosend(message)
    v=mysplit(message)
    i=1
    while v[i] do
        print("Got args : " ..v[i])
        i=i+1
    end
    sendit(v,1)
end
User avatar
By cal
#13623 Moin,

new to lua myself I can't judge on the concrete code. Maybe more "local" like for "v".
It seems to be that some work is being done on mqtt stuff and I know that integration with sdk is tricky for
lua runtime.
I would give a travis build firmware from dev branch a try.

Carsten