26.08.2022, 10:24
Hello Erwin,
I have a SpaceLynk (V1.3 FW 2.7.0) that I would like to use as MQTT client and publish data to a cloud platform here in Belgium "Eniris". This device will serve only the purpose of MQTT client.
Could you or anyone assist me with the MQTT client script to be able to publish data in a certain json structured format?
The format should be like mentioned in the link below:
https://wiki.eniris.be/wiki/publicinform...3A%20MQTT/
In short something like this:
Topic = 'standart1/rp_one_m/T/userid/' and then in the data packet the following
I have tested the MQTT client script below and the connection with the broker is ok. And data is being published but not in the certain format sctructure their platform is expecting it.
Thank you in advance for your responce.
Regards
I have a SpaceLynk (V1.3 FW 2.7.0) that I would like to use as MQTT client and publish data to a cloud platform here in Belgium "Eniris". This device will serve only the purpose of MQTT client.
Could you or anyone assist me with the MQTT client script to be able to publish data in a certain json structured format?
The format should be like mentioned in the link below:
https://wiki.eniris.be/wiki/publicinform...3A%20MQTT/
In short something like this:
Topic = 'standart1/rp_one_m/T/userid/' and then in the data packet the following
Code:
{
"time" : 1661506491 , --In Unix format
"extraTags" : {
"subId" : "T19"
},
"fields" : {
"temperature_degC" : 16.76 --this must be the value of the KNX object as a float datatype towards the broker)
}
}
I have tested the MQTT client script below and the connection with the broker is ok. And data is being published but not in the certain format sctructure their platform is expecting it.
Code:
if not broker then
broker = 'mqtt.eniris.be'
username = 'xxx'
password = 'xxx'
socket = require('socket')
port = 1883
function multiply(mult)
return function(value)
local num = tonumber(value)
if num then
return num * mult
else
return value
end
end
end
-- topic to object map
mqtt_to_object = {
['Actual Temp/Hal A/S2'] = '4/0/1',
['Actual Temp/Hal B/S1'] = '4/0/2',
}
-- optional topic value conversion function
--mqtt_to_object_conv = {
-- ['in/topic1'] = multiply(100),
-- ['in/topic2'] = multiply(0.01),
--}
-- object to topic map
object_to_mqtt = {
['4/0/1'] = 'Actual Temp/Hal A/S2',
['4/0/2'] = 'Actual Temp/Hal B/S1',
}
datatypes = {}
grp.sender = 'mq'
require('socket')
for addr, _ in pairs(object_to_mqtt) do
local obj = grp.find(addr)
if obj then
datatypes[ addr ] = obj.datatype
end
end
mclient = require('mosquitto').new()
mclient.ON_CONNECT = function(res, ...)
log('mqtt connect status', res, ...)
if res then
for topic, _ in pairs(mqtt_to_object) do
mclient:subscribe(topic)
end
else
mclient:disconnect()
end
end
mclient.ON_MESSAGE = function(mid, topic, payload)
local addr = mqtt_to_object[ topic ]
if addr then
local fn = mqtt_to_object_conv[ topic ]
if fn then
payload = fn(payload)
end
grp.write(addr, payload)
end
end
mclient.ON_DISCONNECT = function(...)
log('mqtt disconnect', ...)
mclientfd = nil
end
function mconnect()
local status, rc, msg, fd
status, rc, msg = mclient:connect(broker, port)
if not status then
log('mqtt connect failed ' .. tostring(msg))
end
fd = mclient:socket()
if fd then
mclientfd = fd
end
end
--mclient:tls_insecure_set(true)
mclient:login_set(username, password or '')
mconnect()
function publishvalue(event)
-- message from us or client is not connected
if event.sender == 'mq' or not mclientfd then
return
end
local addr = event.dst
local dpt = datatypes[ addr ]
local topic = object_to_mqtt[ addr ]
-- unknown object
if not dpt or not topic then
return
end
local value = busdatatype.decode(event.datahex, dpt)
if value ~= nil then
if type(value) == 'boolean' then
value = value and 1 or 0
end
mclient:publish(topic, tostring(value))
end
end
lbclient = require('localbus').new(1)
lbclient:sethandler('groupwrite', publishvalue)
lbclientfd = socket.fdmaskset(lbclient:getfd(), 'r')
-- run timer every 5 seconds
timer = require('timerfd').new(5)
timerfd = socket.fdmaskset(timer:getfd(), 'r')
end
-- mqtt connected
if mclientfd then
mclientfdset = socket.fdmaskset(mclientfd, mclient:want_write() and 'rw' or 'r')
res, lbclientstat, timerstat, mclientstat =
socket.selectfds(10, lbclientfd, timerfd, mclientfdset)
-- mqtt not connected
else
res, lbclientstat, timerstat =
socket.selectfds(10, lbclientfd, timerfd)
end
if mclientstat then
if socket.fdmaskread(mclientstat) then
mclient:loop_read()
end
if socket.fdmaskwrite(mclientstat) then
mclient:loop_write()
end
end
if lbclientstat then
lbclient:step()
end
if timerstat then
-- clear armed timer
timer:read()
if mclientfd then
mclient:loop_misc()
else
mconnect()
end
end
Thank you in advance for your responce.
Regards