Implemented NTP client

This commit is contained in:
Mark van Renswoude 2018-01-05 21:15:04 +01:00
parent 488e8598ec
commit 272535006b
3 changed files with 56 additions and 10 deletions

View File

@ -15,4 +15,6 @@ framework = arduino
upload_speed = 115200 upload_speed = 115200
lib_deps = lib_deps =
ArduinoJson ArduinoJson
ESP Async WebServer ESP Async WebServer
NTPClient
Time

View File

@ -4,10 +4,10 @@
const uint8_t VersionMajor = 2; const uint8_t VersionMajor = 2;
const uint8_t VersionMinor = 0; const uint8_t VersionMinor = 0;
const uint8_t VersionPatch = 0; const uint8_t VersionPatch = 0;
const uint8_t VersionMetadata = 8; const uint8_t VersionMetadata = 9;
const char VersionBranch[] = "release/2.0"; const char VersionBranch[] = "release/2.0";
const char VersionSemVer[] = "2.0.0-beta.1"; 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"; const char VersionCommitDate[] = "2018-01-05";
#endif #endif

View File

@ -8,6 +8,9 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h> #include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <TimeLib.h>
extern "C" { extern "C" {
#include <user_interface.h> #include <user_interface.h>
@ -30,8 +33,10 @@ ADC_MODE(ADC_VCC);
void initWiFi(); void initWiFi();
#ifdef SerialDebug #ifdef SerialDebug
void wifiEvent(WiFiEvent_t event); void wifiEvent(WiFiEvent_t event);
void updateDebugStatus();
#endif #endif
void updateLED(); void updateLED();
void updateNTPClient();
void startServer(); void startServer();
void stopServer(); void stopServer();
@ -41,6 +46,9 @@ void handleNotFound(AsyncWebServerRequest* request);
AsyncWebServer server(80); AsyncWebServer server(80);
PCA9685* pwmDriver; PCA9685* pwmDriver;
WiFiUDP ntpUDP;
NTPClient* ntpClient = NULL;
bool accessPoint = false; bool accessPoint = false;
bool stationMode = false; bool stationMode = false;
bool forceAccessPoint = false; bool forceAccessPoint = false;
@ -48,7 +56,7 @@ bool forceAccessPoint = false;
uint32_t stationModeStart = 0; uint32_t stationModeStart = 0;
#ifdef SerialDebug #ifdef SerialDebug
uint32_t memoryStatusTime = 0; uint32_t debugStatusTime = 0;
#endif #endif
@ -132,12 +140,7 @@ void loop()
#ifdef SerialDebug #ifdef SerialDebug
if (currentTime - memoryStatusTime >= 5000) updateDebugStatus();
{
_d("Memory :: available: ");
_dln(ESP.getFreeHeap());
memoryStatusTime = currentTime;
}
#endif #endif
@ -179,6 +182,7 @@ void loop()
} }
updateLED(); updateLED();
updateNTPClient();
stairs->tick(); stairs->tick();
} }
@ -260,6 +264,26 @@ void wifiEvent(WiFiEvent_t event)
_dln("WiFi:: soft AP mode: station disconnected"); break; _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 #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) void handleNotFound(AsyncWebServerRequest *request)
{ {
_d("HTTP :: not found: "); _dln(request->url()); _d("HTTP :: not found: "); _dln(request->url());