Hartmut Wendt
Published © GPL3+

Simple BACnet server with ESP32

How to create a simple BACnet MSTP server with ESP32 feather boards

BeginnerProtip30 minutes56
Simple BACnet server with ESP32

Things used in this project

Hardware components

Adafruit HUZZAH32 – ESP32 Feather Board
Adafruit HUZZAH32 – ESP32 Feather Board
×1
RS485 Feather Wing
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Example Code

C/C++
Simple BACnet MSTP Server example for ESP32
/*
 * BACnetLight - Basic Device Example
 * 
 * Simplest possible BACnet device: one Analog Value.
 * Hardware: Adafruit Feather 32 + Zihatec RS485 Wing
 */


#include <BACnetLight.h>

// --- MSPT definitions ---
#define RS485_DE   -1   // no DE pin needed - we use the auto transmit function of the win 
#define MSTP_MAC   1    // Our MSTP address (0-127)
#define MSTP_BAUD  38400


BACnetMSTP bacnet;

void setup() {
    Serial.begin(115200);
    delay(1000);
    
    // Set device metadata
    bacnet.setDeviceInfo("Vendor", 0, "Model", "1.0.0", "1.0.0");
    
    Serial1.begin(MSTP_BAUD, SERIAL_8N1);
    if (!bacnet.beginMSTP(3000, "MSTP-Feather", Serial1, RS485_DE, MSTP_MAC, MSTP_BAUD)) {
         Serial.println("ERROR: MSTP init failed!");
         while (1) delay(1000);
    }
    
    // Analog Input (read-only sensors) 
    bacnet.addAnalogValue(0, "Temperature", 22.5, BACNET_UNITS_DEGREES_CELSIUS);

    // Binary Output (commandable with priority array)
    bacnet.addBinaryOutput(0, "Relay", false, "Relay output"); 

    Serial.println("BACnet ready!");
}

void loop() {
    bacnet.loop();

    static unsigned long last = 0;
    if (millis() - last >= 5000) {
        last = millis();

        // generate random temperature value for fake temp sensor
        float temp = 20.0 + random(0, 50) / 10.0;
        bacnet.setValue(BACNET_OBJ_ANALOG_VALUE, 0, temp);
        Serial.printf("Temp: %.1f C\n", temp);

        // control led (binary output)
        int relay = bacnet.getValue(BACNET_OBJ_BINARY_OUTPUT, 0); 
        if (relay) {
            Serial.println("Relay: ON");
        } else {
            Serial.println("Relay: OFF");
        }
        

    }
}

Credits

Hartmut Wendt
28 projects • 31 followers
Thanks to Harish Patel.

Comments