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.

85 lines
2.6 KiB

  1. #include <WiFiSettingsService.h>
  2. WiFiSettingsService::WiFiSettingsService(AsyncWebServer* server, FS* fs) : SettingsService(server, fs, WIFI_SETTINGS_SERVICE_PATH, WIFI_SETTINGS_FILE) {
  3. }
  4. WiFiSettingsService::~WiFiSettingsService() {}
  5. void WiFiSettingsService::begin() {
  6. SettingsService::begin();
  7. reconfigureWiFiConnection();
  8. }
  9. void WiFiSettingsService::readFromJsonObject(JsonObject& root){
  10. _ssid = root["ssid"] | "";
  11. _password = root["password"] | "";
  12. _hostname = root["hostname"] | "";
  13. _staticIPConfig = root["static_ip_config"] | false;
  14. // extended settings
  15. readIP(root, "local_ip", _localIP);
  16. readIP(root, "gateway_ip", _gatewayIP);
  17. readIP(root, "subnet_mask", _subnetMask);
  18. readIP(root, "dns_ip_1", _dnsIP1);
  19. readIP(root, "dns_ip_2", _dnsIP2);
  20. // Swap around the dns servers if 2 is populated but 1 is not
  21. if (_dnsIP1 == 0U && _dnsIP2 != 0U){
  22. _dnsIP1 = _dnsIP2;
  23. _dnsIP2 = 0U;
  24. }
  25. // Turning off static ip config if we don't meet the minimum requirements
  26. // of ipAddress, gateway and subnet. This may change to static ip only
  27. // as sensible defaults can be assumed for gateway and subnet
  28. if (_staticIPConfig && (_localIP == 0U || _gatewayIP == 0U || _subnetMask == 0U)){
  29. _staticIPConfig = false;
  30. }
  31. }
  32. void WiFiSettingsService::writeToJsonObject(JsonObject& root){
  33. // connection settings
  34. root["ssid"] = _ssid;
  35. root["password"] = _password;
  36. root["hostname"] = _hostname;
  37. root["static_ip_config"] = _staticIPConfig;
  38. // extended settings
  39. writeIP(root, "local_ip", _localIP);
  40. writeIP(root, "gateway_ip", _gatewayIP);
  41. writeIP(root, "subnet_mask", _subnetMask);
  42. writeIP(root, "dns_ip_1", _dnsIP1);
  43. writeIP(root, "dns_ip_2", _dnsIP2);
  44. }
  45. void WiFiSettingsService::onConfigUpdated() {
  46. reconfigureWiFiConnection();
  47. }
  48. void WiFiSettingsService::reconfigureWiFiConnection() {
  49. Serial.println("Reconfiguring WiFi...");
  50. // disconnect and de-configure wifi and software access point
  51. WiFi.disconnect(true);
  52. // configure static ip config for station mode (if set)
  53. if (_staticIPConfig) {
  54. WiFi.config(_localIP, _gatewayIP, _subnetMask, _dnsIP1, _dnsIP2);
  55. }
  56. // connect to the network
  57. WiFi.hostname(_hostname);
  58. WiFi.begin(_ssid.c_str(), _password.c_str());
  59. }
  60. void WiFiSettingsService::readIP(JsonObject& root, String key, IPAddress& _ip){
  61. if (!root[key] || !_ip.fromString(root[key].as<String>())){
  62. _ip = 0U;
  63. }
  64. }
  65. void WiFiSettingsService::writeIP(JsonObject& root, String key, IPAddress& _ip){
  66. if (_ip != 0U){
  67. root[key] = _ip.toString();
  68. }
  69. }