63 lines
1.2 KiB
Arduino
63 lines
1.2 KiB
Arduino
#include <ESP8266WiFi.h>
|
|
#include <time.h>
|
|
|
|
const char* ssid = "Wayne Manor";
|
|
const char* password = "einfach10zeichen!";
|
|
const char* TZ_INFO = "CET-1CEST,M3.5.0,M10.5.0/3";
|
|
|
|
void connectWiFi() {
|
|
Serial.print("Verbinde mit WLAN");
|
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
|
|
Serial.println("\nVerbunden!");
|
|
Serial.print("IP: ");
|
|
Serial.println(WiFi.localIP());
|
|
}
|
|
|
|
void syncTime() {
|
|
configTime(TZ_INFO, "pool.ntp.org", "time.nist.gov");
|
|
|
|
Serial.print("Synchronisiere Zeit");
|
|
|
|
time_t now = time(nullptr);
|
|
while (now < 100000) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
now = time(nullptr);
|
|
}
|
|
|
|
Serial.println("\nZeit synchronisiert!");
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
Serial.println("\nStarte ESP8266...");
|
|
|
|
connectWiFi();
|
|
syncTime();
|
|
}
|
|
|
|
void loop() {
|
|
time_t now = time(nullptr);
|
|
struct tm* t = localtime(&now);
|
|
|
|
if (t) {
|
|
Serial.printf("%02d:%02d:%02d %02d.%02d.%04d\n",
|
|
t->tm_hour,
|
|
t->tm_min,
|
|
t->tm_sec,
|
|
t->tm_mday,
|
|
t->tm_mon + 1,
|
|
t->tm_year + 1900);
|
|
} else {
|
|
Serial.println("Fehler beim Lesen der Zeit");
|
|
}
|
|
|
|
delay(1000);
|
|
} |