Always when I try to connect more than 1 client , I have a disconnection!
I'm a new guy using LUA!!!
My codes ...
m2 = mqtt.Client(wifi.sta.getmac(), 120, "", "")
m2:lwt("/lwt", wifi.sta.getmac(), 0, 0)
m2:on("offline", function(con)
print ("reconnecting...")
print(node.heap())
tmr.alarm(1, 10000, 0, function()
m2:connect(MQTT_HOST, MQTT_PORT, 0)
end)
end)
-- on publish message receive event
m2:on("message", function(conn, topic, data)
print(topic .. ":" )
if data ~= nil then
print(data)
end
end)
tmr.alarm(0, 1000, 1, function()
if wifi.sta.status() == 5 then
tmr.stop(0)
m2:connect(MQTT_HOST, MQTT_PORT, 0, function(conn)
print("connected")
m2:subscribe("/mcu/cmd/animate",0, function(conn)
m2:publish("/mcu/cmd/animate",'"5"',0,0, function(conn) print("sent") end)
end)
end)
end
end)
m_dis = {}
local count = 0
function animate(m, pl)
-- Confirm that an animation message was recieved on the /mcu/cmd topic
m:publish("/mcu/rgbled_status/", "--> ANIMATE COMMAND", 0, 0,
function(m) print("ANIMATE COMMAND") end)
-- Main option control structure. Pretty gross-looking but it works
-- Option 0 turns everything off
if pl == "0" then
-- Confirm LED being turned off to serial terminal and MQTT broker
m:publish("/mcu/rgbled_status/", "--> LED OFF", 0, 0,
function(m) print("LED OFF") end)
-- Reset the counter and stop the timer from another function
count = 0
tmr.stop(1)
-- RBG Mode
elseif pl == "4" then
m:publish("/mcu/rgbled_status/", "--> DESLIGAR TODOS", 0, 0,
function(m) print("DESLIGAR TODOS") end)
-- Reset the counter and stop the timer from another function
count = 0
tmr.stop(1)
gpio.write(led1, gpio.LOW);
gpio.write(led2, gpio.LOW);
elseif pl == "5" then
m:publish("/mcu/rgbled_status/", "--> LIGAR TODOS", 0, 0,
function(m) print("LIGAR TODOS") end)
-- Reset the counter and stop the timer from another function
count = 0
tmr.stop(1)
gpio.write(led1, gpio.HIGH);
gpio.write(led2, gpio.HIGH);
-- Pick a "random" color and make it breathe mode
-- Something went wrong somehow
else
-- Print and publish to serial terminal and MQTT broker respectively
-- that something went wrong
m:publish("/mcu/rgbled_status/", "--> Error: Unknown Command", 0, 0,
function(m) print("ERROR: UNKNOWN COMMAND") end)
end
end
-- As part of the dispatcher algorithm, this assigns a topic name as a key or
-- index to a particular function name
m_dis["/mcu/cmd/animate"] = animate
-- initialize mqtt client with keepalive timer of 60sec
m = mqtt.Client("PRINCIPAL", 60, "", "") -- Living dangerously. No password!
-- Set up Last Will and Testament (optional)
-- Broker will publish a message with qos = 0, retain = 0, data = "offline"
-- to topic "/lwt" if client don't send keepalive packet
m:lwt("/lwt", "Oh noes! Plz! I don't wanna die!", 0, 0)
-- When client connects, print status message and subscribe to cmd topic
m:on("connect", function(m)
-- Serial status message
print ("\n\n", MQTT_CLIENTID, " connected to MQTT host ", MQTT_HOST,
" on port ", MQTT_PORT, "\n\n")
-- Subscribe to the topic where the ESP8266 will get commands from
m:subscribe("/mcu/cmd/#", 0,
function(m) print("Subscribed to CMD Topic") end)
end)
-- When client disconnects, print a message and list space left on stack
m:on("offline", function(m)
print ("\n\nDisconnected from broker")
print("Heap: ", node.heap())
end)
-- On a publish message receive event, run the message dispatcher and
-- interpret the command
m:on("message", function(m,t,pl)
print("PAYLOAD: ", pl)
print("TOPIC: ", t)
-- This is like client.message_callback_add() in the Paho python client.
-- It allows different functions to be run based on the message topic
if pl~=nil and m_dis[t] then
m_dis[t](m,pl)
end
end)
-- Connect to the broker
m:connect(MQTT_HOST, MQTT_PORT, 0, 1)