--启动mqtt客户端任务 sys.taskInit( function() whiletruedo --是否获取到了分配的ip(是否连上网) if socket.isReady() then local imei = misc.getImei() --新建一个mqtt对象 local mqttClient = mqtt.client(imei,mqttheartbeat,mqttuser,mqttpassword) --尝试连接指定服务器 if mqttClient:connect(mqttip,mqttport,"tcp") then --连接成功 log.info("mqttTest.mqttClient","connect success") else log.info("mqttTest.mqttClient","connect fail") --连接失败 end else --没连上网,原地等待一秒,一秒后会循环回去重试 sys.wait(1000) end end end)
--启动mqtt客户端任务 sys.taskInit( function() whiletruedo --是否获取到了分配的ip(是否连上网) if socket.isReady() then local imei = misc.getImei() --新建一个mqtt对象 local mqttClient = mqtt.client(imei,mqttheartbeat,mqttuser,mqttpassword) --尝试连接指定服务器 if mqttClient:connect(mqttip,mqttport,"tcp") then --连接成功 log.info("mqttTest.mqttClient","connect success") else log.info("mqttTest.mqttClient","connect fail") --连接失败 end else --没连上网 --等待网络环境准备就绪,超时时间是5分钟 sys.waitUntil("IP_READY_IND",300000) --等完了还没连上? ifnot socket.isReady() then --进入飞行模式,20秒之后,退出飞行模式 net.switchFly(true) sys.wait(20000) net.switchFly(false) end end end end)
--启动mqtt客户端任务 sys.taskInit( function() whiletruedo local retryConnectCnt = 0--失败次数统计 --是否获取到了分配的ip(是否连上网) if socket.isReady() then local imei = misc.getImei() --新建一个mqtt对象 local mqttClient = mqtt.client(imei,mqttheartbeat,mqttuser,mqttpassword) --尝试连接指定服务器 if mqttClient:connect(mqttip,mqttport,"tcp") then --连接成功 log.info("mqttTest.mqttClient","connect success") retryConnectCnt = 0--失败次数清零 else log.info("mqttTest.mqttClient","connect fail")--连接失败 retryConnectCnt = retryConnectCnt+1--失败次数加一 end --断开MQTT连接 mqttClient:disconnect() if retryConnectCnt>=5then link.shut() retryConnectCnt=0 end sys.wait(5000) else --没连上网 --等待网络环境准备就绪,超时时间是5分钟 sys.waitUntil("IP_READY_IND",300000) --等完了还没连上? ifnot socket.isReady() then --进入飞行模式,20秒之后,退出飞行模式 net.switchFly(true) sys.wait(20000) net.switchFly(false) end end end end)
--启动mqtt客户端任务 sys.taskInit( function() whiletruedo local retryConnectCnt = 0--失败次数统计 --是否获取到了分配的ip(是否连上网) if socket.isReady() then local imei = misc.getImei() --新建一个mqtt对象 local mqttClient = mqtt.client(imei,mqttheartbeat,mqttuser,mqttpassword) --尝试连接指定服务器 if mqttClient:connect(mqttip,mqttport,"tcp") then --连接成功 log.info("mqttTest.mqttClient","connect success") retryConnectCnt = 0--失败次数清零 --订阅主题 if mqttClient:subscribe({["/s/test/"..imei]=0}) then --循环处理接收和发送的数据 whiletruedo ifnot mqttInMsgProc(mqttClient) then log.error("mqttTest.mqttInMsgProc error") break end ifnot mqttOutMsgProc(mqttClient) then log.error("mqttTest.mqttOutMsgProc error") break end end end else log.info("mqttTest.mqttClient","connect fail")--连接失败 retryConnectCnt = retryConnectCnt+1--失败次数加一 end --断开MQTT连接 mqttClient:disconnect() if retryConnectCnt>=5then link.shut() retryConnectCnt=0 end sys.wait(5000) else --没连上网 --等待网络环境准备就绪,超时时间是5分钟 sys.waitUntil("IP_READY_IND",300000) --等完了还没连上? ifnot socket.isReady() then --进入飞行模式,20秒之后,退出飞行模式 net.switchFly(true) sys.wait(20000) net.switchFly(false) end end end end)
functionmqttInMsgProc(mqttClient) local result,data whiletruedo result,data = mqttClient:receive(2000) --接收到数据 if result then log.info("mqttTest.mqttInMsgProc",data.topic,string.toHex(data.payload)) --TODO:根据需求自行处理data.payload else break end end return result or data=="timeout" end
functionmqttInMsgProc(mqttClient) local result,data whiletruedo result,data = mqttClient:receive(2000) --接收到数据 if result then log.info("mqttTest.mqttInMsgProc",data.topic,string.toHex(data.payload)) --TODO:根据需求自行处理data.payload --如果msgQuene中有等待发送的数据,则立即退出本循环 if waitForSend() thenreturntrueend else break end end return result or data=="timeout" end
functionmqttOutMsgProc(mqttClient) while #msgQuene>0do--数组大于零? local outMsg = table.remove(msgQuene,1)--取出并删除一个元素 local result = mqttClient:publish(outMsg.t,outMsg.p,outMsg.q)--推送对应的mqtt消息 if outMsg.user and outMsg.user.cb then--如果存在回调函数 outMsg.user.cb(result,outMsg.user.para)--执行回调函数 end ifnot result thenreturnend end returntrue end
outMsg.user即为消息队列数组中的,消息数组中的,包含了回调函数的,数组(反正挺绕的)
具体就像下面这样用:
1 2 3 4 5
insertMsg("/d/test/123","done",{cb=testcb})
localfunctiontestcb() log.info("test.testcb","test message sent") end
--测试用的服务器信息,按需求自己改 local mqttip,mqttport,mqttuser,mqttpassword,mqttheartbeat = "x.x.x.x","xxxx","user","password",600
--数据发送的消息队列 local msgQuene = {}
localfunctioninsertMsg(topic,payload,qos,user) table.insert(msgQuene,{t=topic,p=payload,q=qos,user=user}) end
functionwaitForSend() return #msgQuene > 0 end
functionmqttOutMsgProc(mqttClient) while #msgQuene>0do--数组大于零? local outMsg = table.remove(msgQuene,1)--取出并删除一个元素 local result = mqttClient:publish(outMsg.t,outMsg.p,outMsg.q)--推送对应的mqtt消息 if outMsg.user and outMsg.user.cb then--如果存在回调函数 outMsg.user.cb(result,outMsg.user.para)--执行回调函数 end ifnot result thenreturnend end returntrue end
functionmqttInMsgProc(mqttClient) local result,data whiletruedo result,data = mqttClient:receive(2000) --接收到数据 if result then log.info("mqttTest.mqttInMsgProc",data.topic,string.toHex(data.payload)) --TODO:根据需求自行处理data.payload --如果msgQuene中有等待发送的数据,则立即退出本循环 if waitForSend() thenreturntrueend else break end end return result or data=="timeout" end
--启动mqtt客户端任务 sys.taskInit( function() whiletruedo local retryConnectCnt = 0--失败次数统计 --是否获取到了分配的ip(是否连上网) if socket.isReady() then local imei = misc.getImei() --新建一个mqtt对象 local mqttClient = mqtt.client(imei,mqttheartbeat,mqttuser,mqttpassword) --尝试连接指定服务器 if mqttClient:connect(mqttip,mqttport,"tcp") then --连接成功 log.info("mqttTest.mqttClient","connect success") retryConnectCnt = 0--失败次数清零 --订阅主题 if mqttClient:subscribe({["/s/test/"..imei]=0}) then --循环处理接收和发送的数据 whiletruedo ifnot mqttInMsgProc(mqttClient) then log.error("mqttTest.mqttInMsgProc error") break end ifnot mqttOutMsgProc(mqttClient) then log.error("mqttTest.mqttOutMsgProc error") break end end end else log.info("mqttTest.mqttClient","connect fail")--连接失败 retryConnectCnt = retryConnectCnt+1--失败次数加一 end --断开MQTT连接 mqttClient:disconnect() if retryConnectCnt>=5then link.shut() retryConnectCnt=0 end sys.wait(5000) else --没连上网 --等待网络环境准备就绪,超时时间是5分钟 sys.waitUntil("IP_READY_IND",300000) --等完了还没连上? ifnot socket.isReady() then --进入飞行模式,20秒之后,退出飞行模式 net.switchFly(true) sys.wait(20000) net.switchFly(false) end end end end)
--测试用的服务器信息,按需求自己改 local mqttip,mqttport,mqttuser,mqttpassword,mqttheartbeat = "x.x.x.x","xxxx","user","password",600
--数据发送的消息队列 local msgQuene = {}
localfunctioninsertMsg(topic,payload,qos,user) table.insert(msgQuene,{t=topic,p=payload,q=qos,user=user}) end
functionwaitForSend() return #msgQuene > 0 end
functionmqttOutMsgProc(mqttClient) while #msgQuene>0do--数组大于零? local outMsg = table.remove(msgQuene,1)--取出并删除一个元素 local result = mqttClient:publish(outMsg.t,outMsg.p,outMsg.q)--推送对应的mqtt消息 if outMsg.user and outMsg.user.cb then--如果存在回调函数 outMsg.user.cb(result,outMsg.user.para)--执行回调函数 end ifnot result thenreturnend end returntrue end
functionmqttInMsgProc(mqttClient) local result,data whiletruedo result,data = mqttClient:receive(2000) --接收到数据 if result then log.info("mqttTest.mqttInMsgProc",data.topic,string.toHex(data.payload)) --TODO:根据需求自行处理data.payload if data.topic == "/s/test/"..misc.getImei() and data.payload == "ok"then insertMsg("/d/test/"..misc.getImei(),"done") end --如果msgQuene中有等待发送的数据,则立即退出本循环 if waitForSend() thenreturntrueend else break end end return result or data=="timeout" end
--启动mqtt客户端任务 sys.taskInit( function() whiletruedo local retryConnectCnt = 0--失败次数统计 --是否获取到了分配的ip(是否连上网) if socket.isReady() then local imei = misc.getImei() --新建一个mqtt对象 local mqttClient = mqtt.client(imei,mqttheartbeat,mqttuser,mqttpassword) --尝试连接指定服务器 if mqttClient:connect(mqttip,mqttport,"tcp") then --连接成功 log.info("mqttTest.mqttClient","connect success") retryConnectCnt = 0--失败次数清零 --订阅主题 if mqttClient:subscribe({["/s/test/"..imei]=0}) then insertMsg("/d/test/"..misc.getImei(),sim.getIccid()) --循环处理接收和发送的数据 whiletruedo ifnot mqttInMsgProc(mqttClient) then log.error("mqttTest.mqttInMsgProc error") break end ifnot mqttOutMsgProc(mqttClient) then log.error("mqttTest.mqttOutMsgProc error") break end end end else log.info("mqttTest.mqttClient","connect fail")--连接失败 retryConnectCnt = retryConnectCnt+1--失败次数加一 end --断开MQTT连接 mqttClient:disconnect() if retryConnectCnt>=5then link.shut() retryConnectCnt=0 end sys.wait(5000) else --没连上网 --等待网络环境准备就绪,超时时间是5分钟 sys.waitUntil("IP_READY_IND",300000) --等完了还没连上? ifnot socket.isReady() then --进入飞行模式,20秒之后,退出飞行模式 net.switchFly(true) sys.wait(20000) net.switchFly(false) end end end end)