nodemcu-tool upload *.lua && nodemcu-tool terminal

This commit is contained in:
cqc
2021-07-24 04:13:10 -05:00
parent 9b342a7f34
commit 5454e0d9e5
3 changed files with 74 additions and 26 deletions

37
lua/mqtt.lua Normal file
View File

@ -0,0 +1,37 @@
m = mqtt.Client(hostname, 120)
m:lwt("/lwt", "offline", 0, 0)
m:on("connect", function(client) print ("connected") end)
m:on("offline", function(client) print ("offline") end)
m:on("message", function(client, topic, data)
print("MQTT: msg recieved: "..topic)
if data ~= nil then
print(data)
end
end)
-- for TLS: m:connect("192.168.11.118", secure-port, 1)
m:connect("departmentofinter.net", 1883, 0, function(client)
print("connected")
-- 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
client:subscribe("/topic", 0, function(client) print("subscribe success") end)
-- publish a message with data = hello, QoS = 0, retain = 0
client:publish("/topic", "hello", 0, 0, function(client) print("sent") end)
end,
function(client, reason)
print("failed reason: " .. reason)
end)
m:close();
-- you can call m:connect again