Post your best Lua script examples here

User avatar
By sigrokBlack
#15827 Hi,
i want to read a message from a mqtt topic.
Posting with this:
Code: Select allm:publish("Led","hello",0,0, function(con) print("published") end)

Is no problem but how can I read messages of the topic?
Code: Select allm:subscribe("Led",0, function(conn) print("subscribe success") end)
m:on("message", function(conn, topic, data)
  print(topic .. ":" )
  if data ~= nil then
    print(data)
  end
end)

Throws nothing back, but there are messages on the topic?!

Greetings from Germany
Joshua
User avatar
By aniston
#16909 Hello Joshua,
Your code works for both PUBLISH and SUBSCRIBE ;)

You don't see your subsequent Subscribe after your Publish code line as you have no RETAIN=1 but you are Subscribed (as long as you are returned "subscribe success")

simple check is to Publish again without the print("message")
Code: Select allm:publish("Led","HELLO_WORLD",0,0, function(con) end)
and if you are "connected" && have "subscribe success" then you should successfully see "HELLO_WORLD"

For My test setups I send a Publish event to topic "Led" from another device , I use MyMQTT App on Android.

HTH,
aniston.
User avatar
By j0hncc
#16912 What aniston said.

But also-- are you executing code you posted in that order? If so, try just execute it in the opposite order:
Code: Select allm:subscribe("Led"...
m:on("message"...
m:publish("Led"...

Cheers,
John