Skip to content

Instantly share code, notes, and snippets.

@bakroistvan
Created December 12, 2019 21:08
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 bakroistvan/b06a5a456ba7d93887e6857d26c30740 to your computer and use it in GitHub Desktop.
Save bakroistvan/b06a5a456ba7d93887e6857d26c30740 to your computer and use it in GitHub Desktop.
nodemcu-mqtt
-- initiate the mqtt client and set keepalive timer to 120sec
mqtt = mqtt.Client("client_id", 120, "<<user>>", "<<pass>>")
mqtt:on("connect", function(con) print ("connected") end)
mqtt:on("offline", function(con) print ("offline") end)
-- on receive message
mqtt:on("message", function(conn, topic, data)
print(topic .. ":" )
if data ~= nil then
print(data)
end
end)
mqtt:connect("farmer.cloudmqtt.com", 11253, 0, function(conn)
print("connected")
-- subscribe topic with qos = 0
mqtt:subscribe("my_topic",0, function(conn)
-- publish a message with data = my_message, QoS = 0, retain = 0
mqtt:publish("my_topic","my_message",0,0, function(conn)
print("my_message sent")
end)
end)
end)
station_cfg={}
station_cfg.ssid="bagameri"
station_cfg.pwd="jelszavacskam"
station_cfg.save=true
wifi.sta.config(station_cfg)
function connected_cb(SSID, BSSID, ch)
print(SSID)
print(BSSID)
print(ch)
end
wifi.sta.connect(connected_cb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment