How to Use Bacnet MSTP With Raspberry Pi
by Zihatec in Circuits > Raspberry Pi
36 Views, 1 Favorites, 0 Comments
How to Use Bacnet MSTP With Raspberry Pi
BACnet is an internationally standardized communication protocol for building automation that enables devices from different manufacturers—such as heating, ventilation, air conditioning, lighting, and security systems—to communicate with one another. While current BACnet systems mostly use Ethernet as the transmission medium (BACnet/IP), RS485 (BACnet MSTP) was typically used as Layer 0 in the early days.
BACnet stacks are available for the Raspberry Pi in C++ and Python. However, most tutorials focus almost exclusively on BACnet/IP.
In this tutorial, I’d like to show you how to use BACnet MSTP on the Raspberry Pi.
Supplies
It is recommended to use a newer Raspberry Pi 4 or Pi 5 for BACnet implementation. There are two reasons for this. First, the BACnet stack is relatively large and resource-intensive; second, these models have multiple hardware UARTs that are available exclusively for this purpose and are not used simultaneously for other functions such as Bluetooth.
Additionally, we need an RS485 interface for communication via BACnet MSTP. For this, I use a Zihatec RS485 HAT with galvanic isolation.
Wiring
As for BACnet MS/TP, the most common connection method is the daisy-chain bus configuration, where all the devices in the network are connected in a linear chain using a single RS485 cable.
To do this, connect the “+” and “-” terminals of each device to one another. The HAT is equipped with a 5-pin connector, where two pins are labeled ‘A’ and “B.” Pin A must be connected to the “+” terminals, and pin B to the “-” terminals of the other devices.
For longer distances, it is recommended to use a twisted-pair cable. Distances of up to one mile are possible via RS485.
Settings on the HAT
The DIP switches on the HAT must be set to RS485 mode (half-duplex). To do this, set the switches as follows:
- S1: OFF - ON - ON - OFF
- S2: OFF - OFF - ON - ON
- S3: OFF - OFF - ON - ON
Now we need to select the UART we want to use on K3 using two jumpers. The jumpers must be set to either position U3 (UART3), U4 (UART4), or U5 (UART5).
Note: On a Raspberry Pi 5, the UARTs are numbered differently. Here, U3 is UART2, U4 is UART3, and so on.
Preparing the Operating System
First, install a current version of the Raspbian OS (in our case, Debian Trixie, 64-bit) onto an SD card. Then, as usual, update the OS to the latest version:
Now we need to enable the UART we want to use. To do this, open the Config.txt file:
Now let's enable the respective UART at the end of the file. For UART3, for example, that would be:
After saving the Config.txt file and restarting the Raspberry Pi, we can now check whether our UART is enabled:
Among others, an interface should now be available at the path /dev/ttyAMAx for the corresponding UART.
Install and Compile the C/C++ BACnet Stack
Now we can begin installing the BACnet stack.
These compiled files should now be located in the bacnet-stack/bin directory—specifically, the three sample programs for BACnet MSTP:
Testing the BACnet Stack
The stack includes various sample programs for MSTP, including a tool for capturing data from a BACnet MSTP bus.
All we need to do is connect our RS485 interface to an existing system to record the protocols. (If no BACnet MSTP system is available, you can simply install YABE on a PC and record this communication.)
To do this, enter the following command in the /bacnet-stack/bin directory:
The baud rate and path to the interface may need to be adjusted, of course.
mstpcap then records all received packets in a file until the program is terminated with Ctrl+C.
YABE on the Raspberry Pi
Another way to use BACnet IP and MSTP on the Raspberry Pi is to use YABE (Yet Another BACnet Explorer). Since YABE is actually a Windows program written using Microsoft's.NET Framework, we first need to install mono-complete so that YABE can run on Linux.
Now we can download the latest version of YABE as a ZIP file (not the EXE installer!!!) and unzip it.
Yabe can now be started using mono. To access the interfaces, the startup command should be run via sudo
The configuration window for the channel opens automatically. Under “Port, ” select the path to the serial interface, e.g., /dev/ttyAMA3.
After pressing the Start button, YABE automatically sends a Whois query, and the devices available on the bus are displayed.