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.

M-Bus (Meter-Bus) library and examples
#15
The script only handles cases when a single value is needed from each meter. It can be modified like this to be able to specify group address for each meter entry:
Code:
-- config init
if not meters then
  require('mbus')

  -- time to wait between each meter read
  sleeptime = 10
  -- use /dev/RS232 serial port with 2400 baud rate
  mbus.init('/dev/RS232', 2400)
  -- base address for meter values, meter short address will be added to form the meters group address
  base = '2/1/'

  -- meter definition
  -- addr - short address
  -- id - number from data table where value resides
  -- div - optional divisor for conversion to the final value
  meters = {
    { addr = 1, id = 2, div = 1000, groupaddr = '1/1/1' }, -- hot water meter, convert from m3 to liters
    { addr = 2, id = 2, div = 1000, groupaddr = '1/1/2' }, -- cold water meter, convert from m3 to liters
    { addr = 3, id = 5, groupaddr = '1/1/3' }, -- heating meter 1
    { addr = 4, id = 5, groupaddr = '1/1/4' }, -- heating meter 2
  }

  -- reset meter values on first run
  for _, meter in ipairs(meters) do
    meter.value = 0
  end
end

-- read each meter
for _, meter in ipairs(meters) do
  res = mbus.getdata(meter.addr)

  -- read ok
  if type(res) == 'table' then
    data = res.data[ meter.id ]
    value = nil

    -- get current value
    if type(data) == 'table' then
      value = tonumber(data.value)
    end

    -- value is valid and different from previous read
    if value and meter.value ~= value then
      -- apply divisor if set
      div = meter.div
      dpt = div and dt.float32 or dt.uint32
      if div then
        value = value / div
      end

      -- update group address value
      addr = meter.groupaddr or (base .. tostring(meter.addr))
      grp.update(addr, value, dpt)
      meter.value = value
    end
  end

  -- wait for next read
  os.sleep(sleeptime)
end
Reply


Messages In This Thread
RE: M-Bus (Meter-Bus) library and examples - by admin - 03.08.2022, 07:33

Forum Jump: