local nwfnet = require "nwfnet"
local self = {}
function self.mkclient(cf) -- construct a client with config from json file cf
+ local c, k, u, p
if file.open(cf) then
local conf = cjson.decode(file.read())
- local c, k, u, p
if type(conf) == "table" then
- c = conf["clientid"]; k = conf["keepalive"]; u = conf["user"]; p = conf["pass"]
+ c = conf["clientid"]; k = conf["keepalive"]; u = conf["user"]; p = conf["pass"]; l = conf["clean"]
end
+ file.close()
+ if not u or not p then return nil end
c = c or string.format("NODE-%06X",node.chipid())
k = k or 1500
- file.close()
- local m = mqtt.Client(c,k,u,p)
+ l = l or 0
+ local m = mqtt.Client(c,k,u,p,l)
m:on("connect", function(c) nwfnet:runnet("mqttconn",c) end)
m:on("offline", function(c) nwfnet:runnet("mqttdscn",c) end)
m:on("message", function(c,t,m) nwfnet:runmqtt(c,t,m) end)
return m, u, c
end
+ return nil
end
function self.connect(m,cf) -- make a connection with parameters from json file cf
local broker, port, secure