Devices & Components
Arduino® UNO R4 Minima
RS422 / RS485 Shield for Arduino UNO
Software & Tools
Arduino IDE
Project description
Code
BasicDevice
cpp
Simple BACnet MSTP server for ARduino UNO R4
1/* 2 * BACnetLight - Basic Device Example for Arduino UNO R4 3 * 4 * Simplest BACnet MS/TP device: one Analog Value input and one binary output 5 * Hardware: Arduino UNO R4 + Zihatec RS485 Shield 6 */ 7 8 9#include <BACnetLight.h> 10 11// --- definitions --- 12#define RS485_DE -1 // no DE pin needed - we use the auto transmit function of the shield 13#define MSTP_MAC 1 // Our MSTP address (0-127) 14#define MSTP_BAUD 38400 15 16 17BACnetMSTP bacnet; 18 19void setup() { 20 Serial.begin(115200); 21 delay(1000); 22 23 // Set device metadata 24 bacnet.setDeviceInfo("Vendor", 0, "Model", "1.0.0", "1.0.0"); 25 26 Serial1.begin(MSTP_BAUD, SERIAL_8N1); 27 if (!bacnet.beginMSTP(3000, "MSTP-Arduino", Serial1, RS485_DE, MSTP_MAC, MSTP_BAUD)) { 28 Serial.println("ERROR: MSTP init failed!"); 29 while (1) delay(1000); 30 } 31 32 // Analog Input (read-only sensors) 33 bacnet.addAnalogValue(0, "Temperature", 22.5, BACNET_UNITS_DEGREES_CELSIUS); 34 35 // Binary Output (commandable with priority array) 36 bacnet.addBinaryOutput(0, "Relay", false, "Relay output"); 37 38 Serial.println("BACnet ready!"); 39} 40 41void loop() { 42 bacnet.loop(); 43 44 static unsigned long last = 0; 45 if (millis() - last >= 5000) { 46 last = millis(); 47 48 // generate random temperature value for fake temp sensor 49 float temp = 20.0 + random(0, 50) / 10.0; 50 bacnet.setValue(BACNET_OBJ_ANALOG_VALUE, 0, temp); 51 Serial.print("Temp: "); Serial.print(temp); Serial.println("°C"); 52 53 // control led (binary output) 54 int relay = bacnet.getValue(BACNET_OBJ_BINARY_OUTPUT, 0); 55 if (relay) { 56 Serial.println("Relay: ON"); 57 } else { 58 Serial.println("Relay: OFF"); 59 } 60 61 62 } 63}
Comments
Only logged in users can leave comments