From 14820616a667d0a21e85a0e3e1e3c66fae09abeb Mon Sep 17 00:00:00 2001 From: Rick Watson Date: Sun, 11 Nov 2018 17:47:44 +0000 Subject: [PATCH] Add code and required to support differences between eps32 and esp8266 environments --- platformio.ini | 1 + src/NTPSettingsService.cpp | 19 ++++++++++++++++++- src/NTPSettingsService.h | 11 ++++++++--- src/OTASettingsService.cpp | 27 ++++++++++++++++++--------- src/OTASettingsService.h | 16 ++++++++++++++-- src/WiFiScanner.cpp | 5 ++++- src/WiFiSettingsService.cpp | 18 +++++++++++------- src/WiFiStatus.cpp | 29 ++++++++++++++++++++++++++--- src/WiFiStatus.h | 10 ++++++++-- 9 files changed, 108 insertions(+), 28 deletions(-) diff --git a/platformio.ini b/platformio.ini index 17c79e5..b9b8bbc 100644 --- a/platformio.ini +++ b/platformio.ini @@ -22,3 +22,4 @@ lib_deps = https://github.com/gmag11/NtpClient https://github.com/bblanchon/ArduinoJson https://github.com/me-no-dev/ESPAsyncWebServer + https://github.com/me-no-dev/AsyncTCP diff --git a/src/NTPSettingsService.cpp b/src/NTPSettingsService.cpp index db908b1..99962a2 100644 --- a/src/NTPSettingsService.cpp +++ b/src/NTPSettingsService.cpp @@ -1,8 +1,14 @@ #include NTPSettingsService::NTPSettingsService(AsyncWebServer* server, FS* fs) : SettingsService(server, fs, NTP_SETTINGS_SERVICE_PATH, NTP_SETTINGS_FILE) { + +#if defined(ESP8266) _onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1)); _onStationModeGotIPHandler = WiFi.onStationModeGotIP(std::bind(&NTPSettingsService::onStationModeGotIP, this, std::placeholders::_1)); +#elif defined(ESP_PLATFORM) + WiFi.onEvent(std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_AP_STADISCONNECTED); + WiFi.onEvent(std::bind(&NTPSettingsService::onStationModeGotIP, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP); +#endif NTP.onNTPSyncEvent ([this](NTPSyncEvent_t ntpEvent) { _ntpEvent = ntpEvent; @@ -56,6 +62,7 @@ void NTPSettingsService::onConfigUpdated() { _reconfigureNTP = true; } +#if defined(ESP8266) void NTPSettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP& event) { Serial.printf("Got IP address, starting NTP Synchronization\n"); _reconfigureNTP = true; @@ -63,11 +70,21 @@ void NTPSettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP& eve void NTPSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) { Serial.printf("WiFi connection dropped, stopping NTP.\n"); + _reconfigureNTP = false; + NTP.stop(); +} +#elif defined(ESP_PLATFORM) +void NTPSettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) { + Serial.printf("Got IP address, starting NTP Synchronization\n"); + _reconfigureNTP = true; +} - // stop NTP synchronization, ensuring no re-configuration can take place +void NTPSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) { + Serial.printf("WiFi connection dropped, stopping NTP.\n"); _reconfigureNTP = false; NTP.stop(); } +#endif void NTPSettingsService::configureNTP() { Serial.println("Configuring NTP..."); diff --git a/src/NTPSettingsService.h b/src/NTPSettingsService.h index b6d14f0..c3624e7 100644 --- a/src/NTPSettingsService.h +++ b/src/NTPSettingsService.h @@ -34,9 +34,6 @@ class NTPSettingsService : public SettingsService { private: - WiFiEventHandler _onStationModeDisconnectedHandler; - WiFiEventHandler _onStationModeGotIPHandler; - String _server; int _interval; @@ -44,8 +41,16 @@ class NTPSettingsService : public SettingsService { bool _syncEventTriggered = false; NTPSyncEvent_t _ntpEvent; +#if defined(ESP8266) + WiFiEventHandler _onStationModeDisconnectedHandler; + WiFiEventHandler _onStationModeGotIPHandler; + void onStationModeGotIP(const WiFiEventStationModeGotIP& event); void onStationModeDisconnected(const WiFiEventStationModeDisconnected& event); +#elif defined(ESP_PLATFORM) + void onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info); + void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info); +#endif void configureNTP(); void processSyncEvent(NTPSyncEvent_t ntpEvent); diff --git a/src/OTASettingsService.cpp b/src/OTASettingsService.cpp index 403ca38..64e98c2 100644 --- a/src/OTASettingsService.cpp +++ b/src/OTASettingsService.cpp @@ -1,17 +1,15 @@ #include -OTASettingsService::OTASettingsService(AsyncWebServer* server, FS* fs) : SettingsService(server, fs, OTA_SETTINGS_SERVICE_PATH, OTA_SETTINGS_FILE) {} +OTASettingsService::OTASettingsService(AsyncWebServer* server, FS* fs) : SettingsService(server, fs, OTA_SETTINGS_SERVICE_PATH, OTA_SETTINGS_FILE) { +#if defined(ESP8266) + _onStationModeGotIPHandler = WiFi.onStationModeGotIP(std::bind(&OTASettingsService::onStationModeGotIP, this, std::placeholders::_1)); +#elif defined(ESP_PLATFORM) + WiFi.onEvent(std::bind(&OTASettingsService::onStationModeGotIP, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP); +#endif +} OTASettingsService::~OTASettingsService() {} -void OTASettingsService::begin() { - // load settings - SettingsService::begin(); - - // configure arduino OTA - configureArduinoOTA(); -} - void OTASettingsService::loop() { if (_enabled && _arduinoOTA){ _arduinoOTA->handle(); @@ -45,6 +43,7 @@ void OTASettingsService::configureArduinoOTA() { _arduinoOTA = NULL; } if (_enabled) { + Serial.println("Starting OTA Update Service"); _arduinoOTA = new ArduinoOTAClass; _arduinoOTA->setPort(_port); _arduinoOTA->setPassword(_password.c_str()); @@ -68,3 +67,13 @@ void OTASettingsService::configureArduinoOTA() { _arduinoOTA->begin(); } } + +#if defined(ESP8266) +void OTASettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP& event) { + configureArduinoOTA(); +} +#elif defined(ESP_PLATFORM) +void OTASettingsService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) { + configureArduinoOTA(); +} +#endif diff --git a/src/OTASettingsService.h b/src/OTASettingsService.h index 671f2a7..32e76ed 100644 --- a/src/OTASettingsService.h +++ b/src/OTASettingsService.h @@ -2,7 +2,13 @@ #define OTASettingsService_h #include -#include + +#if defined(ESP8266) + #include +#elif defined(ESP_PLATFORM) + #include +#endif + #include #include @@ -20,7 +26,6 @@ class OTASettingsService : public SettingsService { OTASettingsService(AsyncWebServer* server, FS* fs); ~OTASettingsService(); - void begin(); void loop(); protected: @@ -38,6 +43,13 @@ class OTASettingsService : public SettingsService { void configureArduinoOTA(); +#if defined(ESP8266) + WiFiEventHandler _onStationModeGotIPHandler; + void onStationModeGotIP(const WiFiEventStationModeGotIP& event); +#elif defined(ESP_PLATFORM) + void onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info); +#endif + }; #endif // end NTPSettingsService_h diff --git a/src/WiFiScanner.cpp b/src/WiFiScanner.cpp index 7312fb7..f7f0309 100644 --- a/src/WiFiScanner.cpp +++ b/src/WiFiScanner.cpp @@ -25,8 +25,11 @@ void WiFiScanner::listNetworks(AsyncWebServerRequest *request) { network["ssid"] = WiFi.SSID(i); network["bssid"] = WiFi.BSSIDstr(i); network["channel"] = WiFi.channel(i); +#if defined(ESP8266) network["encryption_type"] = WiFi.encryptionType(i); - network["hidden"] = WiFi.isHidden(i); +#elif defined(ESP_PLATFORM) + network["encryption_type"] = (uint8_t) WiFi.encryptionType(i); +#endif } response->setLength(); request->send(response); diff --git a/src/WiFiSettingsService.cpp b/src/WiFiSettingsService.cpp index a940f52..c9d3b9b 100644 --- a/src/WiFiSettingsService.cpp +++ b/src/WiFiSettingsService.cpp @@ -1,7 +1,6 @@ #include -WiFiSettingsService::WiFiSettingsService(AsyncWebServer* server, FS* fs) : SettingsService(server, fs, WIFI_SETTINGS_SERVICE_PATH, WIFI_SETTINGS_FILE) { -} +WiFiSettingsService::WiFiSettingsService(AsyncWebServer* server, FS* fs) : SettingsService(server, fs, WIFI_SETTINGS_SERVICE_PATH, WIFI_SETTINGS_FILE) {} WiFiSettingsService::~WiFiSettingsService() {} @@ -24,15 +23,15 @@ void WiFiSettingsService::readFromJsonObject(JsonObject& root){ readIP(root, "dns_ip_2", _dnsIP2); // Swap around the dns servers if 2 is populated but 1 is not - if (_dnsIP1 == 0U && _dnsIP2 != 0U){ + if (_dnsIP1 == INADDR_NONE && _dnsIP2 != INADDR_NONE){ _dnsIP1 = _dnsIP2; - _dnsIP2 = 0U; + _dnsIP2 = INADDR_NONE; } // 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 - if (_staticIPConfig && (_localIP == 0U || _gatewayIP == 0U || _subnetMask == 0U)){ + if (_staticIPConfig && (_localIP == INADDR_NONE || _gatewayIP == INADDR_NONE || _subnetMask == INADDR_NONE)){ _staticIPConfig = false; } } @@ -68,18 +67,23 @@ void WiFiSettingsService::reconfigureWiFiConnection() { } // connect to the network +#if defined(ESP8266) WiFi.hostname(_hostname); +#elif defined(ESP_PLATFORM) + WiFi.setHostname(_hostname.c_str()); +#endif + WiFi.begin(_ssid.c_str(), _password.c_str()); } void WiFiSettingsService::readIP(JsonObject& root, String key, IPAddress& _ip){ if (!root[key] || !_ip.fromString(root[key].as())){ - _ip = 0U; + _ip = INADDR_NONE; } } void WiFiSettingsService::writeIP(JsonObject& root, String key, IPAddress& _ip){ - if (_ip != 0U){ + if (_ip != INADDR_NONE){ root[key] = _ip.toString(); } } diff --git a/src/WiFiStatus.cpp b/src/WiFiStatus.cpp index c8ef066..9e8574a 100644 --- a/src/WiFiStatus.cpp +++ b/src/WiFiStatus.cpp @@ -2,12 +2,18 @@ WiFiStatus::WiFiStatus(AsyncWebServer *server) : _server(server) { _server->on(WIFI_STATUS_SERVICE_PATH, HTTP_GET, std::bind(&WiFiStatus::wifiStatus, this, std::placeholders::_1)); - +#if defined(ESP8266) _onStationModeConnectedHandler = WiFi.onStationModeConnected(onStationModeConnected); _onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(onStationModeDisconnected); _onStationModeGotIPHandler = WiFi.onStationModeGotIP(onStationModeGotIP); +#elif defined(ESP_PLATFORM) + WiFi.onEvent(onStationModeConnected, WiFiEvent_t::SYSTEM_EVENT_STA_CONNECTED); + WiFi.onEvent(onStationModeDisconnected, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED); + WiFi.onEvent(onStationModeGotIP, WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP); +#endif } +#if defined(ESP8266) void WiFiStatus::onStationModeConnected(const WiFiEventStationModeConnected& event) { Serial.print("WiFi Connected. SSID="); Serial.println(event.ssid); @@ -24,6 +30,23 @@ void WiFiStatus::onStationModeGotIP(const WiFiEventStationModeGotIP& event) { Serial.print(", hostName="); Serial.println(WiFi.hostname()); } +#elif defined(ESP_PLATFORM) +void WiFiStatus::onStationModeConnected(WiFiEvent_t event, WiFiEventInfo_t info) { + Serial.print("WiFi Connected."); +} + +void WiFiStatus::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) { + Serial.print("WiFi Disconnected. Reason code="); + Serial.println(info.disconnected.reason); +} + +void WiFiStatus::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) { + Serial.print("WiFi Got IP. localIP="); + Serial.print(WiFi.localIP().toString()); + Serial.print(", hostName="); + Serial.println(WiFi.getHostname()); +} +#endif void WiFiStatus::wifiStatus(AsyncWebServerRequest *request) { AsyncJsonResponse * response = new AsyncJsonResponse(); @@ -40,10 +63,10 @@ void WiFiStatus::wifiStatus(AsyncWebServerRequest *request) { root["gateway_ip"] = WiFi.gatewayIP().toString(); IPAddress dnsIP1 = WiFi.dnsIP(0); IPAddress dnsIP2 = WiFi.dnsIP(1); - if (dnsIP1 != 0U){ + if (dnsIP1 != INADDR_NONE){ root["dns_ip_1"] = dnsIP1.toString(); } - if (dnsIP2 != 0U){ + if (dnsIP2 != INADDR_NONE){ root["dns_ip_2"] = dnsIP2.toString(); } } diff --git a/src/WiFiStatus.h b/src/WiFiStatus.h index d600abf..194f996 100644 --- a/src/WiFiStatus.h +++ b/src/WiFiStatus.h @@ -26,15 +26,21 @@ class WiFiStatus { AsyncWebServer* _server; +#if defined(ESP8266) // handler refrences for logging important WiFi events over serial WiFiEventHandler _onStationModeConnectedHandler; WiFiEventHandler _onStationModeDisconnectedHandler; - WiFiEventHandler _onStationModeGotIPHandler; - + WiFiEventHandler _onStationModeGotIPHandler; // static functions for logging WiFi events to the UART static void onStationModeConnected(const WiFiEventStationModeConnected& event); static void onStationModeDisconnected(const WiFiEventStationModeDisconnected& event); static void onStationModeGotIP(const WiFiEventStationModeGotIP& event); +#elif defined(ESP_PLATFORM) + // static functions for logging WiFi events to the UART + static void onStationModeConnected(WiFiEvent_t event, WiFiEventInfo_t info); + static void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info); + static void onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info); +#endif void wifiStatus(AsyncWebServerRequest *request);