Beacon_scaner/src/main.cpp

214 lines
6.4 KiB
C++
Raw Normal View History

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);
2024-09-17 15:20:26 +02:00
#define mqtt_port 1883
2024-09-17 13:34:43 +02:00
2024-09-16 23:03:54 +02:00
//cf:d7:ab:1f:24:2c
2024-09-24 23:29:12 +02:00
String Adresse = TRANSPONDER; // Bluetooth Adresse die zu Anwesenheitserkennung überwacht wird ET585
2024-09-23 16:33:16 +02:00
//String Adresse = "cf:d7:ab:1f:24:2c"; // Bluetooth Adresse die zu Anwesenheitserkennung überwacht wird Holy-IOT
2024-09-16 23:03:54 +02:00
const int RelaisPin = LED_BUILTIN; // Pin an dem Relais hängt
2024-09-23 16:33:16 +02:00
int Verzoegerung = 30; // Auschaltverzögerung wenn das Signal von BLE ibeacon fehlt
2024-09-14 11:31:27 +02:00
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 ;
2024-09-23 16:33:16 +02:00
int scanTime = 5; //In seconds
2024-09-16 23:03:54 +02:00
long int Feldstaerke;
2024-09-17 13:34:43 +02:00
// WiFI
2024-09-24 11:15:49 +02:00
IPAddress ip;
IPAddress gateway;
IPAddress subnet(255,255,255,0);
IPAddress dns;
IPAddress secondaryDNS(8, 8, 8, 8);
2024-09-17 13:34:43 +02:00
String hostname = NAME;
2024-09-24 11:15:49 +02:00
const char *MyIP = KMYIP;
2024-09-17 13:34:43 +02:00
char msg[20];
char clientName[30];
char topic[50];
2024-09-23 15:48:42 +02:00
bool gefunden = false;
2024-09-17 13:34:43 +02:00
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 );
2024-09-24 11:15:49 +02:00
if (!ip.fromString(MyIP)) { // try to parse into the IPAddress
Serial.println("UnParsable IP");
}
if (!dns.fromString(DNS)) { // try to parse into the IPAddress
Serial.println("UnParsable DNS");
}
if (!gateway.fromString(GATEWAY)) { // try to parse into the IPAddress
Serial.println("UnParsable GATEWAY");
}
//WiFi.config( ip, dns, gateway, subnet );
if (!WiFi.config(ip, gateway, subnet, dns, secondaryDNS))
{
Serial.println("STA Failed to configure");
}
2024-09-17 13:34:43 +02:00
WiFi.begin (STASSID, STAPSK);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
ErrCount ++;
if (ErrCount >= 20){
2024-09-26 21:44:20 +02:00
delay(200);
2024-09-17 13:34:43 +02:00
ESP.restart();
}
}
Serial.println(" WiFi connected");
client.setServer(mqtt_server, mqtt_port);
2024-09-17 15:20:26 +02:00
client.setCallback(callback);
2024-09-17 13:34:43 +02:00
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-29 20:21:55 +02:00
Serial.print(" RSSI: ");
Serial.print(advertisedDevice.getRSSI());
2024-09-16 23:03:54 +02:00
/* if (advertisedDevice.haveName()) {
Serial.print(" Device name: ");
Serial.print(advertisedDevice.getName().c_str());
} */
2024-09-30 12:56:09 +02:00
if (advertisedDevice.getAddress().equals(*pServerAddress)and (advertisedDevice.getRSSI() > -85)) // ibeacon Adresse Vergleichen
2024-09-14 11:31:27 +02:00
{
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 ");
2024-09-29 20:21:55 +02:00
/* 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
advertisedDevice.getScan()->stop(); // Scanvorgang beenden
2024-09-23 15:48:42 +02:00
gefunden = true;
2024-09-14 11:31:27 +02:00
} // 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
2024-09-23 15:48:42 +02:00
/* sprintf(topic, "%s%ld%s", "hjk/devices/", clientName, "/telemetry/status" );
client.publish(topic, "FERN", true); */
gefunden = false;
2024-09-17 13:34:43 +02:00
}
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("");
2024-09-17 15:20:26 +02:00
setup_wifi();
2024-09-14 11:31:27 +02:00
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);
2024-09-23 15:48:42 +02:00
if (gefunden == true){
2024-09-24 23:29:12 +02:00
sprintf(topic, "%s%s%s", "hjk/devices/", ORT, "/telemetry/status" );
2024-09-23 15:48:42 +02:00
client.publish(topic, "DA", true);
Serial.println();
Serial.print(topic); Serial.println(" DA");
} else {
2024-09-24 23:29:12 +02:00
sprintf(topic, "%s%s%s", "hjk/devices/", ORT, "/telemetry/status" );
2024-09-23 15:48:42 +02:00
client.publish(topic, "FERN", true);
Serial.println();
Serial.print(topic); Serial.println(" FERN");
}
2024-09-14 11:31:27 +02:00
delay(2000); // Alle 2s nach ibeacon scannen
}