Hi, I am new to Arduino. I got interested in it while searching for a digital clock that would display both local time and UTC. Since I am really into amateur radio such a clock would come in handy. Eventually I happened upon Bruce Hall's (W8BH) Arduino based GPS clock. His clock used Adafruit modules and a homebrewed LED display that by his own admission chomped electricity like crazy.
So I decided to go simple. This was around a decade ago so I bought Velleman modules at Fry's (don't y'all miss Fry's?) and a 20 character LCD display from China off Ebay. Then life intervened, as it tends to do.
Over the years I've been copypasta'ing segments of code and drivers from various places on the net, and I finally have something that might work. I'd like to post the code (in .txt at the moment) to the forums to have experienced eyes look it over. I would like guidance with the code, as well as with the use of Velleman Arduino modules. Where should I start posting? Help appreciated.
Nothing else to do, so I wrote some code for an ESP32C3 (from Seeed).
Displays UTC time and local time in the serial monitor.
Just enter your WiFi credentials, time zone string (link in the code) and local pool.
Let me know if you need help with that (need to know where you live).
The prints can also have year/month/date etc included. See this page.
Works of course only within range of WiFi.
Leo..
#include <WiFi.h> // ESP32
#include "esp_sntp.h"
unsigned long prevTime;
bool reSync;
time_t now; // epoch
tm tm; // time struct
void cbSyncTime(struct timeval *tv) { // sync callback
Serial.println("Time was updated");
reSync = true;
}
void setup() {
Serial.begin(115200);
WiFi.begin("MySSID", "MyPass"); // WiFi credentials
sntp_set_time_sync_notification_cb(cbSyncTime); // enable callback
configTzTime("NZST-12NZDT,M9.5.0,M4.1.0/3", "nz.pool.ntp.org"); // https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
Serial.println("Waiting for time sync");
while (!reSync) yield();
}
void loop() {
time(&now);
if (now != prevTime) { // if time has changed
prevTime = now; // remember
gmtime_r(&now, &tm); // get epoch
printf("\nUTC: %02u:%02u:%02u", tm.tm_hour, tm.tm_min, tm.tm_sec); // print UTC
localtime_r(&now, &tm); // convert epoch to local time
printf("\tLocal: %02u:%02u:%02u", tm.tm_hour, tm.tm_min, tm.tm_sec); // print local
}
}