01.07.2022, 06:09
In case somebody else needs this, here's an example that logs all messages for all topics from the local broker. If anonymous access is disabled then remove the comment (--) from line 23 and set username and password as needed.
Code:
broker = '127.0.0.1'
mclient = require('mosquitto').new()
mclient.ON_CONNECT = function(res, ...)
log('mqtt connect status', res, ...)
if res then
mclient:subscribe('#')
else
mclient:disconnect()
end
end
mclient.ON_MESSAGE = function(mid, topic, payload)
log('mqtt message', topic, payload)
end
mclient.ON_DISCONNECT = function(...)
log('mqtt disconnect', ...)
end
-- mclient:login_set('test', '123456')
mclient:connect(broker)
mclient:loop_forever()
os.sleep(1)