M-Bus
This category contains functions specific to M-Bus devices.
- Overview
- Arguments
- Return
- Examples
Sends and receives M-Bus frames.
Ensure M-Bus is enabled by first calling api.mbusState(1).
api.mbusTransaction(msg, timeout, retry)
- msg (string) - Message sent to M-Bus, leave empty to wait only for a response
- timeout (integer) - Maximum time in milliseconds to wait for a response from the M-Bus device
- retry (integer) - Optional number of retransmissions, default number is 1
Sends a custom message.
api.mbusTransaction(index, timeout, retry)
- index (integer) - Index of the secondary address in the table created by api.mbusFilter() (starting with 0)
- timeout (integer) - Maximum time in milliseconds to wait for a response from the M-Bus device
- retry (integer) - Optional number of retransmissions, default number is 1
Sends a readout request with the specified secondary address.
- status (integer) - Number of bytes received, zero on failure
- c (integer) - M-Bus c frame field
- a (integer) - M-Bus a frame field
- ci (integer) - M-Bus ci frame field
- answer (string) - M-Bus frame payload received from the bus
- raw (string) - Complete M-Bus frame with header
Example how to send a custom M-Bus message and wait for the response:
api.mbusState(1) --turn on M-Bus
-- Send M-Bus frame [0x10, 0x50, 0x30, 0x16] and wait 5s for the response, retry twice
msg = pack.pack('<b4', 0x10, 0x50, 0x30, 0x16)
status,c,a,ci,ans = api.mbusTransaction(msg, 5000, 2)
api.mbusState(0) --turn off M-Bus
Example how to use while function to get complete mbus frame (if it's available):
--"i"=index in the table created by api.mbusFilter() (starting with 0)
status,_,_,_,_,raw = api.mbusTransaction(i,3000,1)
if status > 1 then
while status > 1 do
status,_,_,_,_,rawNext = api.mbusTransaction("",200,1) -- Wait for the next frame part
if status > 1 then
raw = raw .. rawNext
else
break
end
end
end
api.dumpArray(raw)
- Overview
- Arguments
- Example
Configures the M-Bus communication interface.
After configuring the M-Bus parameters with this function, activate the M-Bus by calling api.mbusState(1).
- baudrate (integer) - Baudrate to use for communication (up to 921600 baud)
- parity (integer) - Parity, 0 for none, 1 for odd and 2 for even parity
- stopBits (integer) - Number of stop bits, 1 or 2 allowed
- dataBits (integer) - Number of data bits, 7 or 8 allowed
--setup M-Bus interface to 9600 Baud, 8E2
api.mbusSetup(9600, 2, 2, 8)
- Overview
- Arguments
- Return
- Example
Controls the M-Bus circuitry.
Use api.mbusState(1) before api.mbusTransaction() and api.mbusState(0) after to reduce consumption. The consumption significantly raises if the circuitry is turned on for too long.
Do not use the api.mbusState(1) during LoRaWAN or NB-IoT message transmition.
- state (integer) - New state of M-Bus circuitry: 1 for on, 0 for off
Approximately 30s is needed to turn on the M-Bus circuitry.
- UL (integer) - Returns approximate UL value when turing on
Valid only for new topology, otherwise there's no return value
api.mbusState(1) --turn on M-Bus
- Overview
- Arguments
- Return
- Example
Scans for M-Bus devices.
api.mbusScan(filter, timeout, mode)
- filter (string, optional) - Filter by (no option selected = 0 = scan for everything):
- identification
- manufacturer
- version
- medium
- timeout (integer, optional) - By default set to 3000 ms
- mode (integer, optional) - If true, scan in range 0-E instead of default 0-9 (true/false)
- var (hex)(table of variables) - Table of variable (identification, manufacturer, version, medium), look at the example to see how to index in table
- cnt (integer) - Number of found devices
api.mbusSetup(2400,2,1,8)
api.mbusState(1) --turn on M-Bus
e,cnt = api.mbusScan() -- scan for everything(ID, man, ...) & use default timeout 3000 ms
for i = 1, cnt do
print(string.format("%08X", (e[i].identification)))
end
api.mbusState(0) --turn off M-Bus
- Overview
- Operations
- Arguments
- Return
- Examples
Creates and manages internal table of secondary addresses.
- "purge" - Purges the table, removing all stored secondary addresses
- "populate" - Populates the table with a set of secondary addresses provided as input
- "show" - Displays all secondary addresses currently in the table
- "fetch" - Fetches the table from EEPROM, retrieving the previously saved secondary addresses
- "scan"* - Performs a scan of devices using secondary addressing, based on the filter provided
This function is used for secondary addressing using 0xFD address internally. For more details check the example.
api.mbusFilter("purge")
- operation (string)
api.mbusFilter("populate", filter)
- operation (string)
- filter (string) - Filter, array of ids
api.mbusFilter("show")
- operation (string)
api.mbusFilter("fetch")
- operation (string)
api.mbusFilter("scan", filter, save, timeout, scan_mode, useBatteryMonitor, initialDelay, batteryMonitorTimeout, targetVoltage, minimalVoltage, minimalRaise)
- operation (string)
- filter (string) - Filter, array of IDs
- save (integer, optional) - 1 to save the found addresses in the filter
- timeout (integer, optional) - Timeout in ms
- scan_mode (integer, optional) - Mode for the scan operation
- useBatteryMonitor (integer, optional) - 1 to enable battery monitoring
- initialDelay (integer, optional) - Initial delay before scanning in ms
- batteryMonitorTimeout (integer, optional) - Timeout for battery monitoring in ms
- targetVoltage (integer, optional) - Target voltage for the battery monitor
- minimalVoltage (integer, optional) - Minimal voltage for the battery monitor
- minimalRaise (integer, optional) - Minimal raise for the battery monitor