You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
990 B

m = mqtt.Client(node.random(1024), 120, mqtt_user, mqtt_password)
m:lwt("/lwt", "offline", 0, 0)
m:on("connect", function(client) print ("connected") end)
m:on("offline", function(client)
print ("offline, reconnecting")
handle_mqtt_error()
end)
m:on("message", function(client, topic, data)
print(topic .. ":" )
if data ~= nil then
print(data)
if topic == "/voc/alert-viri" then
recieveC3VOC(data)
elseif topic == "/signal/led" then
receiveDirect(data)
end
end
end)
function handle_mqtt_error(client, reason)
tmr.create():alarm(10 * 1000, tmr.ALARM_SINGLE, do_mqtt_connect)
end
function do_mqtt_connect()
m:connect(mqtt_server, mqtt_port, 0, function(client)
print("connected")
-- subscribe topic with qos = 0
client:subscribe({["/voc/alert-viri/#"]=0,["/signal/led/#"]=0}, function(client)
roundWipe(0,255,0,1)
end)
end,
function(client, reason)
print("failed reason: " .. reason)
handle_mqtt_error()
end)
end
do_mqtt_connect()