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.

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