WiFiConfigurator.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #ifndef WiFiConfigurator_h
  2. #define WiFiConfigurator_h
  3. #include <WiFi.h>
  4. #include <WebServer.h>
  5. #include <Preferences.h>
  6. #include <DNSServer.h>
  7. class WiFiConfigurator {
  8. private:
  9. // 引脚定义
  10. const int _slowLedPin;
  11. const int _fastLedPin;
  12. const int _configButtonPin;
  13. // 网络配置
  14. const char* _ap_ssid;
  15. const char* _ap_password;
  16. const byte _dnsPort = 53;
  17. IPAddress _apIP;
  18. IPAddress _gateway;
  19. IPAddress _subnet;
  20. // 网络对象
  21. WebServer _server;
  22. DNSServer _dnsServer;
  23. Preferences _preferences;
  24. // 全局状态变量
  25. String _connectionStatus = "";
  26. int _connectionProgress = 0;
  27. bool _connectionInProgress = false;
  28. bool _connectionCompleted = false;
  29. bool _connectionSuccess = false;
  30. String _wifiScanResults = "";
  31. String _currentSsid = "";
  32. unsigned long _apTimeout = 0;
  33. const unsigned long _apTimeoutDuration = 120000; // 2分钟
  34. unsigned long _successNotificationTime = 0;
  35. const unsigned long _successNotificationDelay = 5000; // 5秒
  36. // LED控制变量
  37. unsigned long _previousSlowMillis = 0;
  38. unsigned long _previousFastMillis = 0;
  39. bool _slowLedState = LOW;
  40. bool _fastLedState = LOW;
  41. const unsigned long _slowInterval = 1000;
  42. const unsigned long _fastInterval = 200;
  43. // 按键控制变量
  44. unsigned long _buttonPressStartTime = 0;
  45. bool _buttonPressed = false;
  46. const unsigned long _configButtonPressDuration = 5000; // 5秒
  47. // HTML页面定义
  48. const char* _captive_html;
  49. const char* _index_html;
  50. const char* _success_html;
  51. const char* _error_html;
  52. // 内部方法
  53. void handleConfigButton();
  54. void enterConfigMode();
  55. void scanWiFiNetworks();
  56. void startAPMode();
  57. void connectToWiFi(const char* ssid, const char* password);
  58. // HTTP请求处理函数
  59. static void handleRoot();
  60. static void handleRefresh();
  61. static void handleConnect();
  62. static void handleStatus();
  63. static void handleSuccess();
  64. static void handleError();
  65. static void handleNotFound();
  66. // 静态指针用于回调函数访问实例
  67. static WiFiConfigurator* _instance;
  68. public:
  69. // 构造函数
  70. WiFiConfigurator(int slowLedPin, int fastLedPin, int configButtonPin,
  71. const char* ap_ssid, const char* ap_password,
  72. IPAddress apIP = IPAddress(192, 168, 4, 1),
  73. IPAddress gateway = IPAddress(192, 168, 4, 1),
  74. IPAddress subnet = IPAddress(255, 255, 255, 0));
  75. // 初始化函数
  76. void begin();
  77. // 主循环处理函数
  78. void loop();
  79. // 检查是否已连接WiFi
  80. bool isConnected();
  81. // 获取当前连接的SSID
  82. String getCurrentSSID();
  83. // 获取IP地址
  84. String getIPAddress();
  85. };
  86. #endif