Fork of the excellent esp8266-react - https://github.com/rjwats/esp8266-react
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.6 KiB

  1. #include <WiFiSettingsService.h>
  2. WiFiSettingsService::WiFiSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) :
  3. _httpEndpoint(WiFiSettings::serialize,
  4. WiFiSettings::deserialize,
  5. this,
  6. server,
  7. WIFI_SETTINGS_SERVICE_PATH,
  8. securityManager),
  9. _fsPersistence(WiFiSettings::serialize, WiFiSettings::deserialize, this, fs, WIFI_SETTINGS_FILE),
  10. _lastConnectionAttempt(0) {
  11. // We want the device to come up in opmode=0 (WIFI_OFF), when erasing the flash this is not the default.
  12. // If needed, we save opmode=0 before disabling persistence so the device boots with WiFi disabled in the future.
  13. if (WiFi.getMode() != WIFI_OFF) {
  14. WiFi.mode(WIFI_OFF);
  15. }
  16. // Disable WiFi config persistance and auto reconnect
  17. WiFi.persistent(false);
  18. WiFi.setAutoReconnect(false);
  19. #ifdef ESP32
  20. // Init the wifi driver on ESP32
  21. WiFi.mode(WIFI_MODE_MAX);
  22. WiFi.mode(WIFI_MODE_NULL);
  23. WiFi.onEvent(
  24. std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2),
  25. WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
  26. WiFi.onEvent(std::bind(&WiFiSettingsService::onStationModeStop, this, std::placeholders::_1, std::placeholders::_2),
  27. WiFiEvent_t::SYSTEM_EVENT_STA_STOP);
  28. #elif defined(ESP8266)
  29. _onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(
  30. std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1));
  31. #endif
  32. addUpdateHandler([&](const String& originId) { reconfigureWiFiConnection(); }, false);
  33. }
  34. void WiFiSettingsService::begin() {
  35. _fsPersistence.readFromFS();
  36. reconfigureWiFiConnection();
  37. }
  38. void WiFiSettingsService::reconfigureWiFiConnection() {
  39. // reset last connection attempt to force loop to reconnect immediately
  40. _lastConnectionAttempt = 0;
  41. // disconnect and de-configure wifi
  42. #ifdef ESP32
  43. if (WiFi.disconnect(true)) {
  44. _stopping = true;
  45. }
  46. #elif defined(ESP8266)
  47. WiFi.disconnect(true);
  48. #endif
  49. }
  50. void WiFiSettingsService::loop() {
  51. unsigned long currentMillis = millis();
  52. if (!_lastConnectionAttempt || (unsigned long)(currentMillis - _lastConnectionAttempt) >= WIFI_RECONNECTION_DELAY) {
  53. _lastConnectionAttempt = currentMillis;
  54. manageSTA();
  55. }
  56. }
  57. void WiFiSettingsService::manageSTA() {
  58. // Abort if already connected, or if we have no SSID
  59. if (WiFi.isConnected() || _state.ssid.length() == 0) {
  60. return;
  61. }
  62. // Connect or reconnect as required
  63. if ((WiFi.getMode() & WIFI_STA) == 0) {
  64. Serial.println(F("Connecting to WiFi."));
  65. if (_state.staticIPConfig) {
  66. // configure for static IP
  67. WiFi.config(_state.localIP, _state.gatewayIP, _state.subnetMask, _state.dnsIP1, _state.dnsIP2);
  68. } else {
  69. // configure for DHCP
  70. #ifdef ESP32
  71. WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
  72. WiFi.setHostname(_state.hostname.c_str());
  73. #elif defined(ESP8266)
  74. WiFi.config(INADDR_ANY, INADDR_ANY, INADDR_ANY);
  75. WiFi.hostname(_state.hostname);
  76. #endif
  77. }
  78. // attempt to connect to the network
  79. WiFi.begin(_state.ssid.c_str(), _state.password.c_str());
  80. }
  81. }
  82. #ifdef ESP32
  83. void WiFiSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
  84. WiFi.disconnect(true);
  85. }
  86. void WiFiSettingsService::onStationModeStop(WiFiEvent_t event, WiFiEventInfo_t info) {
  87. if (_stopping) {
  88. _lastConnectionAttempt = 0;
  89. _stopping = false;
  90. }
  91. }
  92. #elif defined(ESP8266)
  93. void WiFiSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) {
  94. WiFi.disconnect(true);
  95. }
  96. #endif