79 lines
2.9 KiB
C++
79 lines
2.9 KiB
C++
|
#include <Arduino.h>
|
||
|
|
||
|
/*
|
||
|
ibeacon oder Beliebiger BLE Gerät Präsenz Überwachung mit Relais Anwesenheitserkennung
|
||
|
|
||
|
Michael Dworkin http://esp32-server.de/
|
||
|
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
|
||
|
Ported to Arduino ESP32 by Evandro Copercini
|
||
|
*/
|
||
|
#include <BLEDevice.h>
|
||
|
#include <BLEUtils.h>
|
||
|
#include <BLEScan.h>
|
||
|
#include <BLEAdvertisedDevice.h>
|
||
|
#include <BLEAddress.h>
|
||
|
#include <Ticker.h>
|
||
|
#include <PubSubClient.h>
|
||
|
|
||
|
String Adresse = "CF:D7:AB:1F:24:2C"; // Bluetooth Adresse die zu Anwesenheitserkennung überwacht wird
|
||
|
const int RelaisPin = 22; // Pin an dem Relais hängt
|
||
|
int Verzoegerung = 15; // Auschaltverzögerung wenn das Signal von BLE ibeacon fehlt
|
||
|
|
||
|
int VerzoegerungZaeler = 0;
|
||
|
Ticker Tic;
|
||
|
static BLEAddress *pServerAddress;
|
||
|
BLEScan* pBLEScan ;
|
||
|
int scanTime = 30; //In seconds
|
||
|
|
||
|
|
||
|
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks
|
||
|
{
|
||
|
void onResult(BLEAdvertisedDevice advertisedDevice) // passiert wenn BLE Device ( beacon ) gefunden wurde
|
||
|
{
|
||
|
Serial.print(advertisedDevice.getAddress().toString().c_str()); // ibeacon Adresse anzeigen
|
||
|
if (advertisedDevice.getAddress().equals(*pServerAddress)) // ibeacon Adresse Vergleichen
|
||
|
{
|
||
|
Serial.print(" Ueberwachte Adresse"); // wenn überwache Adresse gefunden wurde
|
||
|
if (advertisedDevice.haveName()) {
|
||
|
Serial.print(" Device name: ");
|
||
|
Serial.print(advertisedDevice.getName().c_str());
|
||
|
}
|
||
|
Serial.print(" RSSI: ");
|
||
|
Serial.print(advertisedDevice.getRSSI());
|
||
|
Serial.print(" dB ");
|
||
|
Serial.print(advertisedDevice.getTXPower());
|
||
|
Serial.print(" ");
|
||
|
digitalWrite (RelaisPin, LOW); // Relais Einschalten
|
||
|
VerzoegerungZaeler = 0; // Ausschaltverzögerung zurücksetzen
|
||
|
advertisedDevice.getScan()->stop(); // Scanvorgang beenden
|
||
|
} // Found our server
|
||
|
Serial.println("");
|
||
|
}
|
||
|
};
|
||
|
|
||
|
void SekundenTic() // Wird jede Sekunde ausgefüert
|
||
|
{
|
||
|
VerzoegerungZaeler++; // Sekundenzähler
|
||
|
if (VerzoegerungZaeler >= Verzoegerung) digitalWrite (RelaisPin, HIGH); // Wenn Verzögerungszeit erreicht wurde Auschalten
|
||
|
}
|
||
|
|
||
|
void setup()
|
||
|
{
|
||
|
pinMode (RelaisPin, OUTPUT);
|
||
|
digitalWrite (RelaisPin, HIGH);
|
||
|
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(2000); // Alle 2s nach ibeacon scannen
|
||
|
}
|