30.11.2018, 17:39
Hello,
I'm trying to subscribe to a MQTT topic ("arqueta/humedad") and asign his value to an object of logic machine.
I'm using de test Mosquitto broker and I publish previously a value to the topic "arqueta/humedad".
I'm using this code (extracted from here) as a resident script but It seems doesn't works and I don't know what I'm doing wrong...
I'm trying to subscribe to a MQTT topic ("arqueta/humedad") and asign his value to an object of logic machine.
I'm using de test Mosquitto broker and I publish previously a value to the topic "arqueta/humedad".
I'm using this code (extracted from here) as a resident script but It seems doesn't works and I don't know what I'm doing wrong...
Code:
-- Define a function which is called by mqtt_client:handler(),
-- whenever messages are received on the subscribed topics
function callback(topic, message)
print("Received: " .. topic .. ": " .. message)
if (message == "quit") then running = false end
end
-- Create an MQTT client instance, connect to the MQTT server and
-- subscribe to the topic called "arqueta/humedad"
MQTT = require("mosquitto")
MQTT.Utility.set_debug(true)
mqtt_client = MQTT.client.create("https://test.mosquitto.org", 1883, callback)
mqtt_client:connect("logicmachine")
local humedad = mqtt_client:subscribe("arqueta/humedad")
grp.update('1/1/27', humedad)
-- Continously invoke mqtt_client:handler() to process the MQTT protocol and
-- handle any received messages. Also, publish a message on topic "test/1"
running = true
while (running) do
mqtt_client:handler()
mqtt_client:publish("test/1", "test message")
socket.sleep(1.0) -- seconds
end
-- Clean-up by unsubscribing from the topic and closing the MQTT connection
mqtt_client:unsubscribe({"arqueta/humedad"})
mqtt_client:destroy()