mqtt服務(wù)簡單好用述么,剛好nodemcu又提供了mqtt這個模塊:
基本都是網(wǎng)上源代碼的修改了用戶名和密碼疾棵,服務(wù)器名稱就可以連接上。簡單實用
tmr.alarm(4, 1000, tmr.ALARM_AUTO, function()
if wifi.sta.getip()~= nil then
clientid=node.chipid()
print("Chipid is:"..clientid)
m = mqtt.Client(clientid, 120, "yonghuming", "mima")
m:connect("5iot.top", 1883, 0, function(client)--我的服務(wù)器
print("connected0")
-- Calling subscribe/publish only makes sense once the connection
-- was successfully established. You can do that either here in the
-- 'connect' callback or you need to otherwise make sure the
-- connection was established (e.g. tracking connection status or in
-- m:on("connect", function)).
-- subscribe topic with qos = 0
--m:subscribe("test-1", 0, function(client) print("subscribe success") end)
-- publish a message with data = hello, QoS = 0, retain = 0
-- m:publish("test-1", "hello", 0, 0, function(client) print("sent") end)
end,
function(client, reason)
print("failed reason: " .. reason)
end)
m:on("connect", function(client) print ("connected1")--連接上
status1=1
m:subscribe("test-1", 0, function(client) print("subscribe success") end)
end)--訂閱主題
m:on("offline", function(client) print ("offline")
tmr.start(4)
end)--下線后重連
m:on("message", function(client, topic, data)
print("Recieved Topic:/"..topic .. ":" )
if data ~= nil then
print(data)
end
end)
tmr.stop(4)
end
end)
tmr.alarm(3, 10000, tmr.ALARM_AUTO,function()
if status1==1 then
m:publish("test-1", "hello", 0, 0, function(client) print("sent") end)
gpio.write(led1, gpio.LOW);
tmr.delay(1000)
gpio.write(led1, gpio.HIGH);
end
end)
這是自己的服務(wù)器沿后,下一步連接中移動的ONENET服務(wù)器诲宇,采用MQTT連接际歼,但協(xié)議有部分不同,需要打包協(xié)議姑蓝。