Make an iBeacon out of an ESP32 Board

Hi there guys,

I am trying to follow this article in order to create a BLE iBeacon out of my ESP32 board, but it keeps throwing a lot of errors about undefined types, even though I installed the correct library and the library has those types.

Board: GroundStudio Carbon S2 - ARDUSHOP

Code
#include "sys/time.h"
#include "BLEDevice.h"
#include "BLEUtils.h"
#include "BLEServer.h"
#include "BLEBeacon.h"
#include "esp_sleep.h"

#define GPIO_DEEP_SLEEP_DURATION     10  // sleep x seconds and then wake up
RTC_DATA_ATTR static time_t last;        // remember last boot in RTC Memory
RTC_DATA_ATTR static uint32_t bootcount; // remember number of boots in RTC Memory


// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

BLEAdvertising *pAdvertising;   // BLE Advertisement type
struct timeval now;
#define BEACON_UUID "87b99b2c-90fd-11e9-bc42-526af7764f64" // UUID 1 128-Bit (may use linux tool uuidgen or random numbers via https://www.uuidgenerator.net/)


void setBeacon() {
  BLEBeacon oBeacon = BLEBeacon();

  oBeacon.setManufacturerId(0x4C00); // fake Apple 0x004C LSB (ENDIAN_CHANGE_U16!)

  oBeacon.setProximityUUID(BLEUUID(BEACON_UUID));

  oBeacon.setMajor((bootcount & 0xFFFF0000) >> 16);

  oBeacon.setMinor(bootcount & 0xFFFF);

  BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();

  BLEAdvertisementData oScanResponseData = BLEAdvertisementData();

  oAdvertisementData.setFlags(0x04); // BR_EDR_NOT_SUPPORTED 0x04

  std::string strServiceData = "";

  strServiceData += (char)26;     // Len

  strServiceData += (char)0xFF;   // Type

  strServiceData += oBeacon.getData();

  oAdvertisementData.addData(strServiceData);

  pAdvertising->setAdvertisementData(oAdvertisementData);

  pAdvertising->setScanResponseData(oScanResponseData);
}

void setup() {
  Serial.begin(115200);
  
  gettimeofday(&now, NULL);

  Serial.printf("start ESP32 %d\n", bootcount++);

  Serial.printf("deep sleep (%lds since last reset, %lds since last boot)\n", now.tv_sec, now.tv_sec - last);

  last = now.tv_sec;

  // Create the BLE Device
  BLEDevice::init("ESP32 as iBeacon");

  // Create the BLE Server
  BLEServer *pServer = BLEDevice::createServer(); // <-- no longer required to instantiate BLEServer, less flash and ram usage

  pAdvertising = BLEDevice::getAdvertising();

  BLEDevice::startAdvertising();

  setBeacon();

  // Start advertising
  pAdvertising->start();

  Serial.println("Advertizing started...");

  delay(100);

  pAdvertising->stop();

  Serial.printf("enter deep sleep\n");

  esp_deep_sleep(1000000LL * GPIO_DEEP_SLEEP_DURATION);

  Serial.printf("in deep sleep\n");
}


void loop() {

}

Can anyone help me with this?

Thank you in advance!

I compiled for the "DOIT ESP32 DEVKIT V1" and only got one error:

In function 'void setup()':
 error: unused variable 'pServer'
   BLEServer *pServer = BLEDevice::createServer();  // <-- no longer required to instantiate BLEServer, less flash and ram usage
              ^~~~~~~

After commenting out that line it compiled without error.

Maybe some library is not compatible with the S2 version of the processor? I tried to compile for "Adafruit Feather ESP32-S2" and got a bunch of errors with undefined variables, starting with:

libraries/BLE/src/BLEBeacon.h:33:2: 
error: 'BLEUUID' does not name a type
  BLEUUID     getProximityUUID();
  ^~~~~~~

Thank you for the answer.

Indeed I was trying to use another board, but it remained selected in the IDE the S2 board.

Changed it and I seem to have an error from a library, or something.

Board: GroundStudio Carbon V3 - ARDUSHOP

Errors
In file included from C:\Users\andre\Documents\Arduino\libraries\ESP32_BLE_Arduino\src/BLEAdvertising.h:15,
                 from C:\Users\andre\Documents\Arduino\libraries\ESP32_BLE_Arduino\src/BLEServer.h:19,
                 from C:\Users\andre\Documents\Arduino\libraries\ESP32_BLE_Arduino\src/BLEDevice.h:18,
                 from D:\BLE_iBeacon\BLE_iBeacon.ino:2:
C:\Users\andre\Documents\Arduino\libraries\ESP32_BLE_Arduino\src/FreeRTOS.h:61:28: error: 'ringbuf_type_t' has not been declared
  Ringbuffer(size_t length, ringbuf_type_t type = RINGBUF_TYPE_NOSPLIT);
                            ^~~~~~~~~~~~~~
Multiple libraries were found for "BLEDevice.h"
 Used: C:\Users\andre\Documents\Arduino\libraries\ESP32_BLE_Arduino
 Not used: C:\Users\andre\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.5\libraries\BLE
exit status 1
Error compiling for board ESP32 Dev Module.

Update: I deleted the library from my documents and now it seems to work correctly.

One issue I have is that even though is that even though I have an UUID defined, the scanner finds the beacon with another UUID.

Code:
#define BEACON_UUID "87b99b2c-90fd-11e9-bc42-526af7764f64"

Scanner:

Same bytes in the UUID, just reversed:
"87 b9 9b 2c 90 fd 11 e9 bc 42 52 6a f7 76 4f 64"

"64 4f 76 f7 6a 52 42 bc e9 11 fd 90 2c 9b b9 87"

Damn, I didn't notice that.

What's even more strange is that it seems that if I monitor it in room-assistant, the id changes a bit

Summary
12/15/2022, 10:53:09 PM - error - BluetoothService: Failed to start advertising instance via BLE: Command Disallowed
12/15/2022, 10:53:19 PM - error - BluetoothService: Failed to start advertising instance via BLE: Command Disallowed
12/15/2022, 10:53:22 PM - info - BluetoothLowEnergyService: Discovered nearby BLE peripheral ESP32 as iBeacon with ID 644f76f76a5242bce911fd902c9bb987-0-2 and RSSI -65
12/15/2022, 10:53:30 PM - error - BluetoothService: Failed to start advertising instance via BLE: Command Disallowed
12/15/2022, 10:53:33 PM - info - BluetoothLowEnergyService: Discovered nearby BLE peripheral ESP32 as iBeacon with ID 644f76f76a5242bce911fd902c9bb987-0-3 and RSSI -68
12/15/2022, 10:53:40 PM - error - BluetoothService: Failed to start advertising instance via BLE: Command Disallowed
12/15/2022, 10:53:44 PM - info - BluetoothLowEnergyService: Discovered nearby BLE peripheral ESP32 as iBeacon with ID 644f76f76a5242bce911fd902c9bb987-0-4 and RSSI -70
12/15/2022, 10:53:51 PM - error - BluetoothService: Failed to start advertising instance via BLE: Command Disallowed
12/15/2022, 10:53:55 PM - info - BluetoothLowEnergyService: Discovered nearby BLE peripheral ESP32 as iBeacon with ID 644f76f76a5242bce911fd902c9bb987-0-5 and RSSI -69
12/15/2022, 10:54:01 PM - error - BluetoothService: Failed to start advertising instance via BLE: Command Disallowed
12/15/2022, 10:54:05 PM - info - BluetoothLowEnergyService: Discovered nearby BLE peripheral ESP32 as iBeacon with ID 644f76f76a5242bce911fd902c9bb987-0-6 and RSSI -67
12/15/2022, 10:54:12 PM - error - BluetoothService: Failed to start advertising instance via BLE: Command Disallowed
12/15/2022, 10:54:16 PM - info - BluetoothLowEnergyService: Discovered nearby BLE peripheral ESP32 as iBeacon with ID 644f76f76a5242bce911fd902c9bb987-0-7 and RSSI -67
12/15/2022, 10:54:22 PM - error - BluetoothService: Failed to start advertising instance via BLE: Command Disallowed
12/15/2022, 10:54:27 PM - info - BluetoothLowEnergyService: Discovered nearby BLE peripheral ESP32 as iBeacon with ID 644f76f76a5242bce911fd902c9bb987-0-8 and RSSI -70

No clue why, but at least it partially works. Thank you for the help.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.