Logic Machine Forum
mqtt client - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: mqtt client (/showthread.php?tid=1132)



mqtt client - JohnTH - 08.12.2017

Hi, anyone got a working example on how to connect to mqtt broker in local network?
How should broker ip,port be set? 

I can`t find any examples on how to do this. The broker has been tested and from nodered i can publish to it.

-John

Code:
mqtt = require("mosquitto")
client = mqtt.new()

client.ON_CONNECT = function()
       client:publish("world", "hello")
       local qos = 1
       local retain = true
 local mid = client:publish("lm/test/1", "my payload", qos, retain)
end

client.ON_PUBLISH = function()
client:disconnect()
end


broker = 10.85.0.4:1883

client:login_set("user","password")
client:connect(broker)
client:loop_forever()



RE: mqtt client - admin - 08.12.2017

Like this, port is optional and defaults to 1883:
Code:
broker = '10.85.0.4'

If you need to set a custom port then pass it as a second parameter to connect:
Code:
client:connect('10.85.0.4', 12345)