From 272535006b5a49661a2e60467fce2954f34a0d8e Mon Sep 17 00:00:00 2001 From: Mark van Renswoude Date: Fri, 5 Jan 2018 21:15:04 +0100 Subject: [PATCH] Implemented NTP client --- platformio.ini | 4 ++- src/assets/version.h | 4 +-- src/main.cpp | 58 ++++++++++++++++++++++++++++++++++++++------ 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/platformio.ini b/platformio.ini index 390bfd9..8edb478 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,4 +15,6 @@ framework = arduino upload_speed = 115200 lib_deps = ArduinoJson - ESP Async WebServer \ No newline at end of file + ESP Async WebServer + NTPClient + Time \ No newline at end of file diff --git a/src/assets/version.h b/src/assets/version.h index 149b1f3..623def7 100644 --- a/src/assets/version.h +++ b/src/assets/version.h @@ -4,10 +4,10 @@ const uint8_t VersionMajor = 2; const uint8_t VersionMinor = 0; const uint8_t VersionPatch = 0; -const uint8_t VersionMetadata = 8; +const uint8_t VersionMetadata = 9; const char VersionBranch[] = "release/2.0"; const char VersionSemVer[] = "2.0.0-beta.1"; -const char VersionFullSemVer[] = "2.0.0-beta.1+8"; +const char VersionFullSemVer[] = "2.0.0-beta.1+9"; const char VersionCommitDate[] = "2018-01-05"; #endif diff --git a/src/main.cpp b/src/main.cpp index 85041af..d0ede4b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,9 @@ #include #include #include +#include +#include +#include extern "C" { #include @@ -30,8 +33,10 @@ ADC_MODE(ADC_VCC); void initWiFi(); #ifdef SerialDebug void wifiEvent(WiFiEvent_t event); +void updateDebugStatus(); #endif void updateLED(); +void updateNTPClient(); void startServer(); void stopServer(); @@ -41,6 +46,9 @@ void handleNotFound(AsyncWebServerRequest* request); AsyncWebServer server(80); PCA9685* pwmDriver; +WiFiUDP ntpUDP; +NTPClient* ntpClient = NULL; + bool accessPoint = false; bool stationMode = false; bool forceAccessPoint = false; @@ -48,7 +56,7 @@ bool forceAccessPoint = false; uint32_t stationModeStart = 0; #ifdef SerialDebug -uint32_t memoryStatusTime = 0; +uint32_t debugStatusTime = 0; #endif @@ -132,12 +140,7 @@ void loop() #ifdef SerialDebug - if (currentTime - memoryStatusTime >= 5000) - { - _d("Memory :: available: "); - _dln(ESP.getFreeHeap()); - memoryStatusTime = currentTime; - } + updateDebugStatus(); #endif @@ -179,6 +182,7 @@ void loop() } updateLED(); + updateNTPClient(); stairs->tick(); } @@ -260,6 +264,26 @@ void wifiEvent(WiFiEvent_t event) _dln("WiFi:: soft AP mode: station disconnected"); break; } } + + +void updateDebugStatus() +{ + if (currentTime - debugStatusTime < 5000) return; + debugStatusTime = currentTime; + + + _d("Status :: available heap: "); + _dln(ESP.getFreeHeap()); + + if (ntpClient != NULL) + { + _d("Status :: time: "); + uint32_t time = ntpClient->getEpochTime(); + + _d(day(time)); _d("-"); _d(month(time)); _d("-"); _d(year(time)); _d(" "); + _d(hour(time)); _d(":"); _d(minute(time)); _d(":"); _dln(second(time)); + } +} #endif @@ -283,6 +307,26 @@ void updateLED() } +void updateNTPClient() +{ + if (ntpClient == NULL && WiFi.status() == WL_CONNECTED) + { + _dln("NTP :: initializing NTP client"); + + // TODO make NTP address and refresh interval configurable + ntpClient = new NTPClient(ntpUDP, "nl.pool.ntp.org", 0, 5 * 60 * 1000); + ntpClient->begin(); + } + + + if (ntpClient != NULL) + { + ntpClient->update(); + // TODO check for triggers every 10 seconds or so + } +} + + void handleNotFound(AsyncWebServerRequest *request) { _d("HTTP :: not found: "); _dln(request->url());