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.

134 lines
4.4 KiB

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