Majed Abouhatab, P.E.
Published © LGPL

Saving Your Sanity From Daylight Saving

We stand with Cousin Micki.

IntermediateFull instructions provided2 hours204

Things used in this project

Hardware components

Wemos D1 Mini
Espressif Wemos D1 Mini
×1
MAX7219 Microcontroller 4 In 1 Display LED 5P Line Dot Matrix Module
×2

Software apps and online services

PlatformIO IDE
PlatformIO IDE
worldtimeapi.org

Story

Read more

Schematics

Wiring and Connections

Code

main.cpp

Arduino
#include <WiFiManager.h>
#include <esp8266httpclient.h>
#include <ArduinoJson.h>
#include <U8g2lib.h>
#include <ArduinoOTA.h>

WiFiServer TelnetServer(23);
WiFiClient Telnet;
StaticJsonDocument<1000> doc;
U8G2_MAX7219_64X8_F_4W_HW_SPI u8g2(U8G2_R2, SS, U8X8_PIN_NONE);
int h, m, s;
unsigned long Timestamp;
char buffer[7];

void GetTime(void)
{
  HTTPClient http;
  WiFiClientSecure client;
  client.setInsecure();
  http.useHTTP10(true);
  Serial.printf("%02d:%02d:%02d %s\r\n", h == 0 ? 12 : (h > 12 ? h - 12 : h), m, s, h > 11 ? "PM" : "AM");
  do
  {
    // http.begin(client, "http://worldtimeapi.org/api/ip");
    http.begin(client, "https://timeapi.io/api/time/current/zone?timeZone=America%2FChicago");
    yield();
  } while (http.GET() < 0);
  deserializeJson(doc, http.getString());
  http.end();
  sscanf(doc["dateTime"].as<String>().c_str(), "%*d-%*d-%*dT%d:%d:%d", &h, &m, &s);
  Serial.printf("%02d:%02d:%02d %s\r\n", h == 0 ? 12 : (h > 12 ? h - 12 : h), m, s, h > 11 ? "PM" : "AM");
}

void setup()
{
  tone(LED_BUILTIN, 1);
  u8g2.begin();
  u8g2.setContrast(0);
  u8g2.setFont(u8g2_font_chikita_tr);
  u8g2.clearBuffer();
  // u8g2.drawStr(1, 6, "Connecting...");
  u8g2.sendBuffer();
  u8g2.setFont(u8g2_font_victoriabold8_8r);
  Serial.begin(115200);
  TelnetServer.begin();
  TelnetServer.setNoDelay(true);
  ArduinoOTA.setHostname("WeMosClock");
  ArduinoOTA.begin();
  WiFiManager wm;
  wm.setConfigPortalTimeout(30); // wm.setTimeout(120);
  wm.setBreakAfterConfig(true);
  // wm.resetSettings();
  wm.autoConnect("WeMos");
  if (WiFi.status() != WL_CONNECTED)
    ESP.restart(); // ESP.reset();
  digitalWrite(LED_BUILTIN, 1);
  GetTime();
}

void loop()
{
  if (millis() - Timestamp > 1000)
  {
    Timestamp = millis();
    s++;
    if (s > 59)
    {
      s = 0;
      m++;
      while (m > 59)
        GetTime();
    }
    sprintf(buffer, "%02d:%02d%s", h == 0 ? 12 : (h > 12 ? h - 12 : h), m, h > 11 ? "PM" : "AM");
    u8g2.clearBuffer();
    u8g2.drawStr(0, 7, buffer);
    u8g2.sendBuffer();
    Telnet.printf("%s %d\n", buffer, s);
  }
  if (TelnetServer.hasClient())
  {
    if (!Telnet || !Telnet.connected()) // client is connected
    {
      if (Telnet)
        Telnet.stop();                // client disconnected
      Telnet = TelnetServer.accept(); // ready for new client
    }
    else
      TelnetServer.accept().stop(); // have client, block new conections
  }
  ArduinoOTA.handle();
}

platformio.ini

INI
[env]
; build_flags = -DCORE_DEBUG_LEVEL=5
platform = espressif8266
framework = arduino
upload_speed = 921600
monitor_speed = 115200
upload_protocol = espota
upload_port = WeMosClock.local
monitor_port = socket://WeMosClock.local:23

lib_deps = 
	tzapu/WiFiManager@^0.16.0
	bblanchon/ArduinoJson@^6.18.4
	olikraus/U8g2@^2.28.8

[env:d1_mini]
board = d1_mini

Credits

Majed Abouhatab, P.E.
56 projects • 42 followers
Electronics Professional Engineer, Pilot, Skydiver, Runner, and World Traveler.

Comments