2019-05-05
Achtung!
Funktioniert erst ab Arduino Core Version 2.4.0 oder höher.

// ****************************************************************
// Sketch Esp8266 WiFi NTP Lokalzeit und UTC
// created: Jens Fleischer, 2019-05-05
// last mod: Jens Fleischer, 2019-05-25
// For more information visit: https://fipsok.de
// ****************************************************************
// Hardware: Esp8266
// Software: Esp8266 Arduino Core 2.4.2 / 2.5.0
// Getestet auf: Nodemcu, Wemos D1 Mini Pro, Sonoff Switch, Sonoff Dual
/******************************************************************
  Copyright (c) 2018 Jens Fleischer. All rights reserved.

  This file is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.
  This file is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.
*******************************************************************/
// Automatische Umstellung zwischen Sommer- und Normalzeit
/**************************************************************************************/

#include <ESP8266WiFi.h>
#include <time.h>

struct tm lt;         // http://www.cplusplus.com/reference/ctime/tm/
struct tm utc;

const char* const PROGMEM ntpServer[] = {"fritz.box", "de.pool.ntp.org", "at.pool.ntp.org", "ch.pool.ntp.org", "ptbtime1.ptb.de", "europe.pool.ntp.org"};
const char* const PROGMEM dayNames[] = {"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"};
const char* const PROGMEM dayShortNames[] = {"So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"};
const char* const PROGMEM monthNames[] = {"Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"};
const char* const PROGMEM monthShortNames[] = {"Jan", "Feb", "Mrz", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"};

const char* ssid = "Netzwerkname";             // << kann bis zu 32 Zeichen haben
const char* password = "PasswortvomNetzwerk";  // << mindestens 8 Zeichen jedoch nicht länger als 64 Zeichen

void setupTime() {
  configTime(0, 0, ntpServer[1]);  // deinen NTP Server einstellen (von 0 - 5 aus obiger Liste) alternativ lassen sich durch Komma getrennt bis zu 3 Server angeben
  setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0/3", 1);   // Zeitzone einstellen https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
}

void setup() {
  Serial.begin(115200);
  delay(100);
  Serial.printf("\nSketchname: %s\nBuild: %s\n%s\n\n", (__FILE__), (__TIMESTAMP__), ESP.getFullVersion().c_str());

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nVerbunden mit: " + WiFi.SSID());
  setupTime();
  delay(250);
}

void loop() {
  static char buf[30];                                    // je nach Format von "strftime" eventuell anpassen
  static time_t lastsek {0};
  time_t now = time(&now);
  localtime_r(&now, &lt);
  gmtime_r(&now, &utc);
  if (lt.tm_sec != lastsek) {
    lastsek = lt.tm_sec;
    if (!(time(&now) % 86400)) {                          // einmal am Tag die Zeit vom NTP Server holen o. jede Stunde "% 3600" aller zwei "% 7200"
      setupTime();
    }
    strftime (buf, sizeof(buf), "%d.%m.%Y %T %A", &utc);  // http://www.cplusplus.com/reference/ctime/strftime/
    Serial.printf("Koordinierte  Weltzeit %s\n", buf);
    strftime (buf, sizeof(buf), "%d.%m.%Y %T", &lt);      // http://www.cplusplus.com/reference/ctime/strftime/
    Serial.printf("Mitteleuropäische Zeit %s %s\n", buf, dayNames[utc.tm_wday]);
    Serial.printf("UTC: %.2d:%.2d:%.2d\n", utc.tm_hour, utc.tm_min, utc.tm_sec);
    Serial.printf("%s: %.2d:%.2d:%.2d %s\n\n", *tzname, lt.tm_hour, lt.tm_min, lt.tm_sec, lt.tm_isdst ? "Sommerzeit" : "Normalzeit");
  }
}

Zurück