From f31106a47d7da479cd783737a67b08909b519a01 Mon Sep 17 00:00:00 2001 From: s3lph Date: Fri, 27 Dec 2024 23:19:19 +0100 Subject: [PATCH] feat: fetch ntp servers from dhcp via a software dhcp implementation --- esp/platformio.ini | 3 +- esp/src/main.cpp | 91 ++++++++++++++++++++++++++++++++++------------ 2 files changed, 69 insertions(+), 25 deletions(-) diff --git a/esp/platformio.ini b/esp/platformio.ini index 39bbc1a..4293149 100644 --- a/esp/platformio.ini +++ b/esp/platformio.ini @@ -16,5 +16,4 @@ upload_protocol = esptool lib_deps = adafruit/Adafruit NeoPixel@^1.11.0 sstaub/NTP@^1.6 -;board_build.mcu = esp8266 -;board_build.f_cpu = 80000000L \ No newline at end of file + https://git.kabelsalat.ch/s3lph/Arduino-Library-SoftDhcp.git diff --git a/esp/src/main.cpp b/esp/src/main.cpp index 589c7a4..334bda6 100644 --- a/esp/src/main.cpp +++ b/esp/src/main.cpp @@ -3,33 +3,75 @@ #include #include #include +#include + +#define DHCP_TIME_SERVERS 4 Adafruit_NeoPixel pixels(60, 5, NEO_GRB + NEO_KHZ800); uint16_t ADC_MAX = 900; uint16_t ADC_MIN = 250; uint8_t ADC_INVERT = 1; -uint8_t BRIGHT_MIN = 50; +uint8_t BRIGHT_MIN = 25; uint8_t BRIGHT_MAX = 255; const char *ssid = put your ssid here; const char *password = put your psk here; +SoftDhcp dhcp; WiFiUDP wifiUdp; NTP ntp(wifiUdp); +bool ntpInitialized = false; + +IPAddress ntp_server; + +void setupNtp() { + Serial.print("IP: "); + Serial.println(dhcp.localIP()); + Serial.print("NM: "); + Serial.println(dhcp.subnetMask()); + Serial.print("GW: "); + Serial.println(dhcp.gatewayIP()); + Serial.print("DNS1: "); + Serial.println(dhcp.dnsIP(0)); + Serial.print("DNS2: "); + Serial.println(dhcp.dnsIP(1)); + ntp.stop(); + uint32_t ip; + ntp.ruleDST("CEST", Last, Sun, Mar, 2, 120); // last sunday in march 2:00, timetone +120min (+1 GMT + 1h summertime offset) + ntp.ruleSTD("CET", Last, Sun, Oct, 3, 60); // last sunday in october 3:00, timezone +60min (+1 GMT) + if (dhcp.getOption(DHCP_TIME_SERVERS, &ip, 4, 0) > 0) { + ntp_server = ip; + ntp.begin(ntp_server); + Serial.print("Starting NTP using "); + Serial.println(ntp_server.toString()); + } else { + ntp.begin(); + Serial.println("Starting NTP"); + } + ntp.update(); + ntpInitialized = true; +} + +void expired() { + //Serial.println("DHCP lease expired, stopping NTP"); + ntp.stop(); + ntpInitialized = false; +} void setup() { Serial.begin(115200); + dhcp.onLease(setupNtp); + dhcp.onExpire(expired); + dhcp.requestOption(DHCP_TIME_SERVERS); + dhcp.begin(); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { Serial.println("Connecting ..."); delay(500); } - Serial.println("Connected"); - ntp.ruleDST("CEST", Last, Sun, Mar, 2, 120); // last sunday in march 2:00, timetone +120min (+1 GMT + 1h summertime offset) - ntp.ruleSTD("CET", Last, Sun, Oct, 3, 60); // last sunday in october 3:00, timezone +60min (+1 GMT) - ntp.begin(); - Serial.println("start NTP"); + Serial.println("Connected. Getting DHCP time info"); + dhcp.update(); pixels.begin(); } @@ -45,21 +87,24 @@ void loop() { bscale = 255 - bscale; } bscale = min(max(bscale, BRIGHT_MIN), BRIGHT_MAX); - Serial.print(brightness); - Serial.print(" -> "); - Serial.println(bscale); - ntp.update(); - Serial.println(ntp.formattedTime("%T")); - uint8_t h = sixtyToPixel((ntp.hours() % 12) * 5 + (ntp.minutes() / 12)); - uint8_t hl = sixtyToPixel((ntp.hours() % 12) * 5 + (ntp.minutes() / 12) - 1); - uint8_t hr = sixtyToPixel((ntp.hours() % 12) * 5 + (ntp.minutes() / 12) + 1); - uint8_t m = sixtyToPixel(ntp.minutes()); - uint8_t s = sixtyToPixel(ntp.seconds()); - pixels.clear(); - pixels.setPixelColor(hl, pixels.getPixelColor(hl) | (bscale << 16)); - pixels.setPixelColor(h, pixels.getPixelColor(h) | (bscale << 16)); - pixels.setPixelColor(hr, pixels.getPixelColor(hr) | (bscale << 16)); - pixels.setPixelColor(m, pixels.getPixelColor(m) | (bscale << 8) | (bscale >> 2)); - pixels.setPixelColor(s, pixels.getPixelColor(s) | bscale); - pixels.show(); + //Serial.print(brightness); + //Serial.print(" -> "); + //Serial.println(bscale); + dhcp.update(); + if (ntpInitialized) { + ntp.update(); + Serial.println(ntp.formattedTime("%T")); + uint8_t h = sixtyToPixel((ntp.hours() % 12) * 5 + (ntp.minutes() / 12)); + uint8_t hl = sixtyToPixel((ntp.hours() % 12) * 5 + (ntp.minutes() / 12) - 1); + uint8_t hr = sixtyToPixel((ntp.hours() % 12) * 5 + (ntp.minutes() / 12) + 1); + uint8_t m = sixtyToPixel(ntp.minutes()); + uint8_t s = sixtyToPixel(ntp.seconds()); + pixels.clear(); + pixels.setPixelColor(hl, pixels.getPixelColor(hl) | (bscale << 16)); + pixels.setPixelColor(h, pixels.getPixelColor(h) | (bscale << 16)); + pixels.setPixelColor(hr, pixels.getPixelColor(hr) | (bscale << 16)); + pixels.setPixelColor(m, pixels.getPixelColor(m) | (bscale << 8) | (bscale >> 2)); + pixels.setPixelColor(s, pixels.getPixelColor(s) | bscale); + pixels.show(); + } }