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.

140 lines
4.7 KiB

  1. #include <WiFiSettingsService.h>
  2. WiFiSettingsService::WiFiSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, WIFI_SETTINGS_SERVICE_PATH, WIFI_SETTINGS_FILE) {
  3. // We want the device to come up in opmode=0 (WIFI_OFF), when erasing the flash this is not the default.
  4. // If needed, we save opmode=0 before disabling persistence so the device boots with WiFi disabled in the future.
  5. if (WiFi.getMode() != WIFI_OFF) {
  6. WiFi.mode(WIFI_OFF);
  7. }
  8. // Disable WiFi config persistance and auto reconnect
  9. WiFi.persistent(false);
  10. WiFi.setAutoReconnect(false);
  11. #if defined(ESP8266)
  12. _onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1));
  13. #elif defined(ESP_PLATFORM)
  14. // Init the wifi driver on ESP32
  15. WiFi.mode(WIFI_MODE_MAX);
  16. WiFi.mode(WIFI_MODE_NULL);
  17. WiFi.onEvent(std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
  18. #endif
  19. }
  20. WiFiSettingsService::~WiFiSettingsService() {}
  21. void WiFiSettingsService::begin() {
  22. SettingsService::begin();
  23. reconfigureWiFiConnection();
  24. }
  25. void WiFiSettingsService::readFromJsonObject(JsonObject& root){
  26. _ssid = root["ssid"] | "";
  27. _password = root["password"] | "";
  28. _hostname = root["hostname"] | "";
  29. _staticIPConfig = root["static_ip_config"] | false;
  30. // extended settings
  31. readIP(root, "local_ip", _localIP);
  32. readIP(root, "gateway_ip", _gatewayIP);
  33. readIP(root, "subnet_mask", _subnetMask);
  34. readIP(root, "dns_ip_1", _dnsIP1);
  35. readIP(root, "dns_ip_2", _dnsIP2);
  36. // Swap around the dns servers if 2 is populated but 1 is not
  37. if (_dnsIP1 == INADDR_NONE && _dnsIP2 != INADDR_NONE){
  38. _dnsIP1 = _dnsIP2;
  39. _dnsIP2 = INADDR_NONE;
  40. }
  41. // Turning off static ip config if we don't meet the minimum requirements
  42. // of ipAddress, gateway and subnet. This may change to static ip only
  43. // as sensible defaults can be assumed for gateway and subnet
  44. if (_staticIPConfig && (_localIP == INADDR_NONE || _gatewayIP == INADDR_NONE || _subnetMask == INADDR_NONE)){
  45. _staticIPConfig = false;
  46. }
  47. }
  48. void WiFiSettingsService::writeToJsonObject(JsonObject& root){
  49. // connection settings
  50. root["ssid"] = _ssid;
  51. root["password"] = _password;
  52. root["hostname"] = _hostname;
  53. root["static_ip_config"] = _staticIPConfig;
  54. // extended settings
  55. writeIP(root, "local_ip", _localIP);
  56. writeIP(root, "gateway_ip", _gatewayIP);
  57. writeIP(root, "subnet_mask", _subnetMask);
  58. writeIP(root, "dns_ip_1", _dnsIP1);
  59. writeIP(root, "dns_ip_2", _dnsIP2);
  60. }
  61. void WiFiSettingsService::onConfigUpdated() {
  62. reconfigureWiFiConnection();
  63. }
  64. void WiFiSettingsService::reconfigureWiFiConnection() {
  65. // disconnect and de-configure wifi
  66. WiFi.disconnect(true);
  67. // reset last connection attempt to force loop to reconnect immediately
  68. _lastConnectionAttempt = 0;
  69. }
  70. void WiFiSettingsService::readIP(JsonObject& root, String key, IPAddress& _ip){
  71. if (!root[key].is<String>() || !_ip.fromString(root[key].as<String>())){
  72. _ip = INADDR_NONE;
  73. }
  74. }
  75. void WiFiSettingsService::writeIP(JsonObject& root, String key, IPAddress& _ip){
  76. if (_ip != INADDR_NONE){
  77. root[key] = _ip.toString();
  78. }
  79. }
  80. void WiFiSettingsService::loop() {
  81. unsigned long currentMillis = millis();
  82. if (!_lastConnectionAttempt || (unsigned long)(currentMillis - _lastConnectionAttempt) >= WIFI_RECONNECTION_DELAY) {
  83. _lastConnectionAttempt = currentMillis;
  84. manageSTA();
  85. }
  86. }
  87. void WiFiSettingsService::manageSTA() {
  88. // Abort if already connected, or if we have no SSID
  89. if (WiFi.isConnected() || _ssid.length() == 0) {
  90. return;
  91. }
  92. // Connect or reconnect as required
  93. if ((WiFi.getMode() & WIFI_STA) == 0) {
  94. Serial.println("Connecting to WiFi.");
  95. if (_staticIPConfig) {
  96. // configure for static IP
  97. WiFi.config(_localIP, _gatewayIP, _subnetMask, _dnsIP1, _dnsIP2);
  98. } else {
  99. // configure for DHCP
  100. #if defined(ESP8266)
  101. WiFi.config(INADDR_ANY, INADDR_ANY, INADDR_ANY);
  102. WiFi.hostname(_hostname);
  103. #elif defined(ESP_PLATFORM)
  104. WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
  105. WiFi.setHostname(_hostname.c_str());
  106. #endif
  107. }
  108. // attempt to connect to the network
  109. WiFi.begin(_ssid.c_str(), _password.c_str());
  110. }
  111. }
  112. #if defined(ESP8266)
  113. void WiFiSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) {
  114. WiFi.disconnect(true);
  115. }
  116. #elif defined(ESP_PLATFORM)
  117. void WiFiSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
  118. WiFi.disconnect(true);
  119. }
  120. #endif