Skip to content

BACnet/IP Device Service Example

This example shows how to use the BACnet/IP Device Service from inside a container using JSON configuration files. It is located in the device-bacnet-c directory.

When the example is run, a container is instantiated and populated with components. The container uses component factories to manage components, which are specified in the config/main.json file. Each entry for a component in this file specifies the following:

  • The name of the component, for example: bacnet_device_service
  • The name of the registered factory used to create the component, for example: XRT::BACnetIPDeviceService

The components used in this example are as follows:

Each component can be configured using the associated JSON file; for example, the BACnet/IP Device Service component is configured using the bacnet_device_service.json file. For further information on configuration of the BACnet/IP Device Service component, see BACnet Device Service Component.

In this example, the BACnet/IP Device Service component is configured to read an event from the BACnet Simulator every two seconds and publish it to the Bus. The reading is logged by the Log Exporter component.

The BACnet Simulator simulates a BACnet device that is controlled by a Lua script. The Lua script used in this example is located in the examples/device-bacnet-c/bacnet-simulator directory.

You must manually specify the IP address when running the BACnet Simulator on the same machine as XRT, as it is run in a Docker container. An example of using XRT without specifying the IP address of the BACnet Simulator Docker container, which uses the XRT Docker image and runs the BACnet Simulator on the same machine, is provided in BACnet Simulator.

To connect to the BACnet Simulator Docker container with XRT running on the same machine, complete the following steps:

  1. Start the BACnet Simulator using the following command:

    docker run -it --name=bacnet-server -e RUN_MODE=IP \
    -v /opt/iotech/xrt/examples/device-bacnet-c/bacnet-simulator/:/docker-lua-script/ \
    iotechsys/bacnet-server:1.8.3 --script /docker-lua-script/example.lua --instance
    1234
    

  2. Save the IP address of the BACnet Simulator Docker container to the BACNET_BBMD_ADDRESS environment variable, using the following command:

    export BACNET_BBMD_ADDRESS="$(docker inspect bacnet-server --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}')"
    

  3. Navigate to the BACnet examples directory using the following command:

    cd examples/device-bacnet-c
    

  4. Open the config/bacnet_device_service.json file in an editor

  5. Replace the Driver section in the config/bacnet_device_service.json file with the following:

    "Driver":{
     "BBMD_ADDRESS":"${BACNET_BBMD_ADDRESS}",
     "BBMD_PORT": 47808,
     "APDU_Timeout": 5000,
     "APDU_Retries": 2
    }
    

  6. Save the updated config/bacnet_device_service.json file

  7. Start XRT from the BACnet example configuration directory using the following command:

    xrt config/ 5
    
    The output is similar to the following:
    Trying to register with BBMD at 172.17.0.2:47808 for 30 seconds
    SUCCEEDED to Register with BBMD at 172.17.0.2:47808
    [iot-0-1:1622804558434541:console:Debug] bacnet_get_handler: bacnetsimulator
    [iot-0-1:1622804558434583:console:Debug] [BACnet-IP] protocol:
    [iot-0-1:1622804558434621:console:Debug] Port = 47808
    [iot-0-1:1622804558434634:console:Debug] DeviceInstance = 1234
    [iot-0-1:1622804558434649:console:Debug] [BACnetSupportedServices] protocol:
    [iot-0-1:1622804558434665:console:Debug] DS-WPM-B = true
    [iot-0-1:1622804558434680:console:Debug] DS-RPM-B = true
    [iot-0-1:1622804558434706:console:Debug] read_access_data_populate: resource: AnalogInput0, object type: 0, instance: 0, property: 85
    [iot-0-1:1622804558434778:console:Debug] read_access_data_populate: resource: BinaryInput0, object type: 3, instance: 0, property: 85
    [iot-0-1:1622804558434795:console:Debug] bacnet_get_handler: Read property multiple for 2 readings
    [xrt:1622804558436177:console:Debug] bacnet_read_multi_ack_handler: ObjectIdentifier - type: 0, instance: 0
    [xrt:1622804558436207:console:Debug] bacnet_read_multi_ack_handler: Property: 85
    [xrt:1622804558436218:console:Debug] devsdk_commandresult_populate: REAL -> Float32
    [xrt:1622804558436230:console:Debug] bacnet_read_multi_ack_handler: ObjectIdentifier - type: 3, instance: 0
    [xrt:1622804558436239:console:Debug] bacnet_read_multi_ack_handler: Property: 85
    [xrt:1622804558436248:console:Debug] devsdk_commandresult_populate: ENUMERATED -> Uint32
    [xrt:1622804558436262:console:Debug] bacnet_read_multi_ack_handler: Number of returned readings of 2, matches that of expected 2
    [iot-0-1:1622804558436367:console:Debug] publish: {"device":"bacnetsimulator","resource":"InputsValues","readings":{"AnalogInput0": {"value":8.00000000e+00,"type":"float32"},"BinaryInput0": {"value":255,"type":"uint32"}}}
    

Back to top