Zeit mit Biblio­thek TFT_eSPI

// ESP32
#include "WiFi.h"

// ESP8266
// #include "ESP8266WiFi.h"

#include "time.h"
#include "TFT_eSPI.h"

TFT_eSPI tft = TFT_eSPI();

char Router[] = "Router_SSID";
char Passwort[] = "xxxxxxxx";;

// NTP-Server aus dem Pool
#define Zeitserver "de.pool.ntp.org"

/*
  Liste der Zeitzonen
  https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
  Zeitzone CET = Central European Time -1 -> 1 Stunde zurück
  CEST = Central European Summer Time von
  M3 = März, 5.0 = Sonntag 5. Woche, 02 = 2 Uhr
  bis M10 = Oktober, 5.0 = Sonntag 5. Woche 03 = 3 Uhr
*/
#define Zeitzone "CET-1CEST,M3.5.0/02,M10.5.0/03"

// time_t enthält die Anzahl der Sekunden seit dem 1.1.1970 0 Uhr
time_t aktuelleZeit;

/* 
  Struktur tm
  tm_hour -> Stunde: 0 bis 23
  tm_min -> Minuten: 0 bis 59
  tm_sec -> Sekunden 0 bis 59
  tm_mday -> Tag 1 bis 31
  tm_mon -> Monat: 0 (Januar) bis 11 (Dezember)
  tm_year -> Jahre seit 1900
  tm_yday -> vergangene Tage seit 1. Januar des Jahres
  tm_isdst -> Wert > 0 = Sommerzeit (dst = daylight saving time)
*/
tm Zeit;

const unsigned char Uhr [] PROGMEM = {
	// 'Uhr, 80x80px
	0x00, 0x00, 0x00, 0x00, 0x3f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 
	0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 
	0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 
	0xe0, 0x07, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xfc, 0x00, 0x00, 0x3f, 0xfe, 0x00, 0x00, 
	0x00, 0x01, 0xff, 0xf0, 0x00, 0x00, 0x07, 0xff, 0x80, 0x00, 0x00, 0x03, 0xff, 0x80, 0x00, 0x00, 
	0x01, 0xff, 0xc0, 0x00, 0x00, 0x07, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xe0, 0x00, 0x00, 0x0f, 
	0xfc, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf0, 0x00, 0x00, 0x1f, 0xf0, 0x00, 0x03, 0xc0, 0x00, 0x0f, 
	0xf8, 0x00, 0x00, 0x3f, 0xe0, 0x00, 0x03, 0xc0, 0x00, 0x07, 0xfc, 0x00, 0x00, 0x7f, 0xc0, 0x00, 
	0x01, 0x80, 0x00, 0x03, 0xfe, 0x00, 0x00, 0xff, 0x80, 0x70, 0x00, 0x00, 0x0e, 0x01, 0xff, 0x00, 
	0x00, 0xff, 0x00, 0x70, 0x00, 0x00, 0x0e, 0x00, 0xff, 0x00, 0x01, 0xfe, 0x00, 0x70, 0x00, 0x00, 
	0x0e, 0x00, 0x7f, 0x80, 0x03, 0xfc, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x3f, 0xc0, 0x03, 0xf8, 
	0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x1f, 0xc0, 0x07, 0xf0, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 
	0x0f, 0xe0, 0x07, 0xf0, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x0f, 0xe0, 0x0f, 0xe0, 0x00, 0x00, 
	0x01, 0x80, 0x00, 0x00, 0x07, 0xf0, 0x0f, 0xc0, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x03, 0xf0, 
	0x1f, 0xc0, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x03, 0xf8, 0x1f, 0x80, 0xe0, 0x00, 0x01, 0x80, 
	0x00, 0x03, 0x81, 0xf8, 0x3f, 0x80, 0xe0, 0x00, 0x01, 0x80, 0x00, 0x03, 0x81, 0xfc, 0x3f, 0x80, 
	0xe0, 0x00, 0x01, 0x80, 0x00, 0x03, 0x81, 0xfc, 0x3f, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 
	0x00, 0xfc, 0x7f, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0xfe, 0x7e, 0x00, 0x00, 0x00, 
	0x01, 0x80, 0x00, 0x00, 0x00, 0x7e, 0x7e, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x7e, 
	0x7e, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x7e, 0x7e, 0x00, 0x00, 0x00, 0x01, 0x80, 
	0x00, 0x00, 0x00, 0x7e, 0xfe, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x7f, 0xfc, 0x00, 
	0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x3f, 0xfc, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 
	0x00, 0x3f, 0xfc, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x3f, 0xfc, 0x0c, 0x00, 0x00, 
	0x01, 0x80, 0x00, 0x00, 0x30, 0x3f, 0xfc, 0x1c, 0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x78, 0x3f, 
	0xfc, 0x1c, 0x00, 0x00, 0x07, 0x80, 0x00, 0x00, 0x78, 0x3f, 0xfc, 0x0c, 0x00, 0x00, 0x1f, 0x80, 
	0x00, 0x00, 0x30, 0x3f, 0xfc, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xfc, 0x00, 
	0x00, 0x01, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xfc, 0x00, 0x00, 0x03, 0xe0, 0x00, 0x00, 0x00, 
	0x00, 0x3f, 0xfe, 0x00, 0x00, 0x0f, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x7e, 0x00, 0x00, 0x3f, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x7e, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 
	0x7e, 0x00, 0x01, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x7e, 0x00, 0x07, 0xe0, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x7e, 0x7f, 0x00, 0x07, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x3f, 0x00, 
	0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x3f, 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x03, 
	0x80, 0xfc, 0x3f, 0x81, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x03, 0x81, 0xfc, 0x1f, 0x80, 0xc0, 0x00, 
	0x00, 0x00, 0x00, 0x03, 0x81, 0xf8, 0x1f, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xf8, 
	0x0f, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xf0, 0x0f, 0xe0, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x07, 0xf0, 0x07, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xe0, 0x07, 0xf0, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xe0, 0x03, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x1f, 0xc0, 0x03, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xc0, 0x01, 0xfe, 0x00, 0x70, 
	0x00, 0x00, 0x0e, 0x00, 0x7f, 0x80, 0x00, 0xff, 0x00, 0x70, 0x00, 0x00, 0x0e, 0x00, 0xff, 0x00, 
	0x00, 0xff, 0x80, 0x70, 0x00, 0x00, 0x0e, 0x01, 0xff, 0x00, 0x00, 0x7f, 0xc0, 0x00, 0x01, 0x80, 
	0x00, 0x03, 0xfe, 0x00, 0x00, 0x3f, 0xe0, 0x00, 0x03, 0xc0, 0x00, 0x07, 0xfc, 0x00, 0x00, 0x1f, 
	0xf0, 0x00, 0x03, 0xc0, 0x00, 0x0f, 0xf8, 0x00, 0x00, 0x0f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x3f, 
	0xf0, 0x00, 0x00, 0x07, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xe0, 0x00, 0x00, 0x03, 0xff, 0x80, 
	0x00, 0x00, 0x01, 0xff, 0xc0, 0x00, 0x00, 0x01, 0xff, 0xf0, 0x00, 0x00, 0x07, 0xff, 0x80, 0x00, 
	0x00, 0x00, 0x7f, 0xfc, 0x00, 0x00, 0x3f, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xe0, 0x07, 
	0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 
	0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 
	0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xfc, 0x00, 0x00, 0x00, 0x00
};

const unsigned char Kalender [] PROGMEM = {
	// 'Kalender, 51x60px
	0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xe0, 0xff, 0xff, 
	0xff, 0x1f, 0xff, 0xff, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xe0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 
	0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x80, 0xf0, 
	0x00, 0x00, 0x01, 0xe0, 0x60, 0x81, 0xf8, 0x00, 0x00, 0x03, 0xf0, 0x60, 0x81, 0xf8, 0x00, 0x00, 
	0x03, 0xf0, 0x60, 0x81, 0xf8, 0x00, 0x00, 0x03, 0xf0, 0x60, 0x81, 0xf8, 0x00, 0x00, 0x03, 0xf0, 
	0x60, 0x80, 0xf0, 0x00, 0x00, 0x01, 0xe0, 0x60, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x80, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x80, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x60, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x80, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x60, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 
	0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x80, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x60, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x80, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x60, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x60, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x80, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x80, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x60, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x80, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x60, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 
	0x80, 0xf0, 0x00, 0x00, 0x01, 0xe0, 0x60, 0x81, 0xf8, 0x00, 0x00, 0x03, 0xf0, 0x60, 0x81, 0xf8, 
	0x00, 0x00, 0x03, 0xf0, 0x60, 0xc1, 0xf8, 0x00, 0x00, 0x03, 0xf0, 0x60, 0xc1, 0xf8, 0x00, 0x00, 
	0x03, 0xf0, 0x60, 0xc0, 0xf0, 0x00, 0x00, 0x01, 0xe0, 0x60, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x60, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xc0, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x7f, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xc0
};

// Intervall anpassen
unsigned long Intervall = 10000;

void setup()
{
  // Zeitzone: Parameter für die zu ermittelnde Zeit
  configTzTime(Zeitzone, Zeitserver);

  Serial.begin(9600);

  // WiFi starten und Verbindung aufbauen
  WiFi.begin(Router, Passwort);
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(200);
    Serial.print(".");
  }

  // SSID des Routers anzeigen
  Serial.println();
  Serial.print("Verbunden mit ");
  Serial.println(WiFi.SSID());

  // IP anzeigen
  Serial.print("IP: ");
  Serial.println(WiFi.localIP());

   // TFT starten
  tft.begin();

  // Rotation anpassen Querformat
  tft.setRotation(1);
  tft.fillScreen(TFT_BLACK);
}

void loop() 
{
  // aktuelle Zeit holen
  time(&aktuelleZeit);

  // localtime_r -> Zeit in die lokale Zeitzone setzen
  localtime_r(&aktuelleZeit, &Zeit);
  tft.fillScreen(TFT_BLACK);

  // Kalender-Symbol anzeigen
  tft.drawBitmap(20, 10, Kalender, 51, 60, TFT_DARKGREEN);  

  // Cursor setzen
  tft.setCursor(120, 20);
  tft.setTextFont(7);
  tft.setTextColor(TFT_DARKGREEN);

  // Tag: führende 0 ergänzen
  if (Zeit.tm_mday < 10) 
  {
    Serial.print("0");
    tft.print("0");
  }
  Serial.print(Zeit.tm_mday);
  Serial.print(".");
  tft.print(Zeit.tm_mday);
  tft.print(".");

  // Monat: führende 0 ergänzen
  if (Zeit.tm_mon < 9) 
  {
    Serial.print("0");
    tft.print("0");
  }
  Serial.print(Zeit.tm_mon + 1);
  Serial.print(".");
  tft.print(Zeit.tm_mon + 1);
  tft.print(".");

  // Anzahl Jahre seit 1900
  Serial.print(Zeit.tm_year + 1900);
  Serial.print(" ");
  tft.print(Zeit.tm_year + 1900);

  // Trennlinien
  tft.drawFastHLine(1, 80, tft.width(), TFT_WHITE);
  tft.drawFastHLine(1, 81, tft.width(), TFT_WHITE);
  tft.drawFastHLine(1, 82, tft.width(), TFT_WHITE);

  // Uhr-Symbol anzeigen
  tft.drawBitmap(20, 150, Uhr, 80, 80, TFT_WHITE);

  // Cursor setzen
  tft.setCursor(120, 150);
  tft.setTextFont(8);
  tft.setTextColor(TFT_WHITE);

  // Stunde: wenn Stunde < 10 -> 0 davor setzen
  if (Zeit.tm_hour < 10) 
  {
    Serial.print("0");
    tft.print("0");
  }
  Serial.print(Zeit.tm_hour);
  tft.print(Zeit.tm_hour);
  Serial.print(":");
  tft.print(":");

  // Minuten
  // wenn Minute < 10 -> 0 davor setzen
  if (Zeit.tm_min < 10) 
  {
    Serial.print("0");
    tft.print("0");
  }
  Serial.println(Zeit.tm_min);
  tft.print(Zeit.tm_min);
  delay(Intervall);
}

Letzte Aktualisierung: Feb. 8, 2025 @ 22:27