Logic Machine Forum
MQTT Broker Project with 2024 FW Problem - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: MQTT Broker Project with 2024 FW Problem (/showthread.php?tid=5626)



MQTT Broker Project with 2024 FW Problem - savaskorkmaz - 19.09.2024

Hi,
I attached an old LM project backup which was working as MQTT Broker. I can reach and control the topics via MQTT Explorer. After I upgraded to 2024 fw, i can't see the topics via MQTT Explorer.
I simply created a username and password in the MQTT Broker app. I created a resident script shown below with name MQTT Broker with zero time and i used same username and password in the script which is created in Broker app. It was working but it's not working with 2024 FW as i said before. I can't see the topics via MQTT explorer now.

Can you check script or attached project and tell why it's not working with 2024 FW.

Thx

Code:
if not broker then
  broker = 'localhost'

  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 = {
    ['Magaza1/Sicaklik'] = '1/1/1',
    ['Magaza1/Set_Deger'] = '1/1/2',
    ['Magaza1/Klima_OnOff'] = '1/1/3',
    ['Magaza1/Fan_Hizi'] = '1/1/4',
    ['Magaza1/Klima_Mod'] = '1/1/5',
    ['Magaza2/Sicaklik'] = '1/1/6',
    ['Magaza2/Set_Deger'] = '1/1/7',
    ['Magaza2/Klima_OnOff'] = '1/1/8',
    ['Magaza2/Fan_Hizi'] = '1/1/9',
    ['Magaza2/Klima_Mod'] = '1/1/10',
  }


  -- object to topic map
  object_to_mqtt = {
    ['1/1/1'] = 'Magaza1/Sicaklik',
    ['1/1/2'] = 'Magaza1/Set_Deger',
    ['1/1/3'] = 'Magaza1/Klima_OnOff',
    ['1/1/4'] = 'Magaza1/Fan_Hizi',
    ['1/1/5'] = 'Magaza1/Klima_Mod',
    ['1/1/6'] = 'Magaza2/Sicaklik',
    ['1/1/7'] = 'Magaza2/Set_Deger',
    ['1/1/8'] = 'Magaza2/Klima_OnOff',
    ['1/1/9'] = 'Magaza2/Fan_Hizi',
    ['1/1/10'] = 'Magaza2/Klima_Mod',
  }

 
  mqtt_to_object_conv = {}
  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:login_set('sonaotomasyon', 'sonaotomasyon')
    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 Broker Project with 2024 FW Problem - Daniel - 19.09.2024

Why don't you use MQTT Client app, it is fully working and your setup will be finished in seconds.
https://forum.logicmachine.net/showthread.php?tid=5609


RE: MQTT Broker Project with 2024 FW Problem - savaskorkmaz - 20.09.2024

Hi Daniel,
We have both MQTT Client and MQTT Broker LM's in this project. LM which is used to MQTT Client has less point and we can use it MQTT client app. But in MQTT Broker LM, we have lots of points. To use copy paste function is from excel file much more easy. Therefore we need use resident script in MQTT Broker LM.

Can you check the related script or backup with 2024 fw please which is shared in first post.

Thx


RE: MQTT Broker Project with 2024 FW Problem - Daniel - 20.09.2024

MQTT Broker has no points it is just a messenger between clients. The script you are sharing is MQTT client.


RE: MQTT Broker Project with 2024 FW Problem - admin - 20.09.2024

Post a screenshot of your broken configuration. In MQTT Explorer you need to use the same authentication data as in the script.
Topics won't be created until the mapped object is changed.


RE: MQTT Broker Project with 2024 FW Problem - savaskorkmaz - 20.09.2024

H,
I am sharing 3 screenshots. Initial screen of MQTT Explorer, connected  screen of MQTT Exporer, MQTT Broker app screenshot.


RE: MQTT Broker Project with 2024 FW Problem - admin - 20.09.2024

Try changing object value and the topic will appear in MQTT Explorer.

If you want to keep last topic values for other clients then you need to enable retain flag for messages. To do this replace last line in publishvalue:
Code:
mclient:publish(topic, tostring(value))

With this:
Code:
mclient:publish(topic, tostring(value), 0, true)

Note that LM MQTT broker does not have any persistence. If the broker is restarted then retained topics are lost until new value is published.


Additionally you can modify the script to publish all mapped values after a connection to broker is established. Replace mclient.ON_CONNECT with this:
Code:
  mclient.ON_CONNECT = function(res, ...)
    log('mqtt connect status', res, ...)

    if res then
      for addr, topic in pairs(object_to_mqtt) do
        local value = grp.getvalue(addr)

        if value ~= nil then
          if type(value) == 'boolean' then
            value = value and 1 or 0
          end

          mclient:publish(topic, tostring(value), 0, true)
        end
      end

      for topic, _ in pairs(mqtt_to_object) do
        mclient:subscribe(topic)
      end
    else
      mclient:disconnect()
    end
  end



RE: MQTT Broker Project with 2024 FW Problem - savaskorkmaz - 20.09.2024

Hi,
Thx for your support. It's working wery well now.
Regards