Add code and required to support differences between eps32 and esp8266 environments

This commit is contained in:
Rick Watson 2018-11-11 17:47:44 +00:00
parent 26a03b5ccf
commit 14820616a6
9 changed files with 109 additions and 29 deletions

View File

@ -22,3 +22,4 @@ lib_deps =
https://github.com/gmag11/NtpClient https://github.com/gmag11/NtpClient
https://github.com/bblanchon/ArduinoJson https://github.com/bblanchon/ArduinoJson
https://github.com/me-no-dev/ESPAsyncWebServer https://github.com/me-no-dev/ESPAsyncWebServer
https://github.com/me-no-dev/AsyncTCP

View File

@ -1,8 +1,14 @@
#include <NTPSettingsService.h> #include <NTPSettingsService.h>
NTPSettingsService::NTPSettingsService(AsyncWebServer* server, FS* fs) : SettingsService(server, fs, NTP_SETTINGS_SERVICE_PATH, NTP_SETTINGS_FILE) { 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)); _onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(std::bind(&NTPSettingsService::onStationModeDisconnected, this, std::placeholders::_1));
_onStationModeGotIPHandler = WiFi.onStationModeGotIP(std::bind(&NTPSettingsService::onStationModeGotIP, 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) { NTP.onNTPSyncEvent ([this](NTPSyncEvent_t ntpEvent) {
_ntpEvent = ntpEvent; _ntpEvent = ntpEvent;
@ -56,6 +62,7 @@ void NTPSettingsService::onConfigUpdated() {
_reconfigureNTP = true; _reconfigureNTP = true;
} }
#if defined(ESP8266)
void NTPSettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP& event) { void NTPSettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP& event) {
Serial.printf("Got IP address, starting NTP Synchronization\n"); Serial.printf("Got IP address, starting NTP Synchronization\n");
_reconfigureNTP = true; _reconfigureNTP = true;
@ -63,11 +70,21 @@ void NTPSettingsService::onStationModeGotIP(const WiFiEventStationModeGotIP& eve
void NTPSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) { void NTPSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) {
Serial.printf("WiFi connection dropped, stopping NTP.\n"); Serial.printf("WiFi connection dropped, stopping NTP.\n");
// stop NTP synchronization, ensuring no re-configuration can take place
_reconfigureNTP = false; _reconfigureNTP = false;
NTP.stop(); 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;
}
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() { void NTPSettingsService::configureNTP() {
Serial.println("Configuring NTP..."); Serial.println("Configuring NTP...");

View File

@ -34,9 +34,6 @@ class NTPSettingsService : public SettingsService {
private: private:
WiFiEventHandler _onStationModeDisconnectedHandler;
WiFiEventHandler _onStationModeGotIPHandler;
String _server; String _server;
int _interval; int _interval;
@ -44,8 +41,16 @@ class NTPSettingsService : public SettingsService {
bool _syncEventTriggered = false; bool _syncEventTriggered = false;
NTPSyncEvent_t _ntpEvent; NTPSyncEvent_t _ntpEvent;
#if defined(ESP8266)
WiFiEventHandler _onStationModeDisconnectedHandler;
WiFiEventHandler _onStationModeGotIPHandler;
void onStationModeGotIP(const WiFiEventStationModeGotIP& event); void onStationModeGotIP(const WiFiEventStationModeGotIP& event);
void onStationModeDisconnected(const WiFiEventStationModeDisconnected& 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 configureNTP();
void processSyncEvent(NTPSyncEvent_t ntpEvent); void processSyncEvent(NTPSyncEvent_t ntpEvent);

View File

@ -1,17 +1,15 @@
#include <OTASettingsService.h> #include <OTASettingsService.h>
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() {} OTASettingsService::~OTASettingsService() {}
void OTASettingsService::begin() {
// load settings
SettingsService::begin();
// configure arduino OTA
configureArduinoOTA();
}
void OTASettingsService::loop() { void OTASettingsService::loop() {
if (_enabled && _arduinoOTA){ if (_enabled && _arduinoOTA){
_arduinoOTA->handle(); _arduinoOTA->handle();
@ -45,6 +43,7 @@ void OTASettingsService::configureArduinoOTA() {
_arduinoOTA = NULL; _arduinoOTA = NULL;
} }
if (_enabled) { if (_enabled) {
Serial.println("Starting OTA Update Service");
_arduinoOTA = new ArduinoOTAClass; _arduinoOTA = new ArduinoOTAClass;
_arduinoOTA->setPort(_port); _arduinoOTA->setPort(_port);
_arduinoOTA->setPassword(_password.c_str()); _arduinoOTA->setPassword(_password.c_str());
@ -68,3 +67,13 @@ void OTASettingsService::configureArduinoOTA() {
_arduinoOTA->begin(); _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

View File

@ -2,7 +2,13 @@
#define OTASettingsService_h #define OTASettingsService_h
#include <SettingsService.h> #include <SettingsService.h>
#include <ESP8266mDNS.h>
#if defined(ESP8266)
#include <ESP8266mDNS.h>
#elif defined(ESP_PLATFORM)
#include <ESPmDNS.h>
#endif
#include <WiFiUdp.h> #include <WiFiUdp.h>
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
@ -20,7 +26,6 @@ class OTASettingsService : public SettingsService {
OTASettingsService(AsyncWebServer* server, FS* fs); OTASettingsService(AsyncWebServer* server, FS* fs);
~OTASettingsService(); ~OTASettingsService();
void begin();
void loop(); void loop();
protected: protected:
@ -38,6 +43,13 @@ class OTASettingsService : public SettingsService {
void configureArduinoOTA(); 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 #endif // end NTPSettingsService_h

View File

@ -25,8 +25,11 @@ void WiFiScanner::listNetworks(AsyncWebServerRequest *request) {
network["ssid"] = WiFi.SSID(i); network["ssid"] = WiFi.SSID(i);
network["bssid"] = WiFi.BSSIDstr(i); network["bssid"] = WiFi.BSSIDstr(i);
network["channel"] = WiFi.channel(i); network["channel"] = WiFi.channel(i);
#if defined(ESP8266)
network["encryption_type"] = WiFi.encryptionType(i); 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(); response->setLength();
request->send(response); request->send(response);

View File

@ -1,7 +1,6 @@
#include <WiFiSettingsService.h> #include <WiFiSettingsService.h>
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() {} WiFiSettingsService::~WiFiSettingsService() {}
@ -24,15 +23,15 @@ void WiFiSettingsService::readFromJsonObject(JsonObject& root){
readIP(root, "dns_ip_2", _dnsIP2); readIP(root, "dns_ip_2", _dnsIP2);
// Swap around the dns servers if 2 is populated but 1 is not // 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; _dnsIP1 = _dnsIP2;
_dnsIP2 = 0U; _dnsIP2 = INADDR_NONE;
} }
// Turning off static ip config if we don't meet the minimum requirements // 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 // of ipAddress, gateway and subnet. This may change to static ip only
// as sensible defaults can be assumed for gateway and subnet // 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; _staticIPConfig = false;
} }
} }
@ -68,18 +67,23 @@ void WiFiSettingsService::reconfigureWiFiConnection() {
} }
// connect to the network // connect to the network
#if defined(ESP8266)
WiFi.hostname(_hostname); WiFi.hostname(_hostname);
#elif defined(ESP_PLATFORM)
WiFi.setHostname(_hostname.c_str());
#endif
WiFi.begin(_ssid.c_str(), _password.c_str()); WiFi.begin(_ssid.c_str(), _password.c_str());
} }
void WiFiSettingsService::readIP(JsonObject& root, String key, IPAddress& _ip){ void WiFiSettingsService::readIP(JsonObject& root, String key, IPAddress& _ip){
if (!root[key] || !_ip.fromString(root[key].as<String>())){ if (!root[key] || !_ip.fromString(root[key].as<String>())){
_ip = 0U; _ip = INADDR_NONE;
} }
} }
void WiFiSettingsService::writeIP(JsonObject& root, String key, IPAddress& _ip){ void WiFiSettingsService::writeIP(JsonObject& root, String key, IPAddress& _ip){
if (_ip != 0U){ if (_ip != INADDR_NONE){
root[key] = _ip.toString(); root[key] = _ip.toString();
} }
} }

View File

@ -2,12 +2,18 @@
WiFiStatus::WiFiStatus(AsyncWebServer *server) : _server(server) { WiFiStatus::WiFiStatus(AsyncWebServer *server) : _server(server) {
_server->on(WIFI_STATUS_SERVICE_PATH, HTTP_GET, std::bind(&WiFiStatus::wifiStatus, this, std::placeholders::_1)); _server->on(WIFI_STATUS_SERVICE_PATH, HTTP_GET, std::bind(&WiFiStatus::wifiStatus, this, std::placeholders::_1));
#if defined(ESP8266)
_onStationModeConnectedHandler = WiFi.onStationModeConnected(onStationModeConnected); _onStationModeConnectedHandler = WiFi.onStationModeConnected(onStationModeConnected);
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(onStationModeDisconnected); _onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(onStationModeDisconnected);
_onStationModeGotIPHandler = WiFi.onStationModeGotIP(onStationModeGotIP); _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) { void WiFiStatus::onStationModeConnected(const WiFiEventStationModeConnected& event) {
Serial.print("WiFi Connected. SSID="); Serial.print("WiFi Connected. SSID=");
Serial.println(event.ssid); Serial.println(event.ssid);
@ -24,6 +30,23 @@ void WiFiStatus::onStationModeGotIP(const WiFiEventStationModeGotIP& event) {
Serial.print(", hostName="); Serial.print(", hostName=");
Serial.println(WiFi.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) { void WiFiStatus::wifiStatus(AsyncWebServerRequest *request) {
AsyncJsonResponse * response = new AsyncJsonResponse(); AsyncJsonResponse * response = new AsyncJsonResponse();
@ -40,10 +63,10 @@ void WiFiStatus::wifiStatus(AsyncWebServerRequest *request) {
root["gateway_ip"] = WiFi.gatewayIP().toString(); root["gateway_ip"] = WiFi.gatewayIP().toString();
IPAddress dnsIP1 = WiFi.dnsIP(0); IPAddress dnsIP1 = WiFi.dnsIP(0);
IPAddress dnsIP2 = WiFi.dnsIP(1); IPAddress dnsIP2 = WiFi.dnsIP(1);
if (dnsIP1 != 0U){ if (dnsIP1 != INADDR_NONE){
root["dns_ip_1"] = dnsIP1.toString(); root["dns_ip_1"] = dnsIP1.toString();
} }
if (dnsIP2 != 0U){ if (dnsIP2 != INADDR_NONE){
root["dns_ip_2"] = dnsIP2.toString(); root["dns_ip_2"] = dnsIP2.toString();
} }
} }

View File

@ -26,15 +26,21 @@ class WiFiStatus {
AsyncWebServer* _server; AsyncWebServer* _server;
#if defined(ESP8266)
// handler refrences for logging important WiFi events over serial // handler refrences for logging important WiFi events over serial
WiFiEventHandler _onStationModeConnectedHandler; WiFiEventHandler _onStationModeConnectedHandler;
WiFiEventHandler _onStationModeDisconnectedHandler; WiFiEventHandler _onStationModeDisconnectedHandler;
WiFiEventHandler _onStationModeGotIPHandler; WiFiEventHandler _onStationModeGotIPHandler;
// static functions for logging WiFi events to the UART // static functions for logging WiFi events to the UART
static void onStationModeConnected(const WiFiEventStationModeConnected& event); static void onStationModeConnected(const WiFiEventStationModeConnected& event);
static void onStationModeDisconnected(const WiFiEventStationModeDisconnected& event); static void onStationModeDisconnected(const WiFiEventStationModeDisconnected& event);
static void onStationModeGotIP(const WiFiEventStationModeGotIP& 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); void wifiStatus(AsyncWebServerRequest *request);