/** * @file wifi_manager.h * @brief WiFi 配网管理器 (AP + WebServer) * * 功能: * - 自动连接已保存的 WiFi * - 配网失败/首次启动 → AP 模式 + Captive Portal * - 自定义参数: MQTT 服务器/端口/用户/密码、设备名称、环境备注 * - 长按按钮重置配置 * - LED 状态指示 * - LittleFS 持久化存储 */ #ifndef WIFI_MANAGER_H #define WIFI_MANAGER_H #include #include #include #include #include #include #include "config.h" // 设备配置结构体 struct DeviceConfig { char mqtt_server[40]; char mqtt_port[6]; char mqtt_user[20]; char mqtt_pass[20]; char device_id[20]; char env_note[20]; // 环境备注 char wifi_ssid[33]; // WiFi SSID (持久化) char wifi_pass[65]; // WiFi 密码 (持久化) }; class WiFiMgmt { public: WiFiMgmt(); ~WiFiMgmt(); // 单例指针 (用于静态回调) static WiFiMgmt* _instance; bool begin(); bool loop(); bool isConnected() const; bool justProvisioned(); // 刚配网成功 (读取后自动清除) DeviceConfig getConfig() const; const char* getDeviceId() const; const char* getEnvNote() const; void startConfigPortal(); void resetSettings(); private: bool _connectWiFi(); // 仅尝试已保存凭据 bool _tryConnect(const char* ssid, const char* pass); // 尝试连接指定 WiFi bool _loadConfig(); bool _saveConfig(); void _setLEDMode(uint8_t mode); static void _ledTaskFunc(void* param); bool _checkButtonLongPress(); // Web 服务器 void _startPortal(); void _stopPortal(); void _handleRoot(); void _handleSave(); void _handleReset(); void _updateWifiScan(); // WiFi 扫描缓存 String _htmlPage(); // 返回配网 HTML 页面 WebServer* _server; DNSServer* _dns; DeviceConfig _config; bool _connected; bool _portalActive; bool _portalStopFlag; // 延迟关闭标记 uint32_t _portalStartTime; String _pendingSSID; String _pendingPass; bool _provisionSuccess; // 刚配网成功标记 uint32_t _lastCheckTime; uint32_t _buttonPressTime; bool _buttonPressed; // WiFi 扫描缓存 (避免每次打开页面都扫描3-8秒) String _wifiScanCache; uint32_t _lastScanTime; // LED 独立任务 TaskHandle_t _ledTaskHandle; volatile uint8_t _ledMode; // 0=灭 1=慢闪 2=快闪 3=常亮 }; #endif // WIFI_MANAGER_H