Protocol gateways

BACnet to MQTT Gateway

Poll a BACnet/IP analog input with SPX on Docker and publish its present value to MQTT. Download the YAML and verify the temperature reading.

Publish a BACnet/IP temperature reading to MQTT

A building dashboard may use MQTT while its temperature sensor exposes a BACnet/IP point. SPX acts as a BACnet client, polls Present_Value on the sensor's analogInput:2 object, and publishes the temperature as a JSON number to an MQTT broker. The example can use a simulated sensor in SPX or a reachable BACnet/IP device with a matching point.

You need SPX Server with the bacnet, bacnet_client and MQTT adapters for the complete demo, a reachable MQTT broker and a subscriber. On Linux, run SPX Server with Docker Engine and Compose v2. The gateway uses a configured device address for unicast polling. SPX installation on Linux

Set up the gateway

  1. Import the BACnet/IP temperature source model and start an instance. The demo uses UDP 47828, BACnet Device Instance 2101 and analogInput:2. If the port is busy, choose a free port and use it in both instances.
  2. Import the BACnet to MQTT gateway model and create an instance. Keep bacnet_device_host=127.0.0.1 if the source and gateway share one SPX container. For an external device, configure its reachable address, UDP port, Device Instance and analog input instance. Set mqtt_broker_host to the broker address as seen from SPX Server.
  3. Subscribe to the MQTT topic, then start the gateway. Use the broker address and published port visible to your terminal.
mosquitto_sub -h localhost -p 1883 -q 1 -v -t spx/examples/bacnet-mqtt-gateway/telemetry/room_temperature_c

Build the gateway with an AI agent

Paste this prompt into an agent connected to your SPX workspace.

Prompt for your AI agent

Build a BACnet/IP to MQTT gateway in SPX. 1. Write bacnet_temperature_source.yaml with name: bacnet_temperature_source. Expose k__room_temperature_c as Present_Value on analogInput:2. Use BACnet Device Instance 2101 and UDP 47828, or choose free settings and apply them to both models. Start at 21.5 °C. 2. Write bacnet_to_mqtt.yaml with name: bacnet_to_mqtt. Use bacnet_client to poll that point every 0.5 s into room_temperature_c. Publish it as a JSON number at QoS 1 to spx/examples/bacnet-mqtt-gateway/telemetry/room_temperature_c. 3. Start the source, subscribe with mosquitto_sub -v, and start the gateway. Change k__room_temperature_c from 21.5 to 24.25. Compare the SPX gateway attribute and both MQTT payloads. Use an isolated SPX test workspace. Save the new YAML under custom_models/ and validate it before starting an instance. Use existing reachable services and free ports. Preserve existing models, instances and network settings. Return the YAML paths and the actual client output. If a prerequisite is missing, report which check you could not run.

Tested with GPT-6 Luna · Medium · Est. total 0.0028 USD

In a local Docker lab, Luna generated the YAML, ran the gateway in SPX, and checked the result with protocol clients. The simulated BACnet sensor changed from 21.5 °C to 24.25 °C. Both readings appeared in SPX and on MQTT.

The estimate uses token usage for the successful build and test and Standard API rates.

Connect and observe

In Instances → Attributes, change the source's Internal k__room_temperature_c from 21.5 °C to 24.25 °C. Check that room_temperature_c on the gateway changes and the MQTT subscriber receives the JSON number 24.25 on the temperature topic. An initial -1.0 can appear before the first successful BACnet read.

In our Docker test, SPX polled the simulated sensor over BACnet/IP. A separate Paho MQTT subscriber connected to Mosquitto received both values:

BACnet analogInput:2 Present_Value 21.5 °C | MQTT JSON 21.5
BACnet analogInput:2 Present_Value 24.25 °C | MQTT JSON 24.25

Stop the demo instances when you finish.

Protocol communication

Source
BACnet/IP unicast, UDP 47828, Device Instance 2101
Property
ReadProperty on analogInput:2, Present_Value, in °C
Polling
Every 0.5 s in the example
MQTT topic
spx/examples/bacnet-mqtt-gateway/telemetry/room_temperature_c
Payload
JSON number on change, QoS 1, retain disabled

When to use MQTT to BACnet

Use MQTT as the source when measurements already arrive on a broker and a BMS needs to read them through BACnet/IP. That route uses an MQTT subscription and a BACnet server object exposed by SPX. Writing commands to an existing BACnet controller is a separate workflow, with an outbound client binding and a ReadProperty check of the target.