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.

[OLD] LogicMachine firmware 2019.10
#18
Hi,

I recently install the new firmare and MQTT don´t works . Just receive but don´t send MQTT..(with previous version it works fine)

Has something changed?
Code:
if not broker then
  broker = 'MosquitoBroker'

  mqtt_to_object = {
    ['MyObject'] = 'command...',
  }

 

  object_to_mqtt = {
    ['MyObject'] = 'command...',
  } 

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:login_set('username', 'password')

  mclient.ON_CONNECT = function()
    --log('mqtt connected')
    if grp.getvalue('33/1/116') then WriteSendTo("David","MQTT Conectado") end
    for topic, _ in pairs(mqtt_to_object) do
      mclient:subscribe(topic)
    end
  end

  mclient.ON_MESSAGE = function(mid, topic, payload)
    local addr = mqtt_to_object[ topic ]
    if addr then
      grp.write(addr, payload)
    end
  end

  mclient.ON_DISCONNECT = function(...)
    --log('mqtt disconnect', ...)
    if grp.getvalue('33/1/116') then WriteSendTo("David","MQTT Desconectado") end
    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
  --log("mclientfd")
  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


Thanks


Messages In This Thread
RE: [PRE-RELEASE 1] LogicMachine firmware 2019.10 - by DGrandes - 22.11.2019, 18:53

Forum Jump: