From 47812de4057439feded5340d80996d5833d3fd8e Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Sun, 29 Mar 2020 18:08:54 +0200 Subject: [PATCH] Switched from NTPClient to lwIPs internal SNTP client. This has the advantage of being able to set DST automatically. --- include/effect_big_clock.h | 2 +- include/ntp.h | 6 +----- platformio.ini | 1 - src/effect_analogclock.cpp | 17 +++++++++++++---- src/effect_big_clock.cpp | 21 ++++++++++++--------- src/effect_clock.cpp | 14 +++++++++----- src/effect_pixelclock.cpp | 9 +++++++-- src/effect_timer.cpp | 3 ++- src/ntp.cpp | 25 +++++++++++++++++++------ src/pitrix.cpp | 2 -- 10 files changed, 64 insertions(+), 36 deletions(-) diff --git a/include/effect_big_clock.h b/include/effect_big_clock.h index c7fe495..c029c71 100644 --- a/include/effect_big_clock.h +++ b/include/effect_big_clock.h @@ -10,7 +10,7 @@ private: CRGB _color_seconds_moving_light = CRGB(0x666600); CRGB _color_seconds_moving_dark = CRGB(0x660000); - void _draw_seconds(); + void _draw_seconds(uint8_t seconds); void _draw_border_pixel(accum88 pos, CRGB* color); public: diff --git a/include/ntp.h b/include/ntp.h index 1c4298c..24e55ea 100644 --- a/include/ntp.h +++ b/include/ntp.h @@ -1,9 +1,5 @@ #pragma once -#include -#include "my_wifi.h" #include "config.h" - -extern NTPClient ntpClient; -void updateCallback(NTPClient* c); +void time_changed(); void ntp_setup(); diff --git a/platformio.ini b/platformio.ini index 9e3355a..79fe2a4 100644 --- a/platformio.ini +++ b/platformio.ini @@ -16,7 +16,6 @@ env_default = ota lib_deps = PubSubClient https://github.com/fabianonline/FastLED.git - https://github.com/fabianonline/NTPClient.git https://github.com/me-no-dev/ESPAsyncWebServer.git [env:ota] diff --git a/src/effect_analogclock.cpp b/src/effect_analogclock.cpp index 79be914..0246132 100644 --- a/src/effect_analogclock.cpp +++ b/src/effect_analogclock.cpp @@ -1,6 +1,7 @@ #include "effect_analogclock.h" #include "my_fastled.h" #include "ntp.h" +#include void AnalogClockEffect::loop(uint16_t ms) { window->clear(); @@ -8,10 +9,18 @@ void AnalogClockEffect::loop(uint16_t ms) { CRGB red(0xFF0000); window->circle(8, 8, 7, &white); - uint8_t seconds = ntpClient.getSeconds(); - uint8_t angle = seconds * 256 / 60; - window->lineWithAngle(8, 8, angle, 10, &red); - window->line(1<<8, 1<<8, 12<<8, 4<<8, &white); + time_t now; + tm timeinfo; + time(&now); + localtime_r(&now, &timeinfo); + uint16_t seconds = timeinfo.tm_sec * 1000 + (millis()%1000); + uint16_t angle = seconds * 0x10000 / 60000; + window->lineWithAngle(8, 8, angle, 12, &red); + //window->line(0<<8, 0<<8, 7<<8, 7<<8, &white); + //window->line(15<<8, 0<<8, 8<<8, 7<<8, &red); + //window->line(0<<8, 15<<8, 7<<8, 8<<8, &blue); + //window->line(15<<8, 15<<8, 8<<8, 8<<8, &green); + /*for (uint8_t i=0; i<=12; i++) { window->lineWithAngle(8, 8, 255/12*i, 5, 2, &white); } diff --git a/src/effect_big_clock.cpp b/src/effect_big_clock.cpp index 33569d6..f6b95ef 100644 --- a/src/effect_big_clock.cpp +++ b/src/effect_big_clock.cpp @@ -1,36 +1,39 @@ #include "Effect.h" #include "effect_big_clock.h" #include "fonts.h" -#include "ntp.h" +#include #include "settings.h" void BigClockEffect::loop(uint16_t ms) { window->clear(); - uint8_t h = ntpClient.getHours(); + time_t now; + tm timeinfo; + time(&now); + localtime_r(&now, &timeinfo); + uint8_t h = timeinfo.tm_hour; window->drawChar(&font_numbers3x5_blocky, 6<<8, 2<<8, '0' + (h / 10), &_color_font); window->drawChar(&font_numbers3x5_blocky, 11<<8, 2<<8, '0' + (h % 10), &_color_font); - uint8_t m = ntpClient.getMinutes(); + uint8_t m = timeinfo.tm_min; window->drawChar(&font_numbers3x5_blocky, 6<<8, 9<<8, '0' + (m / 10), &_color_font); window->drawChar(&font_numbers3x5_blocky, 11<<8, 9<<8, '0' + (m % 10), &_color_font); - uint8_t s = ntpClient.getSeconds(); + uint8_t s = timeinfo.tm_sec; if (s & 1) { window->setPixel(3, 10, &_color_font); window->setPixel(3, 12, &_color_font); } - _draw_seconds(); + _draw_seconds(timeinfo.tm_sec); } -void BigClockEffect::_draw_seconds() { - uint8_t seconds = ntpClient.getSeconds(); +void BigClockEffect::_draw_seconds(uint8_t seconds) { for (int i=1; i<=seconds; i++) { _draw_border_pixel(i<<8, (i%5==0) ? &_color_seconds_light : &_color_seconds_dark); } - uint16_t millis = ntpClient.getEpochMillis() % 1000; - accum88 pos = (seconds<<8) + ((settings.effects.big_clock.spacing-1)<<8) * (1000 - millis) / 1000 + (1<<8); + uint16_t mil = millis() % 1000; + accum88 pos = (seconds<<8) + ((settings.effects.big_clock.spacing-1)<<8) * (1000 - mil) / 1000 + (1<<8); uint8_t sec = seconds + 1; while (pos < (60<<8)) { _draw_border_pixel(pos, sec%5==0 ? &_color_seconds_moving_light : &_color_seconds_moving_dark); diff --git a/src/effect_clock.cpp b/src/effect_clock.cpp index 2a90d1f..d34f370 100644 --- a/src/effect_clock.cpp +++ b/src/effect_clock.cpp @@ -42,18 +42,22 @@ void ClockEffect::loop(boolean invert, CRGB fg_color, CRGB bg_color, uint8_t yPo } fg_color = bg_color; } - if (ntpClient.isTimeSet()==false && (ntpClient.getSeconds() & 1)==0) { + /*if (ntpClient.isTimeSet()==false && (ntpClient.getSeconds() & 1)==0) { window->clear(&bg_color); return; - } - int h = ntpClient.getHours(); + }*/ + time_t now; + tm timeinfo; + time(&now); + localtime_r(&now, &timeinfo); + int h = timeinfo.tm_hour; //void drawChar(Font f, uint8_t x, uint8_t y, const char c, CRGB* color, bool mask=false); window->drawChar(&font_numbers3x5, 0<<8, yPos<<8, '0' + (h / 10), &fg_color, invert); window->drawChar(&font_numbers3x5, 4<<8, yPos<<8, '0' + (h % 10), &fg_color, invert); - int m = ntpClient.getMinutes(); + int m = timeinfo.tm_min; window->drawChar(&font_numbers3x5, 9<<8, yPos<<8, '0' + (m / 10), &fg_color, invert); window->drawChar(&font_numbers3x5, 13<<8, yPos<<8, '0' + (m % 10), &fg_color, invert); - if (ntpClient.getSeconds() & 1) { + if (timeinfo.tm_sec & 1) { window->setPixel(7, yPos+1, &fg_color); window->setPixel(7, yPos+3, &fg_color); } diff --git a/src/effect_pixelclock.cpp b/src/effect_pixelclock.cpp index 677c977..79aff2a 100644 --- a/src/effect_pixelclock.cpp +++ b/src/effect_pixelclock.cpp @@ -16,15 +16,20 @@ PixelClockEffect::~PixelClockEffect() { void PixelClockEffect::loop(uint16_t ms) { uint8_t x, y; // Temporary variables for calculating positions window->clear(); + time_t now; + tm timeinfo; + time(&now); + localtime_r(&now, &timeinfo); + // Seconds - uint8_t seconds = ntpClient.getSeconds(); + uint8_t seconds = timeinfo.tm_sec; for (uint8_t s=0; s<60; s++) { x = window->width - 1 - s/10; y = window->height - 1 - (s % 10); if (ssetPixel(x, y, _color_seconds); } - uint8_t minutes = ntpClient.getMinutes(); + uint8_t minutes = timeinfo.tm_min; for (uint8_t m=0; m<60; m++) { x = 6 - m/10; y = window->height - 1 - (m % 10); diff --git a/src/effect_timer.cpp b/src/effect_timer.cpp index 66eceeb..6310e0e 100644 --- a/src/effect_timer.cpp +++ b/src/effect_timer.cpp @@ -9,7 +9,8 @@ void TimerEffect::loop(uint16_t ms) { CRGB bg_color(0x000000); CRGB fg_color(0xCCCCCC); - unsigned long now = ntpClient.getEpochTime() - NTP_OFFSET; + time_t now; + time(&now); long diff = timer - now; window->clear(&bg_color); if (diff < 0 && (now & 1)==0) return; diff --git a/src/ntp.cpp b/src/ntp.cpp index 35e2593..b8f3271 100644 --- a/src/ntp.cpp +++ b/src/ntp.cpp @@ -1,13 +1,26 @@ -#include +#include +#include +#include +#include #include "my_fastled.h" -void updateCallback(NTPClient* c) { +/*void updateCallback(NTPClient* c) { random16_add_entropy(c->getEpochMillis() & 0xFFFF); -} + LOGln("Received current time. Epoch is %lu.", ntpClient.getEpochTime()); +}*/ -WiFiUDP ntpUDP; -NTPClient ntpClient(ntpUDP, NTP_SERVER, NTP_OFFSET, NTP_INTERVAL); +void time_changed() { + random16_add_entropy(millis() & 0xFFFF); + time_t now; + tm timeinfo; + time(&now); + localtime_r(&now, &timeinfo); + char time_s[30]; + strftime(time_s, 30, "%a %d.%m.%Y %T", &timeinfo); + LOGln("NTP * Received time: %s", time_s); +} void ntp_setup() { - ntpClient.setUpdateCallback(updateCallback); + settimeofday_cb(time_changed); + configTime(TZ_Europe_Berlin, "de.pool.ntp.org"); } diff --git a/src/pitrix.cpp b/src/pitrix.cpp index f1b778a..fd4e1e0 100644 --- a/src/pitrix.cpp +++ b/src/pitrix.cpp @@ -45,7 +45,6 @@ void setup() { ntp_setup(); ota_setup(); fastled_setup(); - ntpClient.begin(); #ifdef HTTP_SERVER_ENABLE http_server_setup(); #endif @@ -76,7 +75,6 @@ void loop() { return; } - ntpClient.update(); #ifdef MQTT_ENABLE mqtt_loop(); #endif