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