BACnet client

BACnet client library #

Load the library:

require('bacnet')

Library functions #

read(devid, objtype, objid, prop) #

Reads BACnet property. Returns read result as a string on success, or, in case of error, nil plus error message.

Parameters:

  • devid - target device id
  • objtype - object type
  • objid - object instance id
  • prop - property name

readvalue(devid, objtype, objid) #

Reads current value (present value property) and decodes boolean/numeric values. Returns decoded read result on success, or, in case of error, nil plus error message.

Parameters:

  • devid - target device id
  • objtype - object type
  • objid - object instance id

write(devid, otype, objid, value, prio) #

Write new BACnet value. Returns true on success, or, in case of error, nil plus error message.

Parameters:

  • devid - target device id
  • objtype - object type
  • objid - object instance id
  • value - new value to write, can be nil, boolean, number or string
  • priority - optional value priority (1..16), default is lowest (16)

scandevices() #

Scans the network for all available BACnet devices. Returns Lua table where key is device ip and value is device BACnet id.


scandevice(id) #

Scans the given device id. Returns two variables:

  1. Lua table containing device properties.
  2. Lua table as an array where each element is a table containing single object properties.

Parameters:

  • devid - target device id

Library varibles #

port #

Sets the BACnet communication port number. Default is 0xBAC0.


deviceip #

Sets the BACnet device IP address enabling direct communication for reading/writing. This allows running multiple client scripts in parallel and using BACnet server together with client scripts. Default is nil (unset).

Supported properties #

Object types #

  • analog input
  • analog output
  • analog value
  • binary input
  • binary output
  • binary value
  • command
  • device
  • multi-state input
  • multi-state output
  • multi-state value
  • accumulator
  • pulse converter
  • integer value
  • large analog value
  • positive integer value

Object properties #

  • object name
  • present value
  • priority array
  • relinquish default

Examples #

Read device binary object #

require('bacnet')
value, err = bacnet.readvalue(127001, 'binary value', 2305)
log(value, err)

Read device analog object #

require('bacnet')
value, err = bacnet.readvalue(127001, 'analog value', 2306)
log(value, err)

Set device binary object value (default priority) #

require('bacnet')
res, err = bacnet.write(127001, 'binary value', 2305, true)
log(res, err)

Set device analog object value (default priority) #

require('bacnet')
res, err = bacnet.write(127001, 'analog value', 2306, 22.5)
log(res, err)

Set device binary object value at priority 12 #

require('bacnet')
res, err = bacnet.write(127001, 'binary value', 2305, true, 12)
log(res, err)

Set device analog object value at priority 10 #

require('bacnet')
res, err = bacnet.write(127001, 'analog value', 2306, 22.5, 10)
log(res, err)

Clear device binary object value at priority 12 #

require('bacnet')
res, err = bacnet.write(127001, 'binary value', 2305, nil, 12)
log(res, err)

Scan network #

require('bacnet')
devices = bacnet.scandevices()
log(devices)

Scan device #

require('bacnet')
device, objects = bacnet.scandevice(127001)
log(device, objects)

Read using direct connection mode #

require('bacnet')

-- read from 192.168.0.21, device id 21
bacnet.deviceip = '192.168.0.21'
val1 = bacnet.readvalue(21, 'binary value', 2049)
val2 = bacnet.readvalue(21, 'binary value', 2050)
val3 = bacnet.readvalue(21, 'binary value', 2051)
log(val1, val2, val3)