Hi everyone, I am making a weather station, which has a feature of displaying time on the LCD.
#include <ESP32Servo.h>
#include <WiFi.h>
#include "time.h"
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x20, 16, 2);
Servo servo_1;
const int button_1_pin = 25;
int button_1_state = 0;
const int servo_1_pin = 26;
const char* ssid = "NOVA_0A50";
const char* password = "*****";
const char* ntp_server = "at.pool.ntp.org";
const long utc_offset = 28000;
const int daylight_offset = 0;
unsigned long previous_millis = 0;
const long interval = 13000;
void setup()
{
Serial.begin(115200);
servo_1.attach(servo_1_pin);
pinMode(button_1_pin, INPUT_PULLUP);
lcd.begin();
connectWiFi();
configTime(utc_offset, daylight_offset, ntp_server);
}
void connectWiFi()
{
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("WiFi Connected");
}
void getlocaltime()
{
struct tm timeinfo;
if(!getLocalTime(&timeinfo))
{
Serial.println("Failed to obtain time");
return;
}
const char* monthNames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
String formatted_time = String(monthNames[timeinfo.tm_mon]) + " " +
String(timeinfo.tm_mday) + " " +
String(timeinfo.tm_hour) + ":" +
String(timeinfo.tm_min) + ":" +
String(timeinfo.tm_sec);
Serial.print(formatted_time);
lcd.setCursor(0,0);
lcd.print(formatted_time);
}
void rotateservo()
{
button_1_state = digitalRead(button_1_pin);
if (button_1_state == HIGH)
{
Serial.println("Pressed");
servo_1.write(0);
delay(1000);
servo_1.write(90);
}
}
void loop()
{
unsigned long current_millis = millis();
if (current_millis - previous_millis >= interval)
{
previous_millis = current_millis;
getlocaltime();
}
rotateservo();
}
I am using an ESP32, and it works great, but for whatever reason, the time has around 13 minutes delay to the local time. Is there any problem with my code? Thanks in advance.
You have the offset from UTC set to 28000 seconds, which is 7 hours 46 minutes 40 seconds.
Are you sure that you have set the offset from UTC correctly ?
Maybe the utc_offset?
If the offset is 8 hours, then it should be 28800, not 28000.
Oh, silly me.... it should be 28800... Thanks for the reply
Yeah I made a typo, already fixed it, thanks for the reply
Yeah I am too blind to see it, thanks for the reply
PaulRB
November 24, 2024, 12:33pm
8
Don't do it this way, there is a better, less error-prone way which also deals with daylight savings time automatically (if you have it in your country).
configTime(0, 0, ntp_server); // 0, 0 because we will use TZ in the next line
setenv("TZ", "XXX", 1); // Set environment variable with your time zone
tzset();
You can get the value for XXX for your country/city from here:
"Africa/Abidjan","GMT0"
"Africa/Accra","GMT0"
"Africa/Addis_Ababa","EAT-3"
"Africa/Algiers","CET-1"
"Africa/Asmara","EAT-3"
"Africa/Bamako","GMT0"
"Africa/Bangui","WAT-1"
"Africa/Banjul","GMT0"
"Africa/Bissau","GMT0"
"Africa/Blantyre","CAT-2"
"Africa/Brazzaville","WAT-1"
"Africa/Bujumbura","CAT-2"
"Africa/Cairo","EET-2EEST,M4.5.5/0,M10.5.4/24"
"Africa/Casablanca","<+01>-1"
"Africa/Ceuta","CET-1CEST,M3.5.0,M10.5.0/3"
"Africa/Conakry","GMT0"
"Africa/Dakar","GMT0"
"Africa/Dar_es_Salaam","EAT-3"
"Africa/Djibouti","EAT-3"
"Africa/Douala","WAT-1"
This file has been truncated. show original
For example, my timezone is Europe/London, so I use "GMT0BST,M3.5.0/1,M10.5.0"
Wawa
November 24, 2024, 10:29pm
9
Your code shows how it used to be done. You don't need the time.h library, and references to it any more. Because it's now all built into the latest ESP core.
See this page.
configTime(0, 0, ntp_server);
setenv("TZ", "XXX", 1);
tzset();
can be replaced with a single line. This also takes care of daylight savings.
configTzTime("CET-1CEST,M3.5.0,M10.5.0/3", "at.pool.ntp.org"); // Austria
Basic sketch to play with.
#include <WiFi.h> // ESP32
unsigned long prevTime;
time_t now; // holds epoch
tm tm; // time struct
void setup() {
Serial.begin(115200);
WiFi.begin("NOVA_0A50", "*****"); // WiFi credentials
configTzTime("CET-1CEST,M3.5.0,M10.5.0/3", "at.pool.ntp.org"); // Austria
}
void loop() {
time(&now);
if (now != prevTime) { // if time has changed
prevTime = now; // remember
localtime_r(&now, &tm); // convert epoch to local time
printf("%02u:%02u:%02u\n", tm.tm_hour, tm.tm_min, tm.tm_sec);
}
}
NTP automatically updates every 3 hours.
Leo..
2 Likes