122 lines
3.5 KiB
C++
122 lines
3.5 KiB
C++
/**
|
|
* @file wifi_manager.h
|
|
* @brief WiFi 配网管理器 (AP + WebServer)
|
|
*
|
|
* 功能:
|
|
* - 自动连接已保存的 WiFi
|
|
* - 配网失败/首次启动 → AP 模式 + Captive Portal
|
|
* - 自定义参数: MQTT 服务器/端口/用户/密码、设备名称、环境备注
|
|
* - 长按按钮重置配置
|
|
* - LED 状态指示
|
|
* - LittleFS 持久化存储
|
|
*/
|
|
|
|
#ifndef WIFI_MANAGER_H
|
|
#define WIFI_MANAGER_H
|
|
|
|
#include <Arduino.h>
|
|
#include <WiFi.h>
|
|
#include <WebServer.h>
|
|
#include <DNSServer.h>
|
|
#include <LittleFS.h>
|
|
#include <BLEDevice.h>
|
|
#include <BLEServer.h>
|
|
#include <BLEUtils.h>
|
|
#include <BLE2902.h>
|
|
#include <ArduinoJson.h>
|
|
#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 env_type[16]; // 环境类型 bedroom/bathroom/...
|
|
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 handleBLEProvision(const char* json); // BLE 配网回调
|
|
void enqueueBLEJson(const char* json); // BLE 线程安全入队 (仅暂存)
|
|
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 页面
|
|
|
|
// BLE 蓝牙配网
|
|
void _startBLE();
|
|
void _stopBLE();
|
|
|
|
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=常亮
|
|
|
|
// BLE
|
|
BLEServer* _bleServer;
|
|
BLECharacteristic* _bleInfoChar; // READ: 设备信息
|
|
BLECharacteristic* _bleChar; // WRITE+NOTIFY: 配网
|
|
BLEAdvertising* _bleAdv;
|
|
|
|
// BLE 回调保护: 不在 BTC_TASK 栈里做 Flash 写操作
|
|
String _blePendingJson; // 待处理的 BLE 配网 JSON
|
|
bool _blePendingFlag; // 有未处理的 BLE 数据
|
|
void _processBLEPending(); // 在 loop() 上下文处理
|
|
|
|
// AP SSID (前缀+MAC后4位, BLE 命名复用)
|
|
String _apSsid;
|
|
};
|
|
|
|
#endif // WIFI_MANAGER_H
|