Browse Source

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

master
Rick Watson 6 years ago
parent
commit
14820616a6
  1. 1
      platformio.ini
  2. 19
      src/NTPSettingsService.cpp
  3. 11
      src/NTPSettingsService.h
  4. 27
      src/OTASettingsService.cpp
  5. 16
      src/OTASettingsService.h
  6. 5
      src/WiFiScanner.cpp
  7. 18
      src/WiFiSettingsService.cpp
  8. 29
      src/WiFiStatus.cpp
  9. 10
      src/WiFiStatus.h

1
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

19
src/NTPSettingsService.cpp

@ -1,8 +1,14 @@
#include <NTPSettingsService.h>
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...");

11
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);

27
src/OTASettingsService.cpp

@ -1,17 +1,15 @@
#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() {}
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

16
src/OTASettingsService.h

@ -2,7 +2,13 @@
#define OTASettingsService_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 <ArduinoOTA.h>
@ -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

5
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);

18
src/WiFiSettingsService.cpp

@ -1,7 +1,6 @@
#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() {}
@ -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<String>())){
_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();
}
}

29
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();
}
}

10
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);

Loading…
Cancel
Save