- include/wifi_connector.h: WifiConnector class with WifiStatus enum, onStatusChange callback, begin/loop/reset methods - src/wifi_connector.cpp: autoConnect with AP timeout and restart, reconnect logic on connection loss, printToSerial diagnostics - src/main.cpp: call wifiConnector.begin() and loop() - test_sketches/test_wifi.cpp: first-connect via captive portal, auto-reconnect after reboot, BOOT-button (3s) resets credentials - platformio.ini: add test-wifi environment - Implementation-Plan.md: mark tasks 3.1-3.3 complete
83 lines
2.9 KiB
C++
83 lines
2.9 KiB
C++
#pragma once
|
||
|
||
// =============================================================================
|
||
// wifi_connector.h – WiFi-Verbindungsmanagement via WiFiManager
|
||
// Projekt: MQTT-Display LaserCutter
|
||
//
|
||
// Verwendung:
|
||
// wifi.onStatusChange(myCallback); // optional, vor begin()
|
||
// wifi.begin(); // blockiert bis verbunden oder Timeout
|
||
// if (wifi.isConnected()) { ... }
|
||
// wifi.loop(); // in loop() aufrufen (Reconnect-Logik)
|
||
// =============================================================================
|
||
|
||
#include <Arduino.h>
|
||
#include <WiFi.h>
|
||
#include <WiFiManager.h>
|
||
#include "config.h"
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Verbindungsstatus
|
||
// ---------------------------------------------------------------------------
|
||
enum class WifiStatus {
|
||
DISCONNECTED, // Nicht verbunden, kein AP aktiv
|
||
AP_ACTIVE, // Konfigurations-Portal läuft (AP-Modus)
|
||
CONNECTING, // Verbindungsversuch läuft
|
||
CONNECTED // Verbunden mit WLAN
|
||
};
|
||
|
||
// Callback-Typ: wird bei Statusänderungen aufgerufen
|
||
using WifiStatusCallback = void (*)(WifiStatus newStatus);
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// WifiConnector
|
||
// ---------------------------------------------------------------------------
|
||
class WifiConnector {
|
||
public:
|
||
WifiConnector();
|
||
|
||
// Optionaler AP-Passwortschutz (Standard: kein Passwort)
|
||
void setApPassword(const char* password);
|
||
|
||
// Callback vor begin() registrieren
|
||
void onStatusChange(WifiStatusCallback cb);
|
||
|
||
// WiFi starten: versucht zuerst gespeicherte Credentials zu verwenden.
|
||
// Schlägt das fehl, öffnet das Captive Portal (AP "LaserCutter-Setup").
|
||
// Timeout: WIFI_AP_TIMEOUT_S Sekunden, dann ESP.restart().
|
||
// Blockiert bis verbunden oder Timeout.
|
||
void begin();
|
||
|
||
// In loop() aufrufen: erkennt Verbindungsabbrüche und veranlasst Reconnect.
|
||
void loop();
|
||
|
||
// Aktuellen Status abfragen
|
||
WifiStatus getStatus() const { return _status; }
|
||
bool isConnected() const { return _status == WifiStatus::CONNECTED; }
|
||
IPAddress getIP() const { return WiFi.localIP(); }
|
||
String getSSID() const { return WiFi.SSID(); }
|
||
int8_t getRSSI() const { return WiFi.RSSI(); }
|
||
|
||
// Debug-Ausgabe auf Serial
|
||
void printToSerial() const;
|
||
|
||
// Gespeicherte WiFi-Credentials löschen (zwingt den AP beim nächsten Start)
|
||
void resetCredentials();
|
||
|
||
private:
|
||
WifiStatusCallback _cb;
|
||
WifiStatus _status;
|
||
char _apPassword[32];
|
||
uint32_t _lastConnectedMs;
|
||
|
||
void setStatus(WifiStatus s);
|
||
|
||
// Callbacks für WiFiManager (static nötig, C-Funktionszeiger)
|
||
static WifiConnector* _instance;
|
||
static void _onApStarted();
|
||
static void _onApStopped();
|
||
};
|
||
|
||
// Globale Instanz
|
||
extern WifiConnector wifiConnector;
|