Initial commit
This commit is contained in:
178
src/main.cpp
Normal file
178
src/main.cpp
Normal file
@@ -0,0 +1,178 @@
|
||||
#include <Arduino.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266HTTPClient.h>
|
||||
#include <settings.h>
|
||||
|
||||
|
||||
int numNetworks = sizeof(ssidList) / sizeof(ssidList[0]); // Anzahl der Netzwerke
|
||||
int current_ap = 0; // Start mit der ersten SSID
|
||||
bool reconnecting = false; // Flag für den Reconnect-Status
|
||||
|
||||
// Pin-Definitionen
|
||||
int PinRED = 5;
|
||||
int PinGREEN = 4;
|
||||
int PinBLUE = 0;
|
||||
|
||||
// Funktion, um die LED-Farbe zu setzen
|
||||
void led_set_color(String color) {
|
||||
if (color == "red") {
|
||||
digitalWrite(PinRED, HIGH);
|
||||
digitalWrite(PinGREEN, LOW);
|
||||
digitalWrite(PinBLUE, LOW);
|
||||
} else if (color == "green") {
|
||||
digitalWrite(PinRED, LOW);
|
||||
digitalWrite(PinGREEN, HIGH);
|
||||
digitalWrite(PinBLUE, LOW);
|
||||
} else if (color == "blue") {
|
||||
digitalWrite(PinRED, LOW);
|
||||
digitalWrite(PinGREEN, LOW);
|
||||
digitalWrite(PinBLUE, HIGH);
|
||||
} else if (color == "yellow") { // Rot + Grün
|
||||
digitalWrite(PinRED, HIGH);
|
||||
digitalWrite(PinGREEN, HIGH);
|
||||
digitalWrite(PinBLUE, LOW);
|
||||
} else if (color == "cyan") { // Grün + Blau
|
||||
digitalWrite(PinRED, LOW);
|
||||
digitalWrite(PinGREEN, HIGH);
|
||||
digitalWrite(PinBLUE, HIGH);
|
||||
} else if (color == "pink") { // Rot + Blau (auch Magenta)
|
||||
digitalWrite(PinRED, HIGH);
|
||||
digitalWrite(PinGREEN, LOW);
|
||||
digitalWrite(PinBLUE, HIGH);
|
||||
} else if (color == "white") { // Alle Farben
|
||||
digitalWrite(PinRED, HIGH);
|
||||
digitalWrite(PinGREEN, HIGH);
|
||||
digitalWrite(PinBLUE, HIGH);
|
||||
} else if (color == "off") { // LED aus
|
||||
digitalWrite(PinRED, LOW);
|
||||
digitalWrite(PinGREEN, LOW);
|
||||
digitalWrite(PinBLUE, LOW);
|
||||
} else {
|
||||
Serial.println("Unbekannte Farbe: " + color);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Funktion, um zu einem der Netzwerke zu verbinden
|
||||
void connectToWiFi() {
|
||||
|
||||
Serial.println("WLAN Verbindungsaufbau zu: " + String(ssidList[current_ap]));
|
||||
|
||||
|
||||
// Verbinde mit dem aktuellen AP (SSID und Passwort aus den Arrays)
|
||||
WiFi.begin(ssidList[current_ap], passwordList[current_ap]);
|
||||
|
||||
// Warte auf die Verbindung
|
||||
int attempt = 0;
|
||||
while (WiFi.status() != WL_CONNECTED && attempt < 10) {
|
||||
led_set_color("cyan");
|
||||
Serial.print("."); // Punkt ausgeben, um anzuzeigen, dass es versucht, sich zu verbinden
|
||||
delay(500);
|
||||
led_set_color("off");
|
||||
Serial.print("."); // Punkt ausgeben, um anzuzeigen, dass es versucht, sich zu verbinden
|
||||
delay(500);
|
||||
attempt++;
|
||||
}
|
||||
|
||||
// Wenn keine Verbindung hergestellt werden konnte, wechsle zum nächsten Netzwerk
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
WiFi.disconnect();
|
||||
delay(500);
|
||||
Serial.println("");
|
||||
Serial.println("Verbindung fehlgeschlagen.");
|
||||
current_ap = (current_ap + 1) % numNetworks; // Wechsel zur nächsten SSID, nach der letzten wieder zur ersten
|
||||
reconnecting = false; // Zurücksetzen des Flags, falls alle Netzwerke fehlgeschlagen sind
|
||||
}
|
||||
else {
|
||||
Serial.print("\nWLAN verbunden: ");
|
||||
Serial.print(WiFi.SSID());
|
||||
Serial.print(" ");
|
||||
Serial.println(WiFi.RSSI());
|
||||
|
||||
reconnecting = false; // Verbindung erfolgreich, setze reconnecting zurück
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Funktion zum Extrahieren des Wertes aus der XML-Antwort
|
||||
String extractValue(const String& url) {
|
||||
WiFiClient client; // WiFiClient-Objekt erstellen
|
||||
HTTPClient http;
|
||||
|
||||
http.begin(client, url); // WiFiClient und URL übergeben
|
||||
|
||||
int httpResponseCode = http.GET();
|
||||
if (httpResponseCode > 0) {
|
||||
String response = http.getString();
|
||||
Serial.println("Antwort: " + response);
|
||||
|
||||
int startIndex = response.indexOf("value='");
|
||||
if (startIndex == -1) {
|
||||
http.end(); // Verbindung schließen
|
||||
return "";
|
||||
}
|
||||
|
||||
startIndex += 7; // Länge von "value='"
|
||||
int endIndex = response.indexOf("'", startIndex);
|
||||
if (endIndex == -1) {
|
||||
http.end(); // Verbindung schließen
|
||||
return "";
|
||||
}
|
||||
|
||||
http.end(); // Verbindung schließen
|
||||
return response.substring(startIndex, endIndex);
|
||||
} else {
|
||||
Serial.println("HTTP-Fehler: " + String(httpResponseCode));
|
||||
}
|
||||
|
||||
http.end(); // Verbindung schließen
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(200);
|
||||
|
||||
Serial.println("Setze Pins als Ausgänge");
|
||||
pinMode(PinRED, OUTPUT);
|
||||
pinMode(PinGREEN, OUTPUT);
|
||||
pinMode(PinBLUE, OUTPUT);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
// Prüfe, ob die Verbindung verloren wurde
|
||||
if (WiFi.status() != WL_CONNECTED && !reconnecting) {
|
||||
Serial.println("WLAN Verbindung verloren. Versuche erneut zu verbinden...");
|
||||
reconnecting = true; // Setze Flag, dass ein Reconnectversuch läuft
|
||||
connectToWiFi(); // Versuche eine neue Verbindung
|
||||
}
|
||||
|
||||
// Wenn alle Netzwerke durchlaufen wurden und noch keine Verbindung besteht, setze das reconnecting-Flag zurück
|
||||
if (WiFi.status() != WL_CONNECTED && current_ap == 0) {
|
||||
reconnecting = false;
|
||||
}
|
||||
|
||||
|
||||
if (WiFi.status() == WL_CONNECTED && !reconnecting) {
|
||||
|
||||
|
||||
String value = extractValue(hm_sysvar_url);
|
||||
Serial.println("Extrahierter Wert (Lüften sinnvoll?): " + value);
|
||||
|
||||
if (value) {
|
||||
led_set_color("green");
|
||||
Serial.println("Grüne LED einschalten");
|
||||
|
||||
} else {
|
||||
led_set_color("red");
|
||||
Serial.println("Rote LED einschalten");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
delay(60000);
|
||||
}
|
||||
29
src/settings.h
Normal file
29
src/settings.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef SETTINGS_H
|
||||
#define SETTINGS_H
|
||||
|
||||
// ######################## WLAN-Daten ########################
|
||||
|
||||
const char* hostname = "ESP6288_Keller_Lueften";
|
||||
|
||||
const char* ssidList[] = {
|
||||
"SSID_1", // Netzwerk 1
|
||||
"SSID_2", // Netzwerk 2
|
||||
"SSID_3" // Netzwerk 3
|
||||
// Weitere SSID hier hinzufügen
|
||||
};
|
||||
|
||||
const char* passwordList[] = {
|
||||
"Passwort_1", // Passwort für SSID 1
|
||||
"Passwort_2", // Passwort für SSID 2
|
||||
"Passwort_3" // Passwort für SSID 3
|
||||
// Weitere Passwörter hier hinzufügen
|
||||
};
|
||||
|
||||
// ################# Homematic Systemvariable #################
|
||||
|
||||
const char* hm_sysvar_url = "http://192.168.178.50/addons/xmlapi/sysvar.cgi?sid=kYvzkyRPluLBsfLL&ise_id=20746";
|
||||
|
||||
// ############################################################
|
||||
|
||||
|
||||
#endif // SETTINGS_H
|
||||
Reference in New Issue
Block a user