mqtt with logic machine - abby - 01.05.2024
how to subscribe to mqtt data using logic machine
RE: mqtt with logic machine - admin - 01.05.2024
https://kb.logicmachine.net/integration/mqtt-client/
RE: mqtt with logic machine - abby - 23.05.2024
Hello sir i want to publish these data
mosquitto_pub -h mqtt.intingy.com -u entelechy_gateway -p 1883 -P "wr6NOng8HzFC2KpB5cPoxXXgCSYBHXbGgRG9b3R39ok" -t "entelechy/" -m "mqtt_connection_test"
bit in lm when I publish it in the log it shows empty and nothing shows in it .
RE: mqtt with logic machine - admin - 23.05.2024
Use the first example from our knowledge base. Change variables in the first 6 lines as needed.
RE: mqtt with logic machine - abby - 24.05.2024
thank you sir it got connected but the data from the another side is not coming I am having an error
RE: mqtt with logic machine - admin - 24.05.2024
"not authorized" error means wrong username and/or password.
This works for me:
Code: broker = 'mqtt.intingy.com'
port = 1883
username = 'entelechy_gateway'
password = 'wr6NOng8HzFC2KpB5cPoxXXgCSYBHXbGgRG9b3R39ok'
topic = 'entelechy/'
payload = 'mqtt_connection_test'
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()
log('publish ok')
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
RE: mqtt with logic machine - abby - 24.05.2024
thankyou sir it is working .now I want to receive data what should I do to receive data .
RE: mqtt with logic machine - admin - 24.05.2024
Second example here (Object value exchange via MQTT): https://kb.logicmachine.net/integration/mqtt-client/
RE: mqtt with logic machine - abby - 24.05.2024
I have tried this code but it is showing some error
RE: mqtt with logic machine - admin - 24.05.2024
Check this: https://forum.logicmachine.net/showthread.php?tid=1759&pid=33643#pid33643
RE: mqtt with logic machine - abby - 27.05.2024
I have tried this code but it showing some error
Code: if not broker then
broker = '192.168.29.48'
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 = {
['in/topic1'] = '32/1/1',
['in/topic2'] = '32/1/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 = {
['1/1/1'] = 'out/topic1',
['1/1/2'] = 'out/topic2',
}
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 fd
mclient:connect(broker)
fd = mclient:socket()
-- fd ref is valid
if fd then
mclientfd = fd
end
end
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 mclientfd and 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
RE: mqtt with logic machine - admin - 27.05.2024
After mclient = require('mosquitto').new() (line 45) add this:
Code: mclient:login_set('USERNAME', 'PASSWORD')
Replace USERNAME/PASSWORD with real username and password.
RE: mqtt with logic machine - abby - 27.05.2024
still showing the same error
Code: if not broker then
broker = '192.168.29.48'
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 = {
['in/topic1'] = '32/1/1',
['in/topic2'] = '32/1/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 = {
['1/1/1'] = 'out/topic1',
['1/1/2'] = 'out/topic2',
}
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:login_set('entelechy_gateway', 'wr6NOng8HzFC2KpB5cPoxXXgCSYBHXbGgRG9b3R39ok')
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 fd
mclient:connect(broker)
fd = mclient:socket()
-- fd ref is valid
if fd then
mclientfd = fd
end
end
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 mclientfd and 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
RE: mqtt with logic machine - admin - 27.05.2024
Shouldn't broker address be 'mqtt.intingy.com'?
RE: mqtt with logic machine - abby - 27.05.2024
yes I have change it error are gone now And were will the data will be show I have to show the data into graph format could you please help me with that
RE: mqtt with logic machine - admin - 27.05.2024
Map MQTT topic to a group address (mqtt_to_object table in the script). Then create a trend log attached to this group address.
RE: mqtt with logic machine - abby - 27.05.2024
sir i have tried it but it is showing error
local mqtt_to_object = {
["mqtt.intingy.com"] = "32/1/1",
["test1"] = "32/1/2",
}
local function create_trend_log("32/1/1")
print("TEMP", '32/1/1')
end
for mqtt_topic, group_address in pairs(mqtt_to_object) do
create_trend_log('32/1/1')
end
RE: mqtt with logic machine - admin - 27.05.2024
Check LM manual page 48 on how to create Trend logs: https://openrb.com/wp-content/uploads/2022/05/LM-manual_3.2022.pdf
Contact the seller where you've got LM from for some basic training. Without this it will be very hard to achieve any meaningful results.
RE: mqtt with logic machine - abby - 27.05.2024
sir sorry for the unusual question but I know how to make trend logs
RE: mqtt with logic machine - admin - 28.05.2024
You only need to modify this table:
Code: mqtt_to_object = {
['in/topic1'] = '32/1/1',
['in/topic2'] = '32/1/2',
}
In this example data published to topic named in/topic1 is sent to group address 32/1/1
So you need to adjust the topic name and group address as needed. Then in LM UI create a trend log that is attached to the same group address.
Depending on the trend log settings it might take up to several hours for the chart to start displaying values.
|