Good evening, I am using a NodeMCU-32S and running the code below. I cant seem to get the Realy to operate when the corred BLEAddress is found. The Serial Print output certainly indicates when the correct BLEAddress is forun, but the Relay does not toggle. Coudl someone help please
Here is the code;
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include <BLEAddress.h>
#include <BLEDevice.h>
#include <Ticker.h>
String Adresse = "A8:42:E3:57:98:5E"; // Bluetooth MAC to watch for
const int RelayPin = 22; // pin of relay to swiwtch
int Delay = 15; // delay after which the relay will be switched when BLE device is out of range
int DelayCounter = 0;
Ticker Tic;
static BLEAddress *pServerAddress;
BLEScan* pBLEScan ;
int scanTime = 30; //In seconds
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks
{
void onResult(BLEAdvertisedDevice advertisedDevice) // when BLE device is found
{
Serial.print(advertisedDevice.getAddress().toString().c_str()); // show MAC of BLE
if (advertisedDevice.getAddress().equals(*pServerAddress)) // compare MAC
{
Serial.print(" Monitored Address"); // monitored MAC found
digitalWrite (RelayPin, 0); // switch relay
DelayCounter = 0; // reset delay
advertisedDevice.getScan()->stop(); // stop scanning
} // Found our server
Serial.println("");
}
};
void SekundenTic() // once per second
{
DelayCounter++; // time counter
if (DelayCounter >= Delay) digitalWrite (RelayPin, 1); // switch off after delay
}
void setup()
{
pinMode (RelayPin, OUTPUT);
digitalWrite (RelayPin, 1);
Serial.begin(115200);
Serial.println("");
Serial.println("Starte BLE Scanner");
pServerAddress = new BLEAddress(Adresse.c_str());
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); // create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); // active scan uses more power, but get results faster
Tic.attach( 1,SekundenTic);
}
void loop()
{
pBLEScan->start(scanTime);
delay(1000); // scan every 1s for devices
}
Thank you Peter