2018-02-26 00:11:31 +00:00
|
|
|
#include <WiFiSettingsService.h>
|
|
|
|
|
2018-11-11 17:47:44 +00:00
|
|
|
WiFiSettingsService::WiFiSettingsService(AsyncWebServer* server, FS* fs) : SettingsService(server, fs, WIFI_SETTINGS_SERVICE_PATH, WIFI_SETTINGS_FILE) {}
|
2018-02-26 00:11:31 +00:00
|
|
|
|
|
|
|
WiFiSettingsService::~WiFiSettingsService() {}
|
|
|
|
|
|
|
|
void WiFiSettingsService::begin() {
|
|
|
|
SettingsService::begin();
|
|
|
|
reconfigureWiFiConnection();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WiFiSettingsService::readFromJsonObject(JsonObject& root){
|
|
|
|
_ssid = root["ssid"] | "";
|
|
|
|
_password = root["password"] | "";
|
|
|
|
_hostname = root["hostname"] | "";
|
|
|
|
_staticIPConfig = root["static_ip_config"] | false;
|
|
|
|
|
|
|
|
// extended settings
|
|
|
|
readIP(root, "local_ip", _localIP);
|
|
|
|
readIP(root, "gateway_ip", _gatewayIP);
|
|
|
|
readIP(root, "subnet_mask", _subnetMask);
|
|
|
|
readIP(root, "dns_ip_1", _dnsIP1);
|
|
|
|
readIP(root, "dns_ip_2", _dnsIP2);
|
|
|
|
|
|
|
|
// Swap around the dns servers if 2 is populated but 1 is not
|
2018-11-11 17:47:44 +00:00
|
|
|
if (_dnsIP1 == INADDR_NONE && _dnsIP2 != INADDR_NONE){
|
2018-02-26 00:11:31 +00:00
|
|
|
_dnsIP1 = _dnsIP2;
|
2018-11-11 17:47:44 +00:00
|
|
|
_dnsIP2 = INADDR_NONE;
|
2018-02-26 00:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Turning off static ip config if we don't meet the minimum requirements
|
|
|
|
// of ipAddress, gateway and subnet. This may change to static ip only
|
|
|
|
// as sensible defaults can be assumed for gateway and subnet
|
2018-11-11 17:47:44 +00:00
|
|
|
if (_staticIPConfig && (_localIP == INADDR_NONE || _gatewayIP == INADDR_NONE || _subnetMask == INADDR_NONE)){
|
2018-02-26 00:11:31 +00:00
|
|
|
_staticIPConfig = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WiFiSettingsService::writeToJsonObject(JsonObject& root){
|
|
|
|
// connection settings
|
|
|
|
root["ssid"] = _ssid;
|
|
|
|
root["password"] = _password;
|
|
|
|
root["hostname"] = _hostname;
|
|
|
|
root["static_ip_config"] = _staticIPConfig;
|
|
|
|
|
|
|
|
// extended settings
|
|
|
|
writeIP(root, "local_ip", _localIP);
|
|
|
|
writeIP(root, "gateway_ip", _gatewayIP);
|
|
|
|
writeIP(root, "subnet_mask", _subnetMask);
|
|
|
|
writeIP(root, "dns_ip_1", _dnsIP1);
|
|
|
|
writeIP(root, "dns_ip_2", _dnsIP2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WiFiSettingsService::onConfigUpdated() {
|
|
|
|
reconfigureWiFiConnection();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WiFiSettingsService::reconfigureWiFiConnection() {
|
|
|
|
Serial.println("Reconfiguring WiFi...");
|
|
|
|
|
|
|
|
// disconnect and de-configure wifi and software access point
|
|
|
|
WiFi.disconnect(true);
|
|
|
|
|
|
|
|
// configure static ip config for station mode (if set)
|
|
|
|
if (_staticIPConfig) {
|
|
|
|
WiFi.config(_localIP, _gatewayIP, _subnetMask, _dnsIP1, _dnsIP2);
|
2019-04-28 07:19:33 +00:00
|
|
|
} else { // else setting dynamic ip config and hostname
|
|
|
|
#if defined(ESP8266)
|
|
|
|
WiFi.config(INADDR_ANY, INADDR_ANY, INADDR_ANY);
|
|
|
|
WiFi.hostname(_hostname);
|
2018-11-11 17:47:44 +00:00
|
|
|
#elif defined(ESP_PLATFORM)
|
2019-04-28 07:19:33 +00:00
|
|
|
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
|
|
|
|
WiFi.setHostname(_hostname.c_str());
|
2018-11-11 17:47:44 +00:00
|
|
|
#endif
|
2019-04-28 07:19:33 +00:00
|
|
|
}
|
2018-11-11 17:47:44 +00:00
|
|
|
|
2019-04-28 07:19:33 +00:00
|
|
|
// connect to the network
|
2018-02-26 00:11:31 +00:00
|
|
|
WiFi.begin(_ssid.c_str(), _password.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void WiFiSettingsService::readIP(JsonObject& root, String key, IPAddress& _ip){
|
2019-04-27 03:07:40 +00:00
|
|
|
if ( !root[key].is<String>() || !_ip.fromString(root[key].as<String>())){
|
2018-11-11 17:47:44 +00:00
|
|
|
_ip = INADDR_NONE;
|
2018-02-26 00:11:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WiFiSettingsService::writeIP(JsonObject& root, String key, IPAddress& _ip){
|
2018-11-11 17:47:44 +00:00
|
|
|
if (_ip != INADDR_NONE){
|
2018-02-26 00:11:31 +00:00
|
|
|
root[key] = _ip.toString();
|
|
|
|
}
|
|
|
|
}
|