BACnetStack.h library of ESP32 is not working-ArduinoIDE

Hi all, I need a support to install a proper library of BACnetStack.h for ESP32.I have downloaded and tried 'BACnetStack.h' from many sources but while installing in ArduinoIDE, it is prompted "Specified folder/zip file does not contain a valid library". I had tried libraries from many sources but result is same. Please help me to resolve the problem by giving a proper source.
GitHub - bacnet-stack/bacnet-stack: BACnet Protocol Stack library provides a BACnet application layer, network layer and media access (MAC) layer communications services. -This is one of the links from where I have downloaded.
Thanks in advance..
Sunil

HI @sunilpallikkara. Which version of Arduino IDE are you using (e.g., "2.0.1")? The version is shown on the window title bar and also in the dialog that opens when you select Help > About (or Arduino IDE > About Arduino IDE for macOS users) from the Arduino IDE menus.

I ask for this information because I want to give you the appropriate instructions for the IDE version you are using.

Hi Ptillish,
I did try in 1.8.19 as well as 2.3.3, both are giving same result.
Regards,
Sunil

OK, then I'll proceed under the assumption you are using Arduino IDE 2.3.3.

I will provide instructions you can follow to install the code from https://github.com/bacnet-stack/bacnet-stack as an Arduino library. However, you should note that this project was not designed specifically to be used as an Arduino library so I have no idea whether you will be able to actually use it for your project even after it is installed.

  1. Select File > Preferences... (or Arduino IDE > Settings... for macOS users) from the Arduino IDE menus.
    The "Preferences" dialog will open.
  2. Take note of the path shown in the "Sketchbook location" field of the dialog.
  3. Click the "CANCEL" button.
    The "Preferences" dialog will close.
  4. Click the following link to open the page of the latest release in the project's GitHub repository:
    https://github.com/bacnet-stack/bacnet-stack/releases/tag/bacnet-stack-1.4.0
  5. Click the "Source code (zip)" link you will see under the "Assets" section of that page.
    A download of the ZIP file of the library will start.
  6. Wait for the download to finish.
  7. Extract the downloaded file.
  8. Copy the src/bacnet folder from the extracted folder to the libraries subfolder of the path you saw in the "Sketchbook location" preference.
    The folder structure of the installation must look like this:
    <Sketchbook location>/
    ├── libraries/
    │   ├── bacnet/
    │   │   ├── abort.c
    │   │   ...
    │   ...
    ...
    
    (where <Sketchbook location> is the path from the Sketchbook location" preference)
  9. Select File > Quit (or Arduino IDE > Quit Arduino IDE for macOS users) from the Arduino IDE menus.
    All Arduino IDE windows will close.
  10. Start Arduino IDE.

Something important to note is that the library is written in the C programming language, while the .ino files of Arduino sketches are compiled as C++. C++ is a superset of C, so it is possible to use a library written in C in an .ino file, but you must add some special code to do so. For example, if the header file bacapp.h contains declarations for objects you wish to reference in the code of your .ino file, with a C++ library we would add this line to the sketch:

#include <bacapp.h>

But with a C library you must do this instead:

extern "C" {
#include <bacapp.h>
}

Ok.Let me try and keep posted the update.
Thank you

1 Like

Hi Ptillisch,
While compiling the program I am getting an error.
In file included from C:\Users\Sunil.pb.SUNILPB-LT\Documents\Arduino\BACnet_Test\BACnet_Test.ino:3:
C:\Users\Sunil.pb.SUNILPB-LT\Documents\Arduino\libraries\bacnet/bacapp.h:16:10: fatal error: bacnet/bacdef.h: No such file or directory
16 | #include "bacnet/bacdef.h"
| ^~~~~~~~~~~~~~~~~
compilation terminated.
exit status 1
Error compiling for board ESP32 Dev Module.

The basic program is here..


//#include <BACnetStack.h>
#include <bacapp.h>

// Define the BACnet device instance
BACnetDevice device(1234, 1, 0);

// Define the Honeywell controller's BACnet address
BACnetAddress controllerAddr = { 0x1234, 1, 0 };

void setup() {
  Serial.begin(115200);
  device.begin();
}

void loop() {
  // Read the present value of an analog input object
  uint16_t aiValue = device.readAnalogInput(controllerAddr, 0x00000001);
  Serial.print("AI Value: ");
  Serial.println(aiValue);

  // Read the present value of a binary output object
  uint8_t boValue = device.readBinaryOutput(controllerAddr, 0x00000002);
  Serial.print("BO Value: ");
  Serial.println(boValue);

  delay(1000);
}

I can provide instructions for you to get past this error, but you'll only run into the next one after that.

I can see from the sketch you shared that you are trying to just throw random code at the library in hopes it will magically work. The only way you would have any chance of making this library work for you is if you spent a huge amount of time to fully understand this very complex library. I'm certain you aren't willing to put in that effort, and there is a good chance that it wouldn't even be a productive use of your time even if you were willing to do that.

So now it is time to take a big step back. Provide a detailed description of what you are actually trying to accomplish. Then the forum helpers can provide advice on how best to do that instead of wasting more time on this wild goose chase of trying to install a random library you found.

Hi Ptiliisch,
I understand its very complex from your reply. The description of my expected project is to read some critical data from Honeywell's and Johnson Control's BACnet controllers and send the data to 'sinric.pro' or MQTT client application for remote monitoring-I have already accomplished to read and write data with Johnson Controls MODBUS controllers-. The same way I thought about of implementing with BACnet, so when I was searching, I found the library 'BACnetStack' for ESP32 but unfortunately the library can't be added into Arduino IDE, so I requested for help in the forum. If it is able to accomplish, of course it would be very helpful to me. I am ready to spend time to accomplish it, if you and other experts support me.
Thanks in advance,
Sunil

Please provide a link to the web page where you found this code (or the code upon which it is based):

That information might lead us to a more friendly BACnet library.

Hi Ptiliisch,
The code is taken from Chat GPT, I can attach the codes which I have taken.

Here's an example of a basic BACnet Master code using the BACnet Stack by Ibrahim Abdelkader on an ESP32:

#include <BACnetStack.h>

// Define the BACnet device instance
BACnetDevice device(1234, 1, 0);

void setup() {
  Serial.begin(115200);
  device.begin();
}

void loop() {
  // Read the present value of a BACnet object (e.g., analog input)
  BACnetObject* obj = device.readObject(0x00000001, 0x00000001, 0x00000002);
  if (obj) {
    Serial.print("Present Value: ");
    Serial.println(obj->getPresentValue());
  }

  // Write a new value to a BACnet object (e.g., analog output)
  BACnetObject* obj2 = device.writeObject(0x00000002, 0x00000001, 0x00000003, 42.0);
  if (obj2) {
    Serial.println("Write successful");
  }

  delay(1000);
}

This code:

- Initializes the BACnet device with instance number 1234, device ID 1, and a MAC address of 0.
- In the loop function:
    - Reads the present value of an analog input object (instance 1, type 2) and prints it to the serial monitor.
    - Writes a new value (42.0) to an analog output object (instance 2, type 3) and prints a success message if the write is successful.

Note that you'll need to:

- Install the BACnet Stack library using the Arduino IDE or your preferred development environment.
- Configure the library according to your project requirements (e.g., setting the BACnet device instance, device ID, and MAC address).
- Replace the object instance numbers, types, and values with those relevant to your specific BACnet devices and application.

BACnet MSTP Slave Example

#include <BACnetStack.h>

// Define the BACnet device instance
BACnetDevice device(5678, 2, 0);

void setup() {
  Serial.begin(115200);
  device.begin();
  device.setMSTPMode(BACNET_MSTP_SLAVE);
}

void loop() {
  // Respond to Who-Is messages
  device.respondToWhoIs();

  // Handle incoming ReadPresentValue requests
  BACnetAddress srcAddr;
  uint16_t objInst, objType;
  device.handleReadPresentValue(srcAddr, objInst, objType);
}

To use these examples, you'll need to:

1. Install the BACnet Stack library.
2. Configure the library according to your project requirements.
3. Replace the device instance numbers, IDs, and MAC addresses with your own.

BACnet MSTP Master Example

#include <BACnetStack.h>

// Define the BACnet device instance
BACnetDevice device(1234, 1, 0);

void setup() {
  Serial.begin(115200);
  device.begin();
  device.setMSTPMode(BACNET_MSTP_MASTER);
}

void loop() {
  // Send Who-Is message
  device.sendWhoIs(0, 0xFF);
  delay(1000);

  // Poll device for present value
  BACnetAddress addr = { 0x1234, 1, 0 };
  device.sendReadPresentValue(addr, 0x00000001, 0x00000002);
  delay(1000);
}


Here's a basic BACnet example for ESP32 using the BACnet Stack library:

BACnet Device Example

#include <BACnetStack.h>

// Define the BACnet device instance
BACnetDevice device(1234, 1, 0);

void setup() {
  Serial.begin(115200);
  device.begin();
}

void loop() {
  // Read analog input value
  uint16_t aiValue = device.readAnalogInput(0x00000001);
  Serial.print("AI Value: ");
  Serial.println(aiValue);

  // Write analog output value
  device.writeAnalogOutput(0x00000002, 42.0);
  Serial.println("AO Value Written");

  delay(1000);
}

Another code:

#include <BACnetStack.h>

// Define the BACnet device instance
BACnetDevice device(1234, 1, 0);

// Define the BACnet object instances
uint16_t aiInstance = 0x00000001;
uint16_t boInstance = 0x00000002;

void setup() {
Serial.begin(115200);
device.begin();
device.setMSTPMode(BACNET_MSTP_SLAVE);
}

void loop() {
// Respond to Who-Is messages
device.respondToWhoIs();

// Handle incoming ReadProperty requests
BACnetAddress srcAddr;
uint16_t objInst, objType, propId;
device.handleReadProperty(srcAddr, objInst, objType, propId);

// Handle incoming WriteProperty requests
device.handleWriteProperty(srcAddr, objInst, objType, propId);

delay(100);
}
Explanation
This code sets up an ESP32 board as a BACnet slave device, responding to Who-Is messages and handling incoming ReadProperty and WriteProperty requests.

*BACnet Device Configuration*
- `BACnetDevice device(1234, 1, 0)`: Defines the BACnet device instance with a device ID of 1234, instance number 1, and MAC address 0.

*BACnet Object Instances*
- `uint16_t aiInstance = 0x00000001`: Defines an analog input object instance with an instance number of 1.
- `uint16_t boInstance = 0x00000002`: Defines a binary output object instance with an instance number of 2.

*BACnet Slave Mode*
- `device.setMSTPMode(BACNET_MSTP_SLAVE)`: Sets the device to operate in BACnet MSTP slave mode.

*Handling BACnet Requests*
- `device.respondToWhoIs()`: Responds to incoming Who-Is messages.
- `device.handleReadProperty()`: Handles incoming ReadProperty requests.
- `device.handleWriteProperty()`: Handles incoming WriteProperty requests.

This code provides a basic example of a BACnet slave device on ESP32. You can modify and extend it to suit your specific requirements.