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 in Wiser for KNX
#3
(20.05.2020, 12:40)Daniel. Wrote: First we have to see your script. What fw do you use?
Have you seen this thread?
Hi Daniel thanks for replying.

Wiser has the fw 2.4.0 as you can see in the following image.


I use the script that I publish Admin in this thread
https://forum.logicmachine.net/showthrea...light=mqtt
I have created the resident script at interval 0 and changed the IP address to the address of the pc where the mosquitto broker runs.
The object mapping I have used the same as a test.
Code:
if not broker then
  broker = '192.168.0.12'

  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 = {
    ['in/topic1'] = '32/1/1',
    ['in/topic2'] = '32/1/2',
  }

   -- object to topic map
  object_to_mqtt = {
    ['1/1/1'] = 'out/topic1',
    ['1/1/2'] = 'out/topic2',
  }

  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: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 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

Putting the log lines suggested in that same post, this is the result.
MQTT 20.05.2020 16:03:19
Resident script:130: bad argument #2 to 'selectfds' (number expected, got nil)
stack traceback:
[C]: in function 'selectfds'
Is it necessary to install a library or package?
The topics are published and the broker subscribed to them.
Thank you.
a greeting
Reply


Messages In This Thread
MqTT in Wiser for KNX - by JRP - 20.05.2020, 12:11
RE: MqTT in Wiser for KNX - by Daniel - 20.05.2020, 12:40
RE: MqTT in Wiser for KNX - by JRP - 20.05.2020, 13:55
RE: MqTT in Wiser for KNX - by Daniel - 20.05.2020, 13:59
RE: MqTT in Wiser for KNX - by JRP - 20.05.2020, 18:34
RE: MqTT in Wiser for KNX - by Daniel - 21.05.2020, 07:21
RE: MqTT in Wiser for KNX - by JRP - 21.05.2020, 10:40
RE: MqTT in Wiser for KNX - by Daniel - 21.05.2020, 10:42
RE: MqTT in Wiser for KNX - by JRP - 21.05.2020, 11:20
RE: MqTT in Wiser for KNX - by Daniel - 21.05.2020, 11:22
RE: MqTT in Wiser for KNX - by JRP - 21.05.2020, 11:47
RE: MqTT in Wiser for KNX - by Daniel - 21.05.2020, 11:50
RE: MqTT in Wiser for KNX - by JRP - 21.05.2020, 15:28
RE: MqTT in Wiser for KNX - by admin - 26.08.2022, 10:38
RE: MqTT in Wiser for KNX - by admin - 26.08.2022, 11:06
RE: MqTT in Wiser for KNX - by admin - 26.08.2022, 12:55
RE: MqTT in Wiser for KNX - by JRP - 19.05.2023, 10:18
RE: MqTT in Wiser for KNX - by admin - 19.05.2023, 10:24
RE: MqTT in Wiser for KNX - by JRP - 19.05.2023, 10:42
RE: MqTT in Wiser for KNX - by JRP - 20.05.2023, 17:10
RE: MqTT in Wiser for KNX - by admin - 22.05.2023, 06:52
RE: MqTT in Wiser for KNX - by JRP - 26.05.2023, 08:21

Forum Jump: