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


Messages In This Thread
MQTT Broker Project with 2024 FW Problem - by savaskorkmaz - 19.09.2024, 15:21

Forum Jump: