121 lines
3.9 KiB
C++
121 lines
3.9 KiB
C++
#ifndef WIFI_MANAGER_H
|
||
#define WIFI_MANAGER_H
|
||
|
||
#include <Arduino.h>
|
||
#include <WiFi.h>
|
||
#include <Preferences.h>
|
||
#include <ArduinoJson.h>
|
||
|
||
/**
|
||
* @brief 最大WiFi网络配置数量
|
||
* 定义设备可以保存的WiFi网络配置的最大数量
|
||
*/
|
||
#define MAX_WIFI_NETWORKS 10
|
||
|
||
/**
|
||
* @brief 最小信号强度阈值
|
||
* 定义WiFi网络信号强度的最低要求,低于此值的网络会被过滤
|
||
* 单位:dBm
|
||
*/
|
||
#define MIN_RSSI_THRESHOLD -200
|
||
|
||
/**
|
||
* @brief WiFi连接超时时间
|
||
* 定义WiFi连接的最大等待时间,超过此时间认为连接失败
|
||
* 单位:毫秒
|
||
*/
|
||
#define WIFI_CONNECT_TIMEOUT 15000
|
||
|
||
/**
|
||
* @brief WiFi重连间隔时间
|
||
* 定义WiFi断开后,尝试重新连接的时间间隔
|
||
* 单位:毫秒
|
||
*/
|
||
#define WIFI_RECONNECT_INTERVAL 2000
|
||
|
||
/**
|
||
* @brief WiFi网络信息结构
|
||
* 存储WiFi网络的详细信息,用于扫描和显示
|
||
*/
|
||
typedef struct {
|
||
char ssid[32]; // WiFi网络名称
|
||
char password[64]; // WiFi网络密码
|
||
int rssi; // 信号强度,单位:dBm
|
||
uint8_t channel; // WiFi通道
|
||
uint8_t encryption; // 加密类型
|
||
} WiFiNetworkInfo;
|
||
|
||
/**
|
||
* @brief WiFi配置结构
|
||
* 存储WiFi网络的配置信息,用于保存和加载
|
||
*/
|
||
typedef struct {
|
||
char ssid[32]; // WiFi网络名称
|
||
char password[64]; // WiFi网络密码
|
||
} WiFiConfig;
|
||
|
||
/**
|
||
* @brief WiFi管理器状态枚举
|
||
* 定义WiFi管理器的不同工作状态
|
||
*/
|
||
enum WiFiManagerState {
|
||
WIFI_IDLE, // 空闲状态
|
||
WIFI_SCANNING, // 扫描网络中
|
||
WIFI_CONNECTING, // 连接网络中
|
||
WIFI_CONNECTED, // 已连接
|
||
WIFI_DISCONNECTED, // 断开连接
|
||
WIFI_CONFIGURING // 配网模式
|
||
};
|
||
|
||
/**
|
||
* @brief 网络状态枚举
|
||
* 定义网络的不同状态,用于LED控制
|
||
*/
|
||
enum NetworkStatus {
|
||
NET_INITIAL, // 初始化/未连接 - 慢闪
|
||
NET_CONNECTING, // 连接中 - 快闪
|
||
NET_CONNECTED, // 已连接 - 呼吸灯
|
||
NET_DISCONNECTED // 断开连接 - 慢闪
|
||
};
|
||
|
||
/**
|
||
* @brief WiFi管理器类
|
||
* 负责WiFi网络的扫描、连接、配置和管理
|
||
*/
|
||
class WiFiManager {
|
||
private:
|
||
Preferences preferences; // 用于存储WiFi配置的Preferences对象
|
||
WiFiConfig savedNetworks[MAX_WIFI_NETWORKS]; // 保存的WiFi网络配置数组
|
||
int savedNetworkCount; // 已保存的网络数量
|
||
WiFiManagerState currentState; // 当前WiFi管理器状态
|
||
unsigned long lastReconnectAttempt; // 上次尝试重连的时间
|
||
|
||
bool scanAndMatchNetworks(); // 扫描并匹配网络
|
||
bool connectToNetwork(const char* ssid, const char* password); // 连接到指定网络
|
||
void sendScanResultsViaBLE(); // 发送扫描结果到BLE
|
||
bool saveWiFiConfig(const char* ssid, const char* password); // 保存WiFi配置
|
||
bool loadWiFiConfigs(); // 加载WiFi配置
|
||
|
||
public:
|
||
WiFiManager(); // 构造函数
|
||
void begin(); // 初始化WiFi管理器
|
||
|
||
bool initializeWiFi(); // 初始化WiFi连接
|
||
bool startConfiguration(); // 开始配网模式
|
||
bool handleConfigurationData(const char* ssid, const char* password); // 处理配网数据
|
||
void handleReconnect(); // 处理重连
|
||
|
||
WiFiManagerState getState(); // 获取当前状态
|
||
bool isConnected(); // 检查是否已连接
|
||
void disconnect(); // 断开连接
|
||
|
||
void scanAndSendResults(); // 扫描并发送结果
|
||
bool addWiFiConfig(const char* ssid, const char* password); // 添加WiFi配置
|
||
void clearAllConfigs(); // 清除所有配置
|
||
int getSavedNetworkCount(); // 获取已保存的网络数量
|
||
void getSavedNetworks(); // 获取已保存的WiFi网络列表
|
||
|
||
void update(); // 更新WiFi管理器状态
|
||
};
|
||
|
||
#endif |