Site icon Renzo Mischianti

Applying TImeZone functionality (ESP8266 Ntp )

Hello! I have the example working from the webpage working just fine. However, I am struggling to figure out how to apply the newly correct time to the system clock... I am trying to get this newly formated time into my sketch, but no luck. ... Assume same code up to void loop... I just don't think I understand how to apply the tz.toLocal method on a time object. (See time_t current_time() ) My serial monitor output: 02/02/2021 15:19:15 MyCrummyCode:23:19:15 2.2.2021 Tuesday   


void loop() {
// I print the time from local clock but first I check DST
// to add hours if needed
	Serial.println(getEpochStringByParams(usPT.toLocal(now())));
	Serial.print("MyCrummyCode:");
	digitalClockDisplay();

	delay(1000);
}

tmElements_t tm;

time_t current_time() {
	return make_unixtime(usPT.toLocal(second()), usPT.toLocal(minute()),
			usPT.toLocal(hour()), usPT.toLocal(day()), usPT.toLocal(month()),
			usPT.toLocal(year()));
}

time_t make_unixtime(uint8_t sec, uint8_t mn, uint8_t hr, uint8_t dy,
		uint8_t mon, uint16_t yr) {
	tm.Second = sec;
	tm.Hour = hr;
	tm.Minute = mn;
	tm.Day = dy;
	tm.Month = mon;
	tm.Year = yr - 1970;
	return makeTime(tm);
}

void digitalClockDisplay() {
// digital clock display of the time
	if (DEBUG) {
		yield();
		Serial.print(hour());
		yield();
		printDigits(minute());
		yield();
		printDigits(second());
		yield();
		Serial.print(" ");
		yield();
		Serial.print(day());
		yield();
		Serial.print(".");
		yield();
		Serial.print(month());
		yield();
		Serial.print(".");
		yield();
		Serial.print(year());
		yield();
		Serial.print(" ");
		yield();
		Serial.println(dayStr(weekday()));
	}
}

void printDigits(int digits) {
// utility for digital clock display: prints preceding colon and leading 0
	yield();
	Serial.print(":");
	if (digits & lt; 10)
		Serial.print('0');
	yield();
	Serial.print(digits);
}

 
Exit mobile version