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.

110 lines
3.5 KiB

  1. #ifndef WiFiSettingsService_h
  2. #define WiFiSettingsService_h
  3. #include <StatefulService.h>
  4. #include <FSPersistence.h>
  5. #include <HttpEndpoint.h>
  6. #include <JsonUtils.h>
  7. #define WIFI_SETTINGS_FILE "/config/wifiSettings.json"
  8. #define WIFI_SETTINGS_SERVICE_PATH "/rest/wifiSettings"
  9. #define WIFI_RECONNECTION_DELAY 1000 * 30
  10. #ifndef FACTORY_WIFI_SSID
  11. #define FACTORY_WIFI_SSID ""
  12. #endif
  13. #ifndef FACTORY_WIFI_PASSWORD
  14. #define FACTORY_WIFI_PASSWORD ""
  15. #endif
  16. #ifndef FACTORY_WIFI_HOSTNAME
  17. #define FACTORY_WIFI_HOSTNAME ""
  18. #endif
  19. class WiFiSettings {
  20. public:
  21. // core wifi configuration
  22. String ssid;
  23. String password;
  24. String hostname;
  25. bool staticIPConfig;
  26. // optional configuration for static IP address
  27. IPAddress localIP;
  28. IPAddress gatewayIP;
  29. IPAddress subnetMask;
  30. IPAddress dnsIP1;
  31. IPAddress dnsIP2;
  32. static void read(WiFiSettings& settings, JsonObject& root) {
  33. // connection settings
  34. root["ssid"] = settings.ssid;
  35. root["password"] = settings.password;
  36. root["hostname"] = settings.hostname;
  37. root["static_ip_config"] = settings.staticIPConfig;
  38. // extended settings
  39. JsonUtils::writeIP(root, "local_ip", settings.localIP);
  40. JsonUtils::writeIP(root, "gateway_ip", settings.gatewayIP);
  41. JsonUtils::writeIP(root, "subnet_mask", settings.subnetMask);
  42. JsonUtils::writeIP(root, "dns_ip_1", settings.dnsIP1);
  43. JsonUtils::writeIP(root, "dns_ip_2", settings.dnsIP2);
  44. }
  45. static StateUpdateResult update(JsonObject& root, WiFiSettings& settings) {
  46. settings.ssid = root["ssid"] | FACTORY_WIFI_SSID;
  47. settings.password = root["password"] | FACTORY_WIFI_PASSWORD;
  48. settings.hostname = root["hostname"] | FACTORY_WIFI_HOSTNAME;
  49. settings.staticIPConfig = root["static_ip_config"] | false;
  50. // extended settings
  51. JsonUtils::readIP(root, "local_ip", settings.localIP);
  52. JsonUtils::readIP(root, "gateway_ip", settings.gatewayIP);
  53. JsonUtils::readIP(root, "subnet_mask", settings.subnetMask);
  54. JsonUtils::readIP(root, "dns_ip_1", settings.dnsIP1);
  55. JsonUtils::readIP(root, "dns_ip_2", settings.dnsIP2);
  56. // Swap around the dns servers if 2 is populated but 1 is not
  57. if (settings.dnsIP1 == INADDR_NONE && settings.dnsIP2 != INADDR_NONE) {
  58. settings.dnsIP1 = settings.dnsIP2;
  59. settings.dnsIP2 = INADDR_NONE;
  60. }
  61. // Turning off static ip config if we don't meet the minimum requirements
  62. // of ipAddress, gateway and subnet. This may change to static ip only
  63. // as sensible defaults can be assumed for gateway and subnet
  64. if (settings.staticIPConfig &&
  65. (settings.localIP == INADDR_NONE || settings.gatewayIP == INADDR_NONE || settings.subnetMask == INADDR_NONE)) {
  66. settings.staticIPConfig = false;
  67. }
  68. return StateUpdateResult::CHANGED;
  69. }
  70. };
  71. class WiFiSettingsService : public StatefulService<WiFiSettings> {
  72. public:
  73. WiFiSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
  74. void begin();
  75. void loop();
  76. private:
  77. HttpEndpoint<WiFiSettings> _httpEndpoint;
  78. FSPersistence<WiFiSettings> _fsPersistence;
  79. unsigned long _lastConnectionAttempt;
  80. #ifdef ESP32
  81. bool _stopping;
  82. void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
  83. void onStationModeStop(WiFiEvent_t event, WiFiEventInfo_t info);
  84. #elif defined(ESP8266)
  85. WiFiEventHandler _onStationModeDisconnectedHandler;
  86. void onStationModeDisconnected(const WiFiEventStationModeDisconnected& event);
  87. #endif
  88. void reconfigureWiFiConnection();
  89. void manageSTA();
  90. };
  91. #endif // end WiFiSettingsService_h