sync.lua
local mqtt = require("mqtt")
local client = mqtt.client{
uri = "mqtt.flespi.io",
username = "stPwSVV73Eqw5LSv0iMXbc4EguS7JyuZR9lxU5uLxI5tiNM8ToTVqNpu85pFtJv9",
clean = true,
}
print("created MQTT client", client)
client:on{
connect = function(connack)
if connack.rc ~= 0 then
print("connection to broker failed:", connack:reason_string(), connack)
return
end
print("connected:", connack)
assert(client:subscribe{ topic="luamqtt/#", qos=1, callback=function(suback)
print("subscribed:", suback)
print('publishing test message "hello" to "luamqtt/simpletest" topic...')
assert(client:publish{
topic = "luamqtt/simpletest",
payload = "hello",
qos = 1
})
end})
end,
message = function(msg)
assert(client:acknowledge(msg))
print("received:", msg)
print("disconnecting...")
assert(client:disconnect())
end,
error = function(err)
print("MQTT client error:", err)
end,
close = function()
print("MQTT conn closed")
end
}
print("running client in synchronous input/output loop")
mqtt.run_sync(client)
print("done, synchronous input/output loop is stopped")