Hi everyone. I'm running the TFT eSPI anti-aliased clock example on a round display using a Xiao ESP32C3 and I was wondering though how I can get it to display my local time. It defaults to UTC time. Under the 'NTP_Time_h' tab I changed line 34 from #define TIMEZONE UK to #define TIMEZONE usET but nothing changes. Thanks in advance for any help!
try:
#define TIMEZONE -5 // Eastern Time
Z
Thanks for the suggestion, but it didn't work. It still shows UTC. I've added some code to correct the time, but that kind of defeats the purpose of the timezone libraries!
What I miss in the code is the converting from UTC to local time.
See this for example.
If you're using an ESP32, then you don't need do all the messing around with UDP and handling the NTP transactions yourself like that example does. You don't need the Timezone library either. ESP32 can handle all that for your and it supports the standard POSIX time functions. I configure the time and NTP updates using code like below. There are a lot of diagnostic prints that you could leave out.
#include "Arduino.h"
#include <WiFi.h>
#include "esp_sntp.h"
void handleWifiEvent(arduino_event_id_t event, arduino_event_info_t info);
tm *timeinfo;
char timeString[100] = "Local Time";
time_t now;
void setup() {
const char ssid[] = "xxxxxxxx";
const char password[] = "yyyyyyy";
const char ntpServer1[] = "time.nist.gov";
const char ntpServer2[] = "pool.ntp.org";
const long gmtOffsetSec = -5 * 3600L;
const int daylightOffsetSec = 3600;
const uint32_t syncInterval = 3600UL * 1000;
timeval tv;
uint8_t retryTest = 0;
Serial.begin(115200);
vTaskDelay(5000);
log_i("Station MAC Address: %s", WiFi.macAddress().c_str());
WiFi.onEvent(handleWifiEvent);
while (WiFi.status() != WL_CONNECTED) {
if (retryTest == 0) {
WiFi.disconnect();
WiFi.begin(ssid, password);
retryTest = 5;
} else {
log_i(".");
retryTest--;
vTaskDelay(1000);
}
}
//init and get the time
sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH);
sntp_set_sync_interval(syncInterval);
configTime(gmtOffsetSec, daylightOffsetSec, ntpServer1, ntpServer2);
retryTest = 0;
while (true) { // Wait for NTP sync to take effect
if (retryTest == 0) {
log_i("Waiting for Time Sync");
sntp_sync_time(&tv);
retryTest = 30;
} else {
log_i(".");
retryTest--;
}
vTaskDelay(2000);
now = time(nullptr);
timeinfo = localtime(&now);
if (timeinfo->tm_year >= (2022 - 1900)) {
break;
}
}
sntp_sync_status_t syncStatus = sntp_get_sync_status();
switch (syncStatus) {
case SNTP_SYNC_STATUS_RESET:
log_i("SNTP_SYNC_STATUS_RESET");
break;
case SNTP_SYNC_STATUS_COMPLETED:
log_i("SNTP_SYNC_STATUS_COMPLETED");
break;
case SNTP_SYNC_STATUS_IN_PROGRESS:
log_i("SNTP_SYNC_STATUS_IN_PROGRESS");
break;
default:
log_e("Unknown Sync Status");
break;
}
sntp_sync_mode_t mode = sntp_get_sync_mode();
switch (mode) {
case SNTP_SYNC_MODE_IMMED:
log_i("SNTP_SYNC_MODE_IMMED");
break;
case SNTP_SYNC_MODE_SMOOTH:
log_i("SNTP_SYNC_MODE_SMOOTH");
break;
default:
log_e("Unknown Sync Mode");
break;
}
uint32_t interval = sntp_get_sync_interval();
log_i("NTP Sync Interval = %d", interval);
strftime(timeString, 100, "%A, %B %d %Y %H:%M:%S", timeinfo);
log_i("Local Time: %s", timeString);
}
void IRAM_ATTR handleWifiEvent(arduino_event_id_t event_id, arduino_event_info_t info) {
switch (event_id) {
case ARDUINO_EVENT_WIFI_READY:
log_i("WiFi Ready");
break;
case ARDUINO_EVENT_WIFI_STA_START:
log_i("WiFi Start");
break;
case ARDUINO_EVENT_WIFI_STA_STOP:
log_i("WiFi Stop");
break;
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
log_i("Connected to SSID: %s", reinterpret_cast<char*>(info.wifi_sta_connected.ssid));
break;
case ARDUINO_EVENT_WIFI_STA_GOT_IP: {
uint8_t bytes[4];
uint32_t ipAddress = info.got_ip.ip_info.ip.addr;
for (uint8_t i = 0; i < 4; i++) {
bytes[i] = ipAddress & 0xFF;
ipAddress >>= 8;
}
log_i("Got IP Address: %d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3]);
break;
}
case ARDUINO_EVENT_WIFI_STA_LOST_IP:
log_i("IP Address Lost");
break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
log_i("Disconnected From SSID: %s, Reason: %d", reinterpret_cast<char*>(info.wifi_sta_disconnected.ssid),
info.wifi_sta_disconnected.reason);
break;
default:
log_i("WiFi Event: %d", static_cast<uint8_t>(event_id));
break;
}
}
void loop() {
vTaskDelay(1000);
now = time(nullptr);
timeinfo = localtime(&now);
strftime(timeString, 100, "%A, %B %d %Y %H:%M:%S", timeinfo);
log_i("Local Time: %s", timeString);
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.