| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- #ifndef WiFiConfigurator_h
- #define WiFiConfigurator_h
- #include <WiFi.h>
- #include <WebServer.h>
- #include <Preferences.h>
- #include <DNSServer.h>
- class WiFiConfigurator {
- private:
- // 引脚定义
- const int _slowLedPin;
- const int _fastLedPin;
- const int _configButtonPin;
-
- // 网络配置
- const char* _ap_ssid;
- const char* _ap_password;
- const byte _dnsPort = 53;
- IPAddress _apIP;
- IPAddress _gateway;
- IPAddress _subnet;
-
- // 网络对象
- WebServer _server;
- DNSServer _dnsServer;
- Preferences _preferences;
-
- // 全局状态变量
- String _connectionStatus = "";
- int _connectionProgress = 0;
- bool _connectionInProgress = false;
- bool _connectionCompleted = false;
- bool _connectionSuccess = false;
- String _wifiScanResults = "";
- String _currentSsid = "";
- unsigned long _apTimeout = 0;
- const unsigned long _apTimeoutDuration = 120000; // 2分钟
- unsigned long _successNotificationTime = 0;
- const unsigned long _successNotificationDelay = 5000; // 5秒
-
- // LED控制变量
- unsigned long _previousSlowMillis = 0;
- unsigned long _previousFastMillis = 0;
- bool _slowLedState = LOW;
- bool _fastLedState = LOW;
- const unsigned long _slowInterval = 1000;
- const unsigned long _fastInterval = 200;
-
- // 按键控制变量
- unsigned long _buttonPressStartTime = 0;
- bool _buttonPressed = false;
- const unsigned long _configButtonPressDuration = 5000; // 5秒
-
- // HTML页面定义
- const char* _captive_html;
- const char* _index_html;
- const char* _success_html;
- const char* _error_html;
-
- // 内部方法
- void handleConfigButton();
- void enterConfigMode();
- void scanWiFiNetworks();
- void startAPMode();
- void connectToWiFi(const char* ssid, const char* password);
-
- // HTTP请求处理函数
- static void handleRoot();
- static void handleRefresh();
- static void handleConnect();
- static void handleStatus();
- static void handleSuccess();
- static void handleError();
- static void handleNotFound();
-
- // 静态指针用于回调函数访问实例
- static WiFiConfigurator* _instance;
- public:
- // 构造函数
- WiFiConfigurator(int slowLedPin, int fastLedPin, int configButtonPin,
- const char* ap_ssid, const char* ap_password,
- IPAddress apIP = IPAddress(192, 168, 4, 1),
- IPAddress gateway = IPAddress(192, 168, 4, 1),
- IPAddress subnet = IPAddress(255, 255, 255, 0));
-
- // 初始化函数
- void begin();
-
- // 主循环处理函数
- void loop();
-
- // 检查是否已连接WiFi
- bool isConnected();
-
- // 获取当前连接的SSID
- String getCurrentSSID();
-
- // 获取IP地址
- String getIPAddress();
- };
- #endif
|