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 Broker Project with 2024 FW Problem
#1
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

Attached Files
.zip   Project-LogicMachine-2024.09.19-18.14.zip (Size: 129.15 KB / Downloads: 4)
Reply
#2
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
------------------------------
Ctrl+F5
Reply
#3
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
Reply
#4
MQTT Broker has no points it is just a messenger between clients. The script you are sharing is MQTT client.
------------------------------
Ctrl+F5
Reply
#5
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.
Reply
#6
H,
I am sharing 3 screenshots. Initial screen of MQTT Explorer, connected  screen of MQTT Exporer, MQTT Broker app screenshot.

Attached Files Thumbnail(s)
           
Reply
#7
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
Reply
#8
Hi,
Thx for your support. It's working wery well now.
Regards
Reply


Forum Jump: