ESP32 NTP Local AM-PM Time Display

ESP32 NTP Local AM-PM Time Display…

Just the other day I was asked for a way to display local time (am/pm) on an OLED…

It’s quite easy to achieve using a couple of include code libraries to format DateTime from the NTP server.

So lets have a go at displaying am/pm time using the esp32… it may not be elegant, but it does show the time!

Hardware

We have a ESP32 development board; I am using a LOLIN32 D32 purchased for Phipps Electronics. Nice board every thing you need LiIon/LiPo battery charging circuit and fits a normal breadboard with a row of holes on both sides; for around $20 AUD, not bad!

Hopefully you can see by the pic’s, if you’re careful about soldering the headers you get the pin definitions showing on top of the board. Always disable in my opinion.

Note: I have no affiliation with Phipps Electronics, dealt with them a few times, like the stuff they handle and the service is second to none.

A standard 0.95″ SSD1306 OLED, in this case from my bits box, a breadboard with jumper wires and a 2500mAh Power Bank for portable power completes the hardware.

Wiring

SCL pin from the OLED goes to GPIO 22 on the ESP32
SDA pin from the OLED goes to GPIO21 on the ESP32
VCC in this case is 3.3v
GND goes to GND

Code

Copy this source code listing to a new project in your Arduino-IDE after setting the type of ESP32 board you are using and which COM port it is connected to. Save as whatever file name you like. I used the very imaginative name of esp32-local-time.ino.

Remember to Edit the SSID and Password.

Set the DateTime.setTimeZone(+10) value ie the +10 to your time zone offset

Here is a link to the time zones timezones

Here is the code.

/**
 * Based on code example in ESPDateTime library
 * Simple Mods by Al McDivitt www.iotXplain.com.au
 * ESPDateTime Library
 * https://github.com/mcxiaoke/ESPDateTime
 * SSD1306  Library
 *https://github.com/ThingPulse/esp8266-oled-ssd1306
 */
#include <WiFi.h>

#include "ESPDateTime.h" //https://github.com/mcxiaoke/ESPDateTime

// don't forget to change this to real ssid/password
// or set these in config.h and include it

#define WIFI_SSID "YOUR SSID"
#define WIFI_PASS "YOUR PASSWORD"


#include <Wire.h>
#include "SSD1306.h" //https://github.com/ThingPulse/esp8266-oled-ssd1306
 
SSD1306  display(0x3c, 21, 22);

unsigned long lastMs = 0;
unsigned long ms = millis();

void setupWiFi() {
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  Serial.println(millis());
  Serial.print("WiFi Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
}

void setupDateTime() {
  // setup this after wifi connected
  // you can use custom timeZone,server and timeout
  // DateTime.setTimeZone(-4);
  //   DateTime.setServer("asia.pool.ntp.org");
  //   DateTime.begin(15 * 1000);

  //********************************************
  DateTime.setTimeZone(+10); // note +10 is UTC +10 for Brisbane Australia.

  // set above for your time zone as per list
  // https://www.zeitverschiebung.net/en/all-time-zones.html
  //*************************************************************
  DateTime.begin();
  if (!DateTime.isTimeValid()) {
    Serial.println("Failed to get time from server.");
  }
}


void setup() {
  delay(1000);
  Serial.begin(115200);
  setupWiFi();
  setupDateTime();


  display.init();
  display.flipScreenVertically();
  display.setFont(ArialMT_Plain_16);
  display.drawString(23, 15, "iotXplain");
  display.drawString(18, 30, "Connecting.");
  display.display();

  delay(2000);
  time_t t = DateTime.now();
  
  Serial.println(DateFormatter::format("Now it's %F %I:%M%p.", t));
}

void loop() {
 
if (millis() - ms >5000) { //display 5000 ms  note 1 minute = 60000 ms
    ms = millis();
    Serial.println("--------------------");
    if (!DateTime.isTimeValid()) {
      Serial.println("Failed to get time from server, retry.");
      DateTime.begin();
    } else {
      time_t t = DateTime.now();
    Serial.println(DateFormatter::format("%I:%M%p.", t));
  display.init();
  display.flipScreenVertically();
  display.setFont(ArialMT_Plain_24);
  display.drawString(21, 15, DateFormatter::format("%I:%M%p.", t));
  display.display();
    }
  }
}

Now, with the hardware attached to your computer all you need to do is compile and upload to the device. Give it a second or two and you should see the connecting message, then the local time displayed on the OLED Display.

I’ll go over a quick step by step on adding a library to the Arduino-IDE from a downloaded zip file, just in case some may have not done it before. You will need to add two library’s to make this project work.

Installing a Arduino-IDE library from .zip file

Oh no an error, what will I do?! (See mom those drama classes did come in handy).

We check and find we need the ESPDateTime Library, get it’s address on github.com and go there.

We download the zip file to a folder for later installation.

Select Sketch -> Include Library -> Add .ZIP Library..

Naturally we select the .zip file downloaded earlier.

We should get the Library added message.

Then recompile our code.

If we get the -> Done Compiling message. we are good to go!

We can upload our working code to the device, easy.

And that’s all there is to it…

… hope you had some fun building a IOT time piece for the desk.

Cheer’s Al