iBeacon detector [SOLVED]

So I got an ESP32 working to detect an iBeacon transmitting from an android phone but when I turn off the beacon the LED won't turn off and I can't find the reason it is not doing it since I got the delays properly set. Here's the code:

#include <Arduino.h>

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include <BLEBeacon.h>

#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00) >> 8) + (((x)&0xFF) << 8))

int scanTime = 5; //In seconds
BLEScan *pBLEScan;
bool foundBeacon = false;

class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
{
    void onResult(BLEAdvertisedDevice advertisedDevice)
    {
      if (advertisedDevice.haveManufacturerData() == true)
      {
        std::string strManufacturerData = advertisedDevice.getManufacturerData();

        uint8_t cManufacturerData[100];
        strManufacturerData.copy((char *)cManufacturerData, strManufacturerData.length(), 0);

        if (strManufacturerData.length() == 25 && cManufacturerData[0] == 0x4C && cManufacturerData[1] == 0x00)
        {
          BLEBeacon oBeacon = BLEBeacon();
          oBeacon.setData(strManufacturerData);
          String UUID = oBeacon.getProximityUUID().toString().c_str();
          if (UUID == "3e89e071-5bbc-2480-984e-a24fa66d82f7") { //change "12345678-1234-1234-1234-123456789abc" to the UUID of the beacon you're looking for
            digitalWrite(2,HIGH); //Turns on LED on pin 2
            foundBeacon = true; //set foundBeacon flag to true
            Serial.println("Found specified iBeacon!"); //Outputs message
          }
        }
      }
    }
};

void setup() {
  Serial.begin(115200);
  BLEDevice::init("");
  pBLEScan = BLEDevice::getScan(); //create new scan
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
  pinMode(2,OUTPUT); //Sets pin 2 as output
}

void loop() {
  BLEDevice::getScan()->start(scanTime);
  if(foundBeacon) {
    delay(10000); //10 seconds delay
    if(!foundBeacon) {
      digitalWrite(2, LOW); //Turn off pin 2
    }
    foundBeacon = false;
  } else {
    delay(10000); //10 seconds delay
  }
}

Basically what i'm trying to accomplish here is to later change the pin, add a second relay with opposite output and control a relay based on the beacon's presence

This is the only place in your code where you set pin 2 LOW, BUT if you look at the logic that proceeds it... the code can never get to this point. foundBeacon needs to be true to get passed the first "if" and false to get passed the second "if"... so not possible.

got it, yeah I just changed it to this and it's working, might not be the prettiest but when i turn off the beacon the LED turns off

void loop() {
  BLEDevice::getScan()->start(scanTime);
  if(foundBeacon) {
    delay(1000); //2 minutes delay
    if(!foundBeacon) {
      digitalWrite(2, LOW); //Turn off pin 2
    }
    foundBeacon = false;
  } else {
    digitalWrite(2, LOW); //Turn off pin 2
    Serial.println("iBeacon not Found");
    delay(1000); //2 minutes delay
  }
}

You can probably get rid of these lines completely.

Thanks a ton, already deleted that part and the code is working pretty well, just gotta flush out a couple of errors but that's more hardware, here's the code for future reference:

#include <Arduino.h>

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include <BLEBeacon.h>

#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00) >> 8) + (((x)&0xFF) << 8))

int scanTime = 5; //In seconds
BLEScan *pBLEScan;
bool foundBeacon = false;

class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks
{
    void onResult(BLEAdvertisedDevice advertisedDevice)
    {
      if (advertisedDevice.haveManufacturerData() == true)
      {
        std::string strManufacturerData = advertisedDevice.getManufacturerData();

        uint8_t cManufacturerData[100];
        strManufacturerData.copy((char *)cManufacturerData, strManufacturerData.length(), 0);

        if (strManufacturerData.length() == 25 && cManufacturerData[0] == 0x4C && cManufacturerData[1] == 0x00)
        {
          BLEBeacon oBeacon = BLEBeacon();
          oBeacon.setData(strManufacturerData);
          String UUID = oBeacon.getProximityUUID().toString().c_str();
          if (UUID == "3e89e071-5bbc-2480-984e-a24fa66d82f7") { //change "12345678-1234-1234-1234-123456789abc" to the UUID of the beacon you're looking for
            digitalWrite(2,HIGH); //Turns on LED on pin 2
            digitalWrite(23, LOW);
            foundBeacon = true; //set foundBeacon flag to true
            Serial.println("Found specified iBeacon!"); //Outputs message
          }
        }
      }
    }
};

void setup() {
  Serial.begin(115200);
  BLEDevice::init("");
  pBLEScan = BLEDevice::getScan(); //create new scan
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
  pinMode(2,OUTPUT); //Sets pin 2 as output
  pinMode(23, OUTPUT); //second relay
}

void loop() {
  BLEDevice::getScan()->start(scanTime);
  if(foundBeacon) {
    delay(120000); //2 minutes delay
    foundBeacon = false;
  } else {
    Serial.println("iBeacon not Found");
    delay(120000); //2 minutes delay
    digitalWrite(2, LOW); //Turn off pin 2
    digitalWrite(23, HIGH); //second relay
  }
}

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