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