A Slightly Better NTP Client for the ESP32 (ESPWROOM32) on macOS Catalina

Currently have two ESP8266 modules testing some Blynk apps, whereI'm not so happy with the Blynk business model for developers, but that's another story.

So, with two of my ESP8266s currently "busy", I decided to work on the ESP32, and in particular the ESPWROOM32.

I installed the recommended Arduino ESP32 libs in my Arduino IDE running the 10.15.2 version of Catalina, and I got mixed results with different board managers. In a nutshell, I was having trouble getting anything more than "Hello World" to work.

So, I wrangled with this for a few hours, trying myriad ESP32 board managers; and finally got it working, by following the manual ESP32 for macOS instructions here:

https://github.com/espressif/arduino-esp32/blob/master/docs/arduino-ide/mac.md

This install code worked flawlessly for me:

mkdir -p ~/Documents/Arduino/hardware/espressif && \
cd ~/Documents/Arduino/hardware/espressif && \
git clone https://github.com/espressif/arduino-esp32.git esp32 && \
cd esp32 && \
git submodule update --init --recursive && \
cd tools && \
python get.py 

and I was able to get my "A Slightly Better NTP Client for the ESP32" code working on this ESPWROOM32 using the NodeMCU-32S board manager. No other Arduino ESP32 board manager gave me better results; and I was able to get this NTP client working on the this version of the ESP32:

/*************************************************************
 Slightly Better NTP Client for the ESPWROOM32 v0.1
 Neo www.unix.com 1  Feb 2020
 Use anyway the spirit moves you..... enjoy.
*************************************************************/
#include <NTPClient.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <WiFiUdp.h>
#define MY_TZ_OFFSET_HOURS 7
#define MY_TZ_STRING "Bangkok Time"
const char *ssid     = "YOUR_WIFI_SSID";
const char *password = "YOUR_SECURE_PASSWORD;
bool timeset = false;
const long utcOffsetInSeconds = 3600 * MY_TZ_OFFSET_HOURS;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);

void setup() {
  Serial.begin(115200);

  WiFi.begin(ssid, password);

  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
    Serial.print ( "." );
  }

  timeClient.begin();
}
unsigned long count = 0;
void loop() {
  count++;
  // My ESP32 is faster than my tested ESP8266, so the modulo function is about one to two seconds (delay) . 
  // will do the math on this another time, but it is definitely faster than my ESP8266 module. as it should be.
  int my_modulo_delayer = 7900000;
  if (timeset == false or count % my_modulo_delayer == 0)
  {
    doTime();
  }

}

void doTime() {
  timeClient.update();

  Serial.print(daysOfTheWeek[timeClient.getDay()]);
  Serial.print(", ");
  Serial.print(timeClient.getHours());
  if (timeClient.getMinutes() < 10)
  {
    Serial.print(":0");
  }
  else {
    Serial.print(":");
  }

  Serial.print(timeClient.getMinutes());

  if (timeClient.getSeconds() < 10)
  {
    Serial.print(":0");
  }
  else {
    Serial.print(":");
  }

  Serial.print(timeClient.getSeconds());
  Serial.print(" ");
  Serial.println(MY_TZ_STRING);
  timeset = true;
  //Serial.println(timeClient.getFormattedTime());
}

... and it worked!

22:17:45.016 -> Saturday, 22:17:36 Bangkok Time
22:17:45.016 -> Saturday, 22:17:38 Bangkok Time
22:17:45.016 -> Saturday, 22:17:40 Bangkok Time
22:17:45.016 -> Saturday, 22:17:41 Bangkok Time
22:17:45.016 -> Saturday, 22:17:43 Bangkok Time
22:17:49.984 -> Saturday, 22:17:45 Bangkok Time
22:17:49.984 -> Saturday, 22:17:47 Bangkok Time
22:17:49.984 -> Saturday, 22:17:48 Bangkok Time
22:17:52.303 -> Saturday, 22:17:50 Bangkok Time
22:17:52.932 -> Saturday, 22:17:52 Bangkok Time
22:17:54.683 -> Saturday, 22:17:54 Bangkok Time
22:17:56.441 -> Saturday, 22:17:56 Bangkok Time
22:17:58.177 -> Saturday, 22:17:57 Bangkok Time

But I think I will take a diversion from the ESP modules play around with my RFID module, the RFID-522, and see if I can read some RFID devices I have on my keychain and then attempt to clone them with this $1 to $2 USD RFID board.

1 Like