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
#24
Your topic names are incorrect. On top of that the data is published in JSON so you cannot write it directly to objects. Here's a working example for the multisensor topic:
Code:
if not broker then   json = require('json')   broker = 'mqtt.intingy.com'   mqtt_to_local = {     ['entelechy/multisensor/UNKNOWN-ROOM/E5001000143C'] = function(payload)       local data = json.pdecode(payload)       if type(data) == 'table' then         grp.checkupdate('32/1/2', data['Temperature'])         grp.checkupdate('32/1/3', data['Relative Humidity'])         grp.checkupdate('32/1/4', data['Illumination'])       end     end,   }   -- object to topic map   object_to_mqtt = {}   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_client', 'nNu6Unh0E/4')   mclient.ON_CONNECT = function(res, ...)     log('mqtt connect status', res, ...)     if res then       for topic, _ in pairs(mqtt_to_local) do         mclient:subscribe(topic)       end     else       mclient:disconnect()     end   end   mclient.ON_MESSAGE = function(mid, topic, payload)     local fn = mqtt_to_local[ topic ]     if fn then       fn(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

More topic payload conversion functions can be added to the mqtt_to_local table as needed.
Reply


Messages In This Thread
mqtt with logic machine - by abby - 01.05.2024, 13:36
RE: mqtt with logic machine - by admin - 01.05.2024, 13:57
RE: mqtt with logic machine - by abby - 23.05.2024, 14:15
RE: mqtt with logic machine - by admin - 23.05.2024, 14:39
RE: mqtt with logic machine - by abby - 24.05.2024, 08:58
RE: mqtt with logic machine - by admin - 24.05.2024, 09:01
RE: mqtt with logic machine - by abby - 24.05.2024, 09:37
RE: mqtt with logic machine - by admin - 24.05.2024, 09:41
RE: mqtt with logic machine - by abby - 24.05.2024, 13:14
RE: mqtt with logic machine - by admin - 24.05.2024, 13:29
RE: mqtt with logic machine - by abby - 27.05.2024, 09:35
RE: mqtt with logic machine - by admin - 27.05.2024, 09:41
RE: mqtt with logic machine - by abby - 27.05.2024, 09:57
RE: mqtt with logic machine - by admin - 27.05.2024, 09:59
RE: mqtt with logic machine - by abby - 27.05.2024, 10:12
RE: mqtt with logic machine - by admin - 27.05.2024, 10:28
RE: mqtt with logic machine - by abby - 27.05.2024, 11:21
RE: mqtt with logic machine - by admin - 27.05.2024, 11:51
RE: mqtt with logic machine - by abby - 27.05.2024, 12:39
RE: mqtt with logic machine - by admin - 28.05.2024, 08:28
RE: mqtt with logic machine - by abby - 05.06.2024, 12:41
RE: mqtt with logic machine - by admin - 05.06.2024, 12:45
RE: mqtt with logic machine - by abby - 05.06.2024, 12:56
RE: mqtt with logic machine - by admin - 05.06.2024, 13:19
RE: mqtt with logic machine - by abby - 05.06.2024, 14:08
RE: mqtt with logic machine - by admin - 05.06.2024, 14:17
RE: mqtt with logic machine - by abby - 17.06.2024, 06:52

Forum Jump: