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.

100 lines
3.5 KiB

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