PersWiFiManagerExt.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #ifndef PERSWIFIMANAGER_H
  2. #define PERSWIFIMANAGER_H
  3. #if defined(ESP8266)
  4. #include <ESP8266WiFi.h>
  5. #include <ESP8266WebServer.h>
  6. #elif defined(ESP32)
  7. #include <WiFi.h>
  8. #include <WebServer.h>
  9. #else
  10. #error "Unknown board class"
  11. #endif
  12. #include <DNSServer.h>
  13. // CONFIGURATION
  14. //##define WIFI_HTM_PROGMEM
  15. //#define WIFI_HTM_PATH "/wifi.htm"
  16. //#define DEBUG
  17. //#define DEBUG_2
  18. // values in seconds - 0 to disable function
  19. #define WIFI_CONNECT_TIMEOUT_STARTAP 45 //s
  20. #define WIFI_CONNECT_TIMEOUT 30 //s
  21. #define WIFI_AP_TIMEOUT 5 //min
  22. #define WIFI_CONNCHECK_INTERVAL 20 //s
  23. #define WIFI_REBOOT_ON_NO_CONNECT_AFTER 0 //min
  24. #define WIFI_FORCE_RETRY_WIFI1_INTERVAL 30 //min
  25. //
  26. #define SETWIFI_CONNECT_TRIES 1
  27. // -------------
  28. class PersWiFiManager {
  29. public:
  30. typedef std::function<void(void)> WiFiChangeHandlerFunction;
  31. #if defined(ESP8266)
  32. PersWiFiManager(ESP8266WebServer& s, DNSServer& d);
  33. #elif defined(ESP32)
  34. PersWiFiManager(WebServer& s, DNSServer& d);
  35. #endif
  36. bool attemptConnection(const String& ssid = "", const String& pass = "");
  37. void setupWiFiHandlers();
  38. bool begin(const String& ssid = "", const String& pass = "");
  39. void resetSettings();
  40. String getApSsid();
  41. String getSsid();
  42. void setApCredentials(const String& apSsid, const String& apPass = "");
  43. void setApTimeout(int t);
  44. void setApStartAfter(int t);
  45. void setConnCheckInterval(int t);
  46. void setForceRetryWifi1Interval(int t);
  47. void setRebootOnNoConnect(int t);
  48. void setWifi1Credentials(const String& wSsid, const String& wPass = "");
  49. void setWifi2Credentials(const String& wSsid, const String& wPass = "");
  50. void setHttpCredentials(const String& user = "", const String& pass = "");
  51. void setConnectNonBlock(bool b);
  52. void setApModeAutostart(bool m);
  53. void handleWiFi();
  54. void startApMode();
  55. void stopApMode();
  56. void onConnect(WiFiChangeHandlerFunction fn);
  57. void onAp(WiFiChangeHandlerFunction fn);
  58. void onApOff(WiFiChangeHandlerFunction fn);
  59. int getActiveWiFiNum();
  60. private:
  61. #if defined(ESP8266)
  62. ESP8266WebServer * _server;
  63. #elif defined(ESP32)
  64. WebServer * _server;
  65. #endif
  66. DNSServer * _dnsServer;
  67. String _apSsid, _apPass;
  68. String _w1Ssid, _w1Pass;
  69. String _w2Ssid, _w2Pass;
  70. String _httpUser, _httpPass;
  71. bool _connectNonBlock;
  72. unsigned long _connectStartTime;
  73. unsigned long _apStartTime;
  74. bool _freshConnectionAttempt;
  75. bool _useSetWifi1Credentials;
  76. bool _useSetWifi2Credentials;
  77. unsigned int _wifi1ConnectAttempts; // count connect attempts
  78. unsigned int _wifi2ConnectAttempts; // count connect attempts
  79. unsigned int _connectSetWifi; // stores which set Wifi (via setWifi[1|2]Credentials) was tried/connected latest
  80. unsigned long _lastSuccessfulConnect;
  81. bool _apModeFinished;
  82. unsigned int _apTimeout;
  83. unsigned int _apStartAfter;
  84. unsigned int _connCheckInterval;
  85. unsigned int _rebootOnNoConnectAfter;
  86. unsigned int _forceRetryWifi1Interval;
  87. unsigned long _lastRunHandleWifi;
  88. unsigned long _lastConnectionCheckOK;
  89. int _wifiLastState;
  90. int _wifiDisconnectedCounter;
  91. unsigned long _forceRetryWifi1LastTime;
  92. bool _apModeEnabled;
  93. bool _httpAuth;
  94. WiFiChangeHandlerFunction _connectHandler;
  95. WiFiChangeHandlerFunction _apHandler;
  96. WiFiChangeHandlerFunction _apOffHandler;
  97. };//class
  98. #endif