26.08.2022, 14:05
(26.08.2022, 12:55)admin Wrote: You can create a scheduled script that runs every minute and publishes all values. Each message has to specified manually due to a variable format. Add as many addmessage calls as needed and change grp.getvalue addresses. The script will automatically disconnect once all messages are published.
Thank you very much admin. Always a life saverCode:broker = 'mqtt.eniris.be'
username = 'xxx'
password = 'xxx'
port = 1883
messages = {}
time = os.time()
function addmessage(topic, data)
messages[ #messages + 1 ] = {
topic = topic,
data = require('json').encode(data),
}
end
addmessage('standard1/rp_one_m/waterMetrics/' .. username, {
time = time,
extraTags = {
subId = 'T19',
},
fields = {
absVolume_m3 = grp.getvalue('1/1/1'),
flow_m3ph = grp.getvalue('1/1/2'),
}
})
addmessage('standard1/rp_one_m/weatherMetrics/' .. username, {
time = time,
extraTags = {
subId = 'T19',
},
fields = {
outdoorTemperature_degC = grp.getvalue('1/1/3'),
outdoorBrightness_lux = grp.getvalue('1/1/4'),
windSpeed_kmph = grp.getvalue('1/1/5'),
}
})
addmessage('standard1/rp_one_m/gasMetrics/' .. username, {
time = time,
extraTags = {
subId = 'T19',
},
fields = {
absVolume_Nm3 = grp.getvalue('1/1/6'),
}
})
mqtt = require('mosquitto')
client = mqtt.new()
mids = {}
pubcount = 0
client.ON_CONNECT = function(status, rc, msg)
if status then
log('mqtt cloud connected')
for _, message in ipairs(messages) do
local mid = client:publish(message.topic, message.data)
if mid then
pubcount = pubcount + 1
mids[ mid ] = true
end
end
if pubcount == 0 then
client:disconnect()
end
else
log('mqtt cloud connect failed ' .. tostring(msg))
client:disconnect()
end
end
client.ON_PUBLISH = function(mid)
if mids[ mid ] then
mids[ mid ] = nil
pubcount = pubcount - 1
if pubcount == 0 then
log('disconnecting')
client:disconnect()
end
end
end
client:login_set(username, password)
status, rc, msg = client:connect(broker, port)
if status then
client:loop_forever()
else
log('cloud connect failed: ' .. tostring(msg))
end