local P = {} local m, topic, cid local function p(msg) print("MQTT: "..msg) end local function mq_client_connected(con) p("connected") end function mq_on_timer() -- This crashes the module: -- local status = wifi.sta.status() -- p("checking status, status="..status) local status = wifi.sta.getip() if status and status ~= "0.0.0.0" then if not m then p("connecting") -- client id, keepalive, username, password m = mqtt.Client(cid, 120) -- m:on("connect", mq_connected) m:on("offline", function() m:close(); m = nil end) -- host, port, secure, auto_reconnect, function(client), ssl=8883 m:connect("trygvis.io", 1883, 0, 0, mq_client_connected) end else if m then p("Lost wifi connection, disconnecting") m:close() m = nil end end end function P.init(timer_id, client_id) cid = client_id topic = "/esp8266/"..client_id tmr.alarm(timer_id, 3 * 1000, 1, mq_on_timer) end function P.publish(path, payload) if not m then print("Not connected, dropping message to "..path) return end path = topic.."/"..path print("path="..path) m:publish(path, payload, 0, 0) return true, "yo?" end return P