Analog clock with the Wemos D1 Mini

FunduinoLast updated on

Die Uhrzeit per NTP aus dem Internet holen

Eine Uhr braucht nicht zwingend ein Echtzeitmodul. Wenn der Mikrocontroller ins WLAN kommt, holt er sich die Zeit einfach aus dem Internet – über das NTP (Network Time Protocol).

Genau das macht dieses Projekt: Ein Wemos D1 Mini mit ESP8266-Chip verbindet sich mit deinem Router, fragt einen öffentlichen NTP-Server ab und zeigt Datum und Uhrzeit auf einem OLED-Display an. Sommer- und Winterzeit berücksichtigt das Programm automatisch.

Gebraucht werden nur zwei Bauteile: das Wemos D1 Mini und ein OLED mit I²C-Anschluss.

OLED display with date and time
Date and time on the OLED

You need this

0.96-inch OLED display - 128x64 pixels, I2C, SSD1306 0.96-inch OLED display - 128x64 pixels, I2C, SSD1306 4,19 € *Item no. FUN-1010561About the product — 0.96-inch OLED display - 128x64 pixels, I2C, SSD1306
WeMos D1 Mini ESP8266 - development board with Wi-Fi, 3.3 V WeMos D1 Mini ESP8266 - development board with Wi-Fi, 3.3 V 5,95 € *Item no. F23106678About the product — WeMos D1 Mini ESP8266 - development board with Wi-Fi, 3.3 V

All 2 parts together 10,14 € *

* All prices include statutory VAT, plus shipping costs.

Wemos D1 Mini and NTP

The Wemos D1 Mini is a microcontroller with an ESP8266 chip and therefore Wi-Fi capable. That’s exactly what this project needs: the time comes from the internet, not from a real-time clock module.

That’s where NTP (Network Time Protocol), a protocol for synchronizing clocks in computer systems, comes in. Public NTP servers are available on the internet for anyone to query.

What should be displayed

Time and date appear on an OLED display: the date at the top, a divider line below it, and the time in large text underneath.

Required components:

  • Wemos D1 Mini
  • 0.96-inch OLED display, 128x64, I²C

The item is called “Analog Clock,” but the program displays the time in digits - digitally. No clock face with hands is drawn.

OLED display with date and time
Date and time on the OLED

Install the board

Enter an additional board manager URL under File → Preferences:

https://arduino.esp8266.com/stable/package_esp8266com_index.json

Then open the Boards Manager via Tools → Board → Boards Manager, search for esp8266 and install the package.

If the Wemos D1 Mini is not detected automatically, click “Select another board and port” and search for Wemos D1 there. The USB port name varies depending on the operating system.

Input field for additional Board Manager URLs in the settings
File → Settings → Board Manager URL
ESP8266 package in Boards Manager
Install the esp8266 package
Dialog for selecting a board and port
Select board and port
Selecting the Wemos D1 Mini from the board list
Select Wemos D1 Mini
1 / 4

Install the libraries

Two libraries are required:

  • U8g2 – for output on the OLED
  • NTP – for querying the time from the NTP server

You can find both in the Library Manager. Detailed documentation for U8g2 is available in the project's GitHub wiki.

U8g2 library in the library manager
The U8g2 library
NTP library in the library manager
The NTP library
1 / 2

The circuit diagram

The OLED is connected via I²C: VCC, GND, SCL, and SDA - that's all it needs.

Wiring a Wemos D1 Mini and OLED via I2C
The circuit diagram

The program

The program connects to the local Wi-Fi network via DHCP. First, enter SSID and password for your router in the sketch.

Once connected, the Serial Monitor displays the router name and the assigned IP address. The date and time then appear on the display and are updated every 30 seconds.

The SSID and password are in plain text in the sketch. So don’t share the sketch unchanged.

SSID and IP address in the Serial Monitor
Connection successfully established
Display the date and time via NTP on the OLED
// Include libraries
# include <ESP8266WiFi.h>
# include <NTP.h>
# include <WiFiUdp.h>
# include <NTP.h>
# include <U8g2lib.h>

/*
  Type designation with screen size in pixels
  1 = page buffer mode
  Hardware I2C
  OLED name
  Rotation R0 (none)
*/
U8G2_SSD1306_128X64_NONAME_1_HW_I2C oled(U8G2_R0, U8X8_PIN_NONE);

// Router SSID and password
char Router[] = "SSID_des_Routers";
char Passwort[] = "xxxxxxx";

// Start NTP
WiFiUDP wifiUdp;
NTP ntp(wifiUdp);

// Time until the next measurement
static unsigned long GesicherteStartZeit = 0;
unsigned long Startzeit;

// 30-second interval, can be adjusted
int Intervall = 30000;

void setup()
{
  Serial.begin(9600);
  delay(500);

  // Start WiFi with DHCP
  WiFi.begin(Router, Passwort);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
  }

  // Display the router SSID in the serial monitor
  Serial.print("Connected to ");
  Serial.println(WiFi.SSID());

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

  /*
    Time zone
    CEST: Central European Summer Time
    Start of European daylight saving time: last Sunday in March at 2 a.m. GMT + 2 hours
  */
  ntp.ruleDST("CEST", Last, Sun, Mar, 2, 120);

  // CET: Central European Time
  // Start of standard time: last Sunday in October at 3 a.m. GMT + 1 hour
  ntp.ruleSTD("CET", Last, Sun, Oct, 3, 60);

  // Start NTP
  ntp.begin();

  // Start OLED
  oled.begin();

  // Font
  oled.setFont(u8g2_font_courB24_tf);

  // Draw color white
  oled.setDrawColor(1);

  // Horizontal text
  oled.setFontDirection(0);

  // Display date and time on startup
  ntp.update();
  oled.clearDisplay();
  oled.firstPage();
  do
  {
    oled.setCursor(2, 15);
    oled.setFont(u8g2_font_t0_22_te);

    // Day: add leading 0
    if (ntp.day() < 10) oled.print("0");
    oled.print(ntp.day());
    oled.print(".");

    // Month: add leading 0
    if (ntp.month() < 10) oled.print("0");
    oled.print(ntp.month());
    oled.print(".");

    // Year
    oled.print(ntp.year());

    // Horizontal line
    oled.drawHLine(1, 22, oled.getDisplayWidth());
    oled.setCursor(2, 63);

    // Time
    // Hours: add leading 0
    oled.setFont(u8g2_font_logisoso32_tf);
    if (ntp.hours() < 10) oled.print("0");
    oled.print(String(ntp.hours()) + ":");

    // Minutes: add leading 0
    if (ntp.minutes() < 10) oled.print("0");
    oled.print(String(ntp.minutes()));
  }
  while (oled.nextPage());
}

void loop()
{
  // Set start time
  Startzeit = millis();

  // If the specified interval has elapsed
  if (Startzeit - GesicherteStartZeit > Intervall)
  {
    // Update time
    ntp.update();
    oled.clearDisplay();

    // Reset start time
    GesicherteStartZeit = Startzeit;

    // Display date and time on the OLED
    oled.setCursor(3, 20);
    oled.firstPage();
    do
    {
      oled.setCursor(2, 15);
      oled.setFont(u8g2_font_t0_22_te);

      // Day: add leading 0
      if (ntp.day() < 10) oled.print("0");
      oled.print(ntp.day());
      oled.print(".");

      // Month: add leading 0
      if (ntp.month() < 10) oled.print("0");
      oled.print(ntp.month());
      oled.print(".");
      oled.print(ntp.year());

      // Horizontal line
      oled.drawHLine(1, 22, oled.getDisplayWidth());
      oled.setCursor(2, 63);

      // Time
      // Hours: add leading 0
      oled.setFont(u8g2_font_logisoso32_tf);
      if (ntp.hours() < 10) oled.print("0");
      oled.print(String(ntp.hours()) + ":");

      // Minutes: add leading 0
      if (ntp.minutes() < 10) oled.print("0");
      oled.print(String(ntp.minutes()));
    }
    while (oled.nextPage());
  }
}
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.