ntp +2h issue

This commit is contained in:
2023-09-17 12:12:48 +02:00
parent 17e974f0a1
commit 3012bdd3be
9 changed files with 1381 additions and 805 deletions

View File

@@ -1,80 +1,68 @@
#include "clock.h"
int RTC_hours, RTC_minutes, RTC_seconds, RTC_day, RTC_month, RTC_year, RTC_day_of_week;
bool RTC_present;
bool NTPinit = false;
// Central European Time (Frankfurt, Paris)
TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120}; // Central European Summer Time
TimeChangeRule CET = {"CET ", Last, Sun, Oct, 3, 60}; // Central European Standard Time
Timezone myTZ(CEST, CET);
const char *ssid = "iot";
const char *password = "Rijnstraat214";
TimeChangeRule *tcr;
void getRTCTime()
bool timeIsSet()
{
// RTC_seconds = timeClient.getSeconds();
// RTC_minutes = timeClient.getMinutes();
// RTC_hours = timeClient.getHours();
// RTC_day_of_week = 1
// RTC_day = timeClient.getDay();
// RTC_month = timeClient.
// RTC_year =
return RTC_present;
}
void printDateTime(time_t t, const char *tz)
{
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
//setup NTP
timeClient.begin();
char buf[32];
char m[4]; // temporary storage for month string (DateStrings.cpp uses shared buffer)
strcpy(m, monthShortStr(month(t)));
sprintf(buf, "%.2d:%.2d:%.2d %s %.2d %s %d %s",
hour(t), minute(t), second(t), dayShortStr(weekday(t)), day(t), m, year(t), tz);
Serial.println(buf);
}
time_t getLocalTime()
{
time_t utc = getNtpTime();
time_t local = myTZ.toLocal(utc, &tcr);
printDateTime(local,tcr -> abbrev);
printDateTime(utc,"UTC");
getRTCTime();
byte prevSeconds=RTC_seconds;
unsigned long RTC_ReadingStartTime=millis();
RTC_present=true;
while(prevSeconds==RTC_seconds)
{
getRTCTime();
//Serial.println(RTC_seconds);
if ((millis()-RTC_ReadingStartTime)>3000)
{
Serial.println(F("Warning! RTC DON'T RESPOND!"));
RTC_present=false;
break;
}
}
setTime(RTC_hours, RTC_minutes, RTC_seconds, RTC_day, RTC_month, RTC_year);
return local;
}
void initClock()
{
initNTP();
}
void loopClock()
{
timeClient.update();
// if (((millis()%60000)==0)&&(RTC_present)) //synchronize with RTC every 10 seconds
// {
// getRTCTime();
// setTime(RTC_hours, RTC_minutes, RTC_seconds, RTC_day, RTC_month, RTC_year);
// Serial.println("sync:");
// Serial.println(RTC_seconds);
// }
if(getWifiStatus())
{
if(!NTPinit && getWifiStatus())
{
setSyncProvider(getLocalTime);
setSyncInterval(300);
NTPinit = true;
}
RTC_present = timeStatus ? timeSet: true, false;
}
else
{
RTC_present = false;
}
}
String getTimeNow()
{
if (value[hModeValueIndex]==24) return PreZero(tijd.getHour())+PreZero(tijd.getMinute())+PreZero(tijd.getSecond());
else return PreZero(tijd.getHour())+PreZero(tijd.getMinute())+PreZero(tijd.getSecond());
{
return PreZero(hour())+PreZero(minute())+PreZero(second());
}
String updateDateString()
{
static unsigned long lastTimeDateUpdate=millis();
static String DateString=PreZero(tijd.getDay())+PreZero(tijd.getMonth())+PreZero(tijd.getYear()%1000);
if ((millis()-lastTimeDateUpdate)>1000)
{
lastTimeDateUpdate=millis();
DateString=PreZero(tijd.getDay())+PreZero(tijd.getMonth())+PreZero(tijd.getYear()%1000);
}
return DateString;
}
return PreZero(day())+PreZero(month())+PreZero(year()%1000);
}