M-Bus
M-Bus Functions
This category contains functions specific to M-Bus devices.
| API Name | Brief Description |
|---|---|
| api.mbusTransaction() | Sends and receives M-Bus frames. Multiple variants. See below. |
| api.mbusSetup(baudRate, parity, stopBits, dataBits) | Configures the M-Bus communication interface. |
| api.mbusState(state) | Controls the M-Bus circuitry. |
| api.mbusScan(filter, timeout, mode) | Scans for M-Bus devices. |
| api.mbusFilter() | Creates and manages internal table of secondary addresses. Multiple variants. See below. |
| api.mbusVifDifFilter() | Filters received M-Bus frames by given group of bytes (VIF/DIF). Multiple variants. See below. |
api.mbusTransaction()
Sends and receives M-Bus frames.
Tip: Ensure M-Bus is enabled by first calling
api.mbusState(1).
| API Name | Brief Description |
|---|---|
| api.mbusTransaction(msg, timeout, retry) | Sends a custom M-Bus message. |
| api.mbusTransaction(index, timeout, retry) | Sends a readout request with the specified secondary address. |
api.mbusTransaction(msg, timeout, retry)
api.mbusTransaction(pack.pack("<b4", 0x10, 0x50, 0x30, 0x16), 5000, 1)
Sends a custom M‑Bus message and waits for a response or nil to receive without sending.
Arguments
- msg (string or nil):
- string - raw bytes to send (use Lua strings like
"\x68..."orpack.pack(...)). - nil - do not send, wait only for a response.
- string - raw bytes to send (use Lua strings like
Optional
- timeout (integer, optional): Maximum time in milliseconds to wait for a response (default: device MBUS timeout setting).
- retry (integer, optional): Number of receive attempts if no data is returned.
0default.1no retry.
Return
- status (integer): Receive result:
> 1- data length (payload bytes received)1- single‑byte response (e.g., ACK0xE5)0- no data received after retries-1bad filter ID.-2device not responding to selection.
- c (integer): M‑Bus C field (Control).
- a (integer): M‑Bus A field (Address).
- ci (integer): M‑Bus CI field (Control Information).
- answer (string): Payload received from the bus (data portion). May be post‑processed by a VIF/DIF filter if enabled on the device.
- raw (string): Complete M‑Bus frame including header/trailer (e.g.,
68 LL LL 68 C A CI DATA CS 16).
Examples
-- Send a custom M-Bus message and wait for the response
api.mbusState(1) -- Turn on M-Bus
local msg = pack.pack("<b4", 0x10, 0x50, 0x30, 0x16)
local status, c, a, ci, answer, raw = api.mbusTransaction(msg, 5000, 2)
api.mbusState(0) -- Turn off M-Bus
-- Receive-only for 500 ms (no transmit)
local status, c, a, ci, answer, raw = api.mbusTransaction(nil, 500)
-- Also works (empty string => no transmit)
local status, c, a, ci, answer, raw = api.mbusTransaction("", 500)
api.mbusTransaction(index, timeout, retry)
api.mbusTransaction(0, 3000, 1)
Sends a readout request to the device whose secondary address is referenced by the given filter index.
Arguments
- index (integer): Index of the secondary address in the table created by
api.mbusFilter()(starting with0).
Optional
- timeout (integer, optional): Maximum time in milliseconds to wait for a response. Default: device M-Bus timeout setting.
- retry (integer, optional): Number of receive attempts if no data is returned (Default:
1- no retry).*
Return
- status (integer): Receive result:
> 1- payload length (bytes received)1- single-byte response (e.g., ACK0xE5)0- no data received after retries< 0- error:-1- bad filter index-2- device not responding during secondary-address selection- other negatives - transport-layer codes
- c (integer): M-Bus C field (Control).
- a (integer): M-Bus A field (Address).
- ci (integer): M-Bus CI field (Control Information).
- answer (string): Payload (data portion) of the response. May be post-processed by a VIF/DIF filter; length can differ from status.
- raw (string): Complete M-Bus frame including header/trailer (e.g.,
68 LL LL 68 C A CI DATA CS 16).
Example
-- Send a readout request using a secondary address (index 0)
api.mbusState(1) -- Turn on M-Bus
local status, c, a, ci, answer, raw = api.mbusTransaction(0, 3000, 1)
api.mbusState(0) -- Turn off M-Bus
print(status, raw)
api.mbusSetup(baudRate, parity, stopBits, dataBits)
api.mbusSetup(9600, 2, 2, 8)
Configures the M-Bus communication interface.
Tip: After configuring the M-Bus parameters with this function, activate the M-Bus by calling
api.mbusState(1).
Arguments
- baudrate (integer): Baudrate for communication (up to
921600baud). - parity (integer): Parity setting:
0for none.1for odd.2for even.
- stopBits (integer): Number of stop bits:
12
- dataBits (integer): Number of data bits:
78
Example
-- Configure M-Bus interface to 9600 baud, 8E2
api.mbusSetup(9600, 2, 2, 8)
--------------------------------
if state==1 then
api.mbusSetup(baudR, parity, stopB, dataBits)
print(mbh,"Baud rate: "..baudR,"Parity: ".. parity,"Stop byte: ".. stopB,"Data byte: ".. dataBits)
api.mbusState(state)
api.mbusState(1)
Controls the M-Bus circuitry.
Info: Use
api.mbusState(1)beforeapi.mbusTransaction()andapi.mbusState(0)after to reduce consumption. The consumption significantly increases if the circuitry is turned on for too long.Danger: Do not use
api.mbusState(1)during LoRaWAN or NB-IoT message transmission.
Controls the M-Bus circuitry.
Arguments
- state (integer): New state of M-Bus circuitry:
1for on.- Approximately 30 seconds is needed to turn on the M-Bus circuitry.
0for off.
Return
- UL (integer): Approximate UL value when turning on (valid only for new topology, otherwise no return value).
Example
-- Turn on M-Bus
api.mbusState(1)
--------------------------------
function onWake ()
if transmissionCounter % mbusRefreshEverNWake == 0 then
print("Refresh MBus frame by readout...")
-- set link parameters - 2400 baud, 8E1
api.mbusSetup(baudrate,parity,stopBits,dataBits)
api.mbusState(1)
api.delayms(initialDelay)
api.mbusScan(filter, timeout, mode)
api.mbusScan()
Scans for M-Bus devices.
Arguments
Optional
- filter (string, optional): Filter by:
identification,manufacturer,version,medium. If no option is selected, scans for everything. - timeout (integer, optional): Timeout in milliseconds, default is
3000. - mode (integer, optional): If
1, scan in range0-Einstead of default0-9(1for true,0for false).
Return
- var (table of variables): Table of variables (identification, manufacturer, version, medium).
- cnt (integer): Number of found devices.
Example
-- Scan for M-Bus devices
api.mbusSetup(2400, 2, 1, 8)
api.mbusState(1) -- Turn on M-Bus
e, cnt = api.mbusScan() -- Scan for everything with default timeout
for i = 1, cnt do
print(string.format("%08X", (e[i].identification)))
end
api.mbusState(0) -- Turn off M-Bus
api.mbusFilter()
Creates and manages internal table of secondary addresses.
| API Name | Brief Description |
|---|---|
| api.mbusFilter("purge") | Purges the table, removing all stored secondary addresses. |
| api.mbusFilter("populate", filter) | Populates the table with a set of secondary addresses. |
| api.mbusFilter("show") | Displays all secondary addresses currently in the table. |
| api.mbusFilter("fetch") | Fetches the table from EEPROM. |
| api.mbusFilter("scan", filter, save, timeout, scan_mode, useBatteryMonitor, initialDelay, batteryMonitorTimeout, targetVoltage, minimalVoltage, minimalRaise) | Performs a scan of devices using secondary addressing. |