feat: fetch ntp servers from dhcp via a software dhcp implementation
This commit is contained in:
parent
5d1704cdf8
commit
f31106a47d
2 changed files with 69 additions and 25 deletions
|
@ -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
|
||||
https://git.kabelsalat.ch/s3lph/Arduino-Library-SoftDhcp.git
|
||||
|
|
|
@ -3,33 +3,75 @@
|
|||
#include <WiFiUdp.h>
|
||||
#include <NTP.h>
|
||||
#include <Adafruit_NeoPixel.h>
|
||||
#include <SoftDhcp.h>
|
||||
|
||||
#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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue