2015-04-04 20:22:51 +02:00
|
|
|
-- test with cloudmqtt.com
|
2019-12-31 00:53:54 +01:00
|
|
|
local m_dis = {}
|
|
|
|
|
|
|
|
local function dispatch(m,t,pl)
|
|
|
|
if pl~=nil and m_dis[t] then
|
|
|
|
m_dis[t](m,pl)
|
|
|
|
end
|
2015-04-04 20:22:51 +02:00
|
|
|
end
|
2019-12-31 00:53:54 +01:00
|
|
|
|
|
|
|
local function topic1func(_,pl)
|
|
|
|
print("get1: "..pl)
|
2015-04-04 20:22:51 +02:00
|
|
|
end
|
2019-12-31 00:53:54 +01:00
|
|
|
|
|
|
|
local function topic2func(_,pl)
|
|
|
|
print("get2: "..pl)
|
2015-04-04 20:22:51 +02:00
|
|
|
end
|
2019-12-31 00:53:54 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
m_dis["/topic1"] = topic1func
|
|
|
|
m_dis["/topic2"] = topic2func
|
|
|
|
-- Lua: mqtt.Client(clientid, keepalive, user, pass)
|
|
|
|
local m = mqtt.Client("nodemcu1", 60, "test", "test123")
|
|
|
|
m:on("connect",function(client)
|
|
|
|
print("connection "..node.heap())
|
|
|
|
client:subscribe("/topic1",0,function() print("sub done") end)
|
|
|
|
client:subscribe("/topic2",0,function() print("sub done") end)
|
|
|
|
client:publish("/topic1","hello",0,0)
|
|
|
|
client:publish("/topic2","world",0,0)
|
|
|
|
end)
|
|
|
|
m:on("offline", function()
|
2015-04-04 20:22:51 +02:00
|
|
|
print("disconnect to broker...")
|
|
|
|
print(node.heap())
|
2019-12-31 00:53:54 +01:00
|
|
|
end)
|
|
|
|
m:on("message",dispatch )
|
|
|
|
-- Lua: mqtt:connect( host, port, secure, function(client) )
|
|
|
|
m:connect("m11.cloudmqtt.com",11214,0)
|
|
|
|
tmr.create():alarm(10000, tmr.ALARM_AUTO, function()
|
|
|
|
local pl = "time: "..tmr.time()
|
|
|
|
m:publish("/topic1",pl,0,0)
|
|
|
|
end)
|
|
|
|
end
|