1 | /*
|
2 | MQTT Bluetooth (BLE) Anwesenheitserkennung. Raum präsenzmelder für Hausautomatisierung. Personen präsenz im Zimmer durch:
|
3 | iBeacon, Mi Band fitness tracker, smartwatch aufspüren. Publish ist MQTT_Name, Daten MAC und RSSI in JSON Format alle 10s
|
4 | {
|
5 | "28:A1:83:E1:58:8E":-60,
|
6 | "ea:5f:8d:69:be:56":-80
|
7 | }
|
8 | FHEM, Node-RED, iobroker, openHAB, Domoticz */
|
9 |
|
10 | #include <WiFi.h>
|
11 | #include <WiFiClient.h>
|
12 | #include <PubSubClient.h>
|
13 | #include <BLEDevice.h>
|
14 | #include <BLEScan.h>
|
15 | #include <BLEAddress.h>
|
16 |
|
17 | // MQTT Zugangsdaten
|
18 | #define MQTT_server "m21.cloudmqtt.com"
|
19 | #define MQTT_port 12247
|
20 | #define MQTT_User "iasfjkqc"
|
21 | #define MQTT_Pass "qsXY5N74m-VN"
|
22 | #define MQTT_Name "Schlafzimmer" // gleichzeitig publish Name
|
23 | #define QoS 0
|
24 |
|
25 | const char* ssid = "xxxxxxxxxxxxx"; // WLAN Zugangsdaten Eintragen
|
26 | const char* password = "xxxxxxxxx";
|
27 |
|
28 | int scanTime = 10; //sek. iBeacon Suchzeit und MQTT Berichtinterval
|
29 | BLEScan* pBLEScan ;
|
30 |
|
31 | // --------------- MQTT Variablen ---------------------------------------
|
32 | WiFiClient wclient;
|
33 | PubSubClient mqtt(wclient);
|
34 |
|
35 | void setup()
|
36 | {
|
37 | int i;
|
38 | Serial.begin(115200);
|
39 | Serial.println("");
|
40 | Serial.println("Start");
|
41 |
|
42 | // --------------------- WiFi ---------------------------------------------
|
43 | Serial.println("Warte auf Verbindung");
|
44 | WiFi.mode(WIFI_STA); ;
|
45 | WiFi.begin(ssid, password);
|
46 | Serial.println("");
|
47 | int timout = 0;
|
48 | while (WiFi.status() != WL_CONNECTED)
|
49 | {
|
50 | delay(500);
|
51 | Serial.print("O");
|
52 | timout++;
|
53 | if (timout > 20) // Wenn Anmeldung nicht möglich
|
54 | {
|
55 | Serial.println("");
|
56 | Serial.println("Wlan verbindung fehlt");
|
57 | ESP.restart(); // ESP32 neu starten
|
58 | }
|
59 | }
|
60 | Serial.println("");
|
61 | Serial.print("IP Addresse: ");
|
62 | Serial.println(WiFi.localIP());
|
63 |
|
64 | // ---------------------- MQTT ---------------------------------------------------
|
65 | mqtt.set_server(MQTT_server, MQTT_port); // MQTT Port einstellen
|
66 | if (mqtt.connect(MQTT::Connect(MQTT_Name).set_auth(MQTT_User, MQTT_Pass)))
|
67 | {
|
68 | Serial.println("Mit MQTT verbunden");
|
69 | mqtt.publish(MQTT::Publish("hallo", MQTT_Name).set_qos(QoS)); // im Topic hallo Präsenzmelder Name schreiben
|
70 | }
|
71 |
|
72 | Serial.println("Starte BLE Scanner");
|
73 | BLEDevice::init("");
|
74 | pBLEScan = BLEDevice::getScan();
|
75 | pBLEScan->setActiveScan(true); // Aktives Bluetooth Scannen
|
76 | pBLEScan->setInterval(100);
|
77 | pBLEScan->setWindow(99);
|
78 | }
|
79 |
|
80 | void loop()
|
81 | {
|
82 | int j;
|
83 | String NachrichtJSON;
|
84 | BLEScanResults foundDevices = pBLEScan->start(scanTime); // BLE Scanen
|
85 |
|
86 | NachrichtJSON = "{\r\n"; // JSON Format vorbereiten
|
87 |
|
88 | for (j = 0; j < foundDevices.getCount(); j++) // Alle Gefundene Bluetooth Device durchgehen
|
89 | {
|
90 | NachrichtJSON = NachrichtJSON + "\"" + foundDevices.getDevice(j).getAddress().toString().c_str() + "\":";
|
91 | if (j != foundDevices.getCount() - 1) NachrichtJSON = NachrichtJSON + String(foundDevices.getDevice(j).getRSSI()) + ",\r\n";
|
92 | else NachrichtJSON = NachrichtJSON + String(foundDevices.getDevice(j).getRSSI()) + "\r\n}";
|
93 | } // Alle gefundene MACs durchgehen
|
94 | // diese "if" spührt das letzte Eintragung und beendet den json Datensatz.
|
95 | pBLEScan->clearResults(); // Speicher freigeben
|
96 | mqtt.publish(MQTT::Publish(MQTT_Name, NachrichtJSON).set_qos(QoS)); // JSON per MQTT senden
|
97 | Serial.println(NachrichtJSON);
|
98 | delay(100);
|
99 | if (WiFi.status() != WL_CONNECTED) ESP.restart(); // Wenn Wlan fehlt neustarten
|
100 | if (mqtt.connected())
|
101 | {
|
102 | mqtt.loop();
|
103 | }
|
104 | else
|
105 | {
|
106 | Serial.println("MQTT Verbindung fehlt"); // Wenn MQTT Verbindung fehlt dann neu verbinden
|
107 | mqtt.connect(MQTT::Connect(MQTT_Name).set_auth(MQTT_User, MQTT_Pass));
|
108 | } // ende MQTT Verbindung ja/nein
|
109 | }
|