broker = "localhost"
port = 1883
username = "admin"
password = "password"
topic = "casambi/1/set/target_level"
value = event.getvalue()
if value == 1
then
topic = "casambi/1/set/target_level"
payload = tostring"("level":255,"duration":1,"targetid":177,"targettype":1,)"
else
payload = tostring"("level":0,"duration":1,"targetid":177,"targettype":1,)"
end
mqtt = require("mosquitto")
client = mqtt.new()
client.ON_CONNECT = function(status, rc, msg)
if status then
log("mqtt connected")
client:publish(topic, tostring(payload))
else
log("mqtt connect failed " .. tostring(msg))
client:disconnect()
end
end
client.ON_PUBLISH = function()
client:disconnect()
end
client:login_set(username, password)
status, rc, msg = client:connect(broker, port)
if status then
client:loop_forever()
else
log("connect failed: " .. tostring(msg))
end
Error log shows:
attempt to call global 'level' (a nil value)
stack traceback:
port = 1883
username = "admin"
password = "password"
topic = "casambi/1/set/target_level"
value = event.getvalue()
if value == 1
then
topic = "casambi/1/set/target_level"
payload = tostring"("level":255,"duration":1,"targetid":177,"targettype":1,)"
else
payload = tostring"("level":0,"duration":1,"targetid":177,"targettype":1,)"
end
mqtt = require("mosquitto")
client = mqtt.new()
client.ON_CONNECT = function(status, rc, msg)
if status then
log("mqtt connected")
client:publish(topic, tostring(payload))
else
log("mqtt connect failed " .. tostring(msg))
client:disconnect()
end
end
client.ON_PUBLISH = function()
client:disconnect()
end
client:login_set(username, password)
status, rc, msg = client:connect(broker, port)
if status then
client:loop_forever()
else
log("connect failed: " .. tostring(msg))
end
Error log shows:
attempt to call global 'level' (a nil value)
stack traceback: