This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

mqtt with logic machine
#1
how to subscribe to mqtt data using logic machine
Reply
#2
https://kb.logicmachine.net/integration/mqtt-client/
Reply
#3
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 .
Reply
#4
Use the first example from our knowledge base. Change variables in the first 6 lines as needed.
Reply
#5
thank you sir it got connected but the data from the another side is not coming I am having an error

Attached Files Thumbnail(s)
       
Reply
#6
"not authorized" error means wrong username and/or password.

This works for me:
Code:
123456789101112131415161718192021222324252627282930313233
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
Reply
#7
thankyou sir it is working .now I want to receive data what should I do to receive data .

Attached Files Thumbnail(s)
   
Reply
#8
Second example here (Object value exchange via MQTT): https://kb.logicmachine.net/integration/mqtt-client/
Reply
#9
I have tried this code but it is showing some error

Attached Files Thumbnail(s)
   
Reply
#10
Check this: https://forum.logicmachine.net/showthrea...3#pid33643
Reply
#11
I have tried this code but it showing some error 

Code:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
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

Attached Files Thumbnail(s)
   
Reply
#12
After mclient = require('mosquitto').new() (line 45) add this:
Code:
1
mclient:login_set('USERNAME', 'PASSWORD')

Replace USERNAME/PASSWORD with real username and password.
Reply
#13
still showing the same error

Code:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
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
Reply
#14
Shouldn't broker address be 'mqtt.intingy.com'?
Reply
#15
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
Reply
#16
Map MQTT topic to a group address (mqtt_to_object table in the script). Then create a trend log attached to this group address.
Reply
#17
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
Reply
#18
Check LM manual page 48 on how to create Trend logs: https://openrb.com/wp-content/uploads/20...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.
Reply
#19
sir sorry for the unusual question but I know how to make trend logs
Reply
#20
You only need to modify this table:
Code:
1234
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.
Reply


Forum Jump: