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-Client script stopped sending Units
#1
Hi,

I have LM5Lp2 with BluHome-Addon for three FreeAir 100 ventilation devices. I used the Client-Script https://kb.logicmachine.net/integration/mqtt-client/, my configuration as attachement. Initially, MQTT sent units with the measurements, but since some days units are missing. I would like to have the units send again, what have I to do? Thanks!

Attached Files Thumbnail(s)
   
.txt   mqtt-client 2024-07-18.txt (Size: 3.11 KB / Downloads: 9)
Reply
#2
Modified example that will publish value + units:
Code:
if not broker then
  broker = '192.168.1.101'

  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 = {}

  -- optional topic value conversion function
  mqtt_to_object_conv = {}

  -- object to topic map
  object_to_mqtt = {}

  units = {}
  datatypes = {}

  objs = grp.tag('mqtt-control')
  for _, obj in ipairs(objs) do
    object_to_mqtt[ obj.address ] = obj.name
    units[ obj.address ] = obj.units
    datatypes[ obj.address ] = obj.datatype
  end

  objs = grp.tag('mqtt-status')
  for _, obj in ipairs(objs) do
    mqtt_to_object[ obj.name ] = obj.address
  end

  grp.sender = 'mq'
  require('socket')

  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

      value = tostring(value) .. (units[ addr ] or '')
      mclient:publish(topic, 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
#3
Thanks for the quick reply. I changed the script an restarted it. Now the broker gets no data at all, the log shows:

* arg: 1
  * string: mqtt connect status
* arg: 2
  * bool: true
* arg: 3
  * number: 0
* arg: 4
  * string: connection accepted
* arg: 5
  * number: 0
* arg: 6
  * nil

Did I forget anything?
Reply
#4
Values are published only on change. Change any object with mqtt-control tag and the new value will be published.
Reply


Forum Jump: