2024-09-14 11:31:27 +02:00
|
|
|
#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>
|
2024-09-17 13:34:43 +02:00
|
|
|
#include <WiFi.h>
|
2024-09-14 11:31:27 +02:00
|
|
|
#include <PubSubClient.h>
|
2024-09-16 23:03:54 +02:00
|
|
|
|
2024-09-17 13:34:43 +02:00
|
|
|
WiFiClient espClient;
|
|
|
|
PubSubClient client(espClient);
|
|
|
|
|
|
|
|
#define mqtt_port 61883
|
|
|
|
|
2024-09-16 23:03:54 +02:00
|
|
|
//cf:d7:ab:1f:24:2c
|
|
|
|
String Adresse = "96:33:bb:bf:ff:ab"; // Bluetooth Adresse die zu Anwesenheitserkennung überwacht wird ET585
|
|
|
|
//String Adresse = "cf:d7:ab:1f:24:2c"; // Bluetooth Adresse die zu Anwesenheitserkennung überwacht wird Holy-IOT
|
|
|
|
const int RelaisPin = LED_BUILTIN; // Pin an dem Relais hängt
|
2024-09-14 11:31:27 +02:00
|
|
|
int Verzoegerung = 15; // Auschaltverzögerung wenn das Signal von BLE ibeacon fehlt
|
|
|
|
|
|
|
|
int VerzoegerungZaeler = 0;
|
2024-09-16 23:03:54 +02:00
|
|
|
//long int ZZ = 0;
|
2024-09-14 11:31:27 +02:00
|
|
|
Ticker Tic;
|
|
|
|
static BLEAddress *pServerAddress;
|
|
|
|
BLEScan* pBLEScan ;
|
|
|
|
int scanTime = 30; //In seconds
|
2024-09-16 23:03:54 +02:00
|
|
|
|
|
|
|
long int Feldstaerke;
|
|
|
|
|
2024-09-17 13:34:43 +02:00
|
|
|
// WiFI
|
|
|
|
IPAddress ip( 192, 168, 127, 241 ); // 10 bis 29 für feste IP
|
|
|
|
IPAddress gateway( 192, 168, 127, 1 );
|
|
|
|
IPAddress subnet( 255, 255, 255, 0 );
|
|
|
|
IPAddress dns(192, 168, 127, 1); // DNS-Server
|
|
|
|
String hostname = NAME;
|
|
|
|
char msg[20];
|
|
|
|
char clientName[30];
|
|
|
|
char topic[50];
|
|
|
|
|
|
|
|
const char* mqtt_zentrale = mqtt_server;
|
|
|
|
|
|
|
|
void callback(char* topic1, byte* payload, unsigned int length) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
Serial.print("Message arrived [");
|
|
|
|
Serial.print(topic1);
|
|
|
|
Serial.print("] ");
|
|
|
|
#endif
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
msg[i] = (char)payload[i];
|
|
|
|
}
|
|
|
|
msg[length] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
void reconnect() {
|
|
|
|
sprintf(clientName, "%s%s", "BLE", "_Gate" );
|
|
|
|
// Loop until we're reconnected
|
|
|
|
while (!client.connected()) {
|
|
|
|
Serial.print("Attempting MQTT connection...");
|
|
|
|
if (client.connect(clientName)) {
|
|
|
|
/* MQTTStatus.setPic(3); */
|
|
|
|
Serial.println("connected");
|
|
|
|
} else {
|
|
|
|
Serial.print("failed, rc=");
|
|
|
|
Serial.print(client.state());
|
|
|
|
Serial.println(" try again in 5 seconds");
|
|
|
|
// Wait 5 seconds before retrying
|
|
|
|
delay(5000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void setup_wifi() {
|
|
|
|
|
|
|
|
long ErrCount = 0;
|
|
|
|
|
|
|
|
delay(10);
|
|
|
|
// We start by connecting to a WiFi network
|
|
|
|
Serial.println();
|
|
|
|
Serial.print("Connecting to ");
|
|
|
|
Serial.print(STASSID);
|
|
|
|
Serial.print(" ");
|
|
|
|
|
|
|
|
// WiFi.forceSleepWake();
|
|
|
|
delay( 1 );
|
|
|
|
WiFi.persistent( false );
|
|
|
|
WiFi.setHostname(hostname.c_str()); //define hostname
|
|
|
|
WiFi.mode( WIFI_STA );
|
|
|
|
WiFi.config( ip, gateway, subnet, dns, dns );
|
|
|
|
|
|
|
|
WiFi.begin (STASSID, STAPSK);
|
|
|
|
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
|
|
delay(500);
|
|
|
|
Serial.print(".");
|
|
|
|
ErrCount ++;
|
|
|
|
if (ErrCount >= 20){
|
|
|
|
delay(100);
|
|
|
|
ESP.restart();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Serial.println(" WiFi connected");
|
|
|
|
client.setServer(mqtt_server, mqtt_port);
|
|
|
|
//client.setCallback(callback);
|
|
|
|
Serial.print("IP address: \t");
|
|
|
|
Serial.println(WiFi.localIP());
|
|
|
|
}
|
|
|
|
|
2024-09-16 23:03:54 +02:00
|
|
|
|
2024-09-14 11:31:27 +02:00
|
|
|
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks
|
|
|
|
{
|
|
|
|
void onResult(BLEAdvertisedDevice advertisedDevice) // passiert wenn BLE Device ( beacon ) gefunden wurde
|
|
|
|
{
|
2024-09-16 23:03:54 +02:00
|
|
|
//Serial.print(ZZ); Serial.print("\t");
|
2024-09-14 11:31:27 +02:00
|
|
|
Serial.print(advertisedDevice.getAddress().toString().c_str()); // ibeacon Adresse anzeigen
|
2024-09-16 23:03:54 +02:00
|
|
|
/* if (advertisedDevice.haveName()) {
|
|
|
|
Serial.print(" Device name: ");
|
|
|
|
Serial.print(advertisedDevice.getName().c_str());
|
|
|
|
} */
|
2024-09-14 11:31:27 +02:00
|
|
|
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(" ");
|
2024-09-16 23:03:54 +02:00
|
|
|
digitalWrite (RelaisPin, HIGH); // Relais Einschalten
|
2024-09-14 11:31:27 +02:00
|
|
|
VerzoegerungZaeler = 0; // Ausschaltverzögerung zurücksetzen
|
2024-09-17 13:34:43 +02:00
|
|
|
printf(topic, "%s%ld%s", "hjk/devices/", clientName, "/telemetry/status" );
|
|
|
|
client.publish(topic, "DA", true);
|
2024-09-14 11:31:27 +02:00
|
|
|
advertisedDevice.getScan()->stop(); // Scanvorgang beenden
|
|
|
|
} // Found our server
|
|
|
|
Serial.println("");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void SekundenTic() // Wird jede Sekunde ausgefüert
|
|
|
|
{
|
|
|
|
VerzoegerungZaeler++; // Sekundenzähler
|
2024-09-16 23:03:54 +02:00
|
|
|
//ZZ++;
|
2024-09-17 13:34:43 +02:00
|
|
|
if (VerzoegerungZaeler >= Verzoegerung){
|
|
|
|
digitalWrite (RelaisPin, LOW); // Wenn Verzögerungszeit erreicht wurde Auschalten
|
|
|
|
printf(topic, "%s%ld%s", "hjk/devices/", clientName, "/telemetry/status" );
|
|
|
|
client.publish(topic, "FERN", true);
|
|
|
|
}
|
2024-09-14 11:31:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
pinMode (RelaisPin, OUTPUT);
|
2024-09-16 23:03:54 +02:00
|
|
|
digitalWrite (RelaisPin, LOW);
|
2024-09-14 11:31:27 +02:00
|
|
|
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()
|
|
|
|
{
|
2024-09-17 13:34:43 +02:00
|
|
|
if (!client.connected()) {
|
|
|
|
reconnect();
|
|
|
|
}
|
|
|
|
client.loop();
|
2024-09-14 11:31:27 +02:00
|
|
|
pBLEScan->start(scanTime);
|
|
|
|
delay(2000); // Alle 2s nach ibeacon scannen
|
|
|
|
}
|