2024-09-1701

This commit is contained in:
hans-jurgen 2024-09-17 13:34:43 +02:00
parent a311dccf29
commit a28e509697
2 changed files with 97 additions and 2 deletions

View File

@ -23,7 +23,7 @@ lib_deps =
build_flags = ${env.build_flags}
-DDEBUG=0
-DNOADS=0
-DNAME=\"WETTERSTATION\"
-DNAME=\"BLE_GATE\"
-DSTASSID=\"MagentaWLAN-RGDO\"
-DSTAPSK=\"93329248424922704583\"
-DGATEWAY=\"192.168.127.1\"

View File

@ -13,8 +13,14 @@
#include <BLEAdvertisedDevice.h>
#include <BLEAddress.h>
#include <Ticker.h>
#include <WiFi.h>
#include <PubSubClient.h>
WiFiClient espClient;
PubSubClient client(espClient);
#define mqtt_port 61883
//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
@ -30,6 +36,85 @@ int scanTime = 30; //In seconds
long int Feldstaerke;
// 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());
}
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks
{
@ -55,6 +140,8 @@ class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks
Serial.print(" ");
digitalWrite (RelaisPin, HIGH); // Relais Einschalten
VerzoegerungZaeler = 0; // Ausschaltverzögerung zurücksetzen
printf(topic, "%s%ld%s", "hjk/devices/", clientName, "/telemetry/status" );
client.publish(topic, "DA", true);
advertisedDevice.getScan()->stop(); // Scanvorgang beenden
} // Found our server
Serial.println("");
@ -65,7 +152,11 @@ void SekundenTic() // Wird jede Sekunde ausgefüert
{
VerzoegerungZaeler++; // Sekundenzähler
//ZZ++;
if (VerzoegerungZaeler >= Verzoegerung) digitalWrite (RelaisPin, LOW); // Wenn Verzögerungszeit erreicht wurde Auschalten
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);
}
}
void setup()
@ -85,6 +176,10 @@ void setup()
void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
pBLEScan->start(scanTime);
delay(2000); // Alle 2s nach ibeacon scannen
}