Skip to content

Instantly share code, notes, and snippets.

@Annath
Created February 24, 2017 01:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Annath/0098672d0da66b210feea6acd80bf0a2 to your computer and use it in GitHub Desktop.
Save Annath/0098672d0da66b210feea6acd80bf0a2 to your computer and use it in GitHub Desktop.
-- initiate the mqtt client and set keepalive timer to 120sec
mqtt2 = mqtt.Client("huzzah", 120, "user", "pass")
mqtt2:on("connect", function(con) print ("connected") end)
mqtt2:on("offline", function(con) print ("offline") end)
-- on receive message
mqtt2:on("message", function(conn, topic, data)
print(topic .. ":" )
if data ~= nil then
print(data)
end
end)
mqtt2:connect("m13.cloudmqtt.com", 12759, 0, function(conn)
print("connected")
-- subscribe topic with qos = 0
mqtt2:subscribe("my_topic",0, function(conn)
-- publish a message with data = my_message, QoS = 0, retain = 0
mqtt2:publish("my_topic","my_message",0,0, function(conn)
print("sent")
end)
end)
end,
function(conn, reason)
print("failed: ", reason)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment