Page 1 of 2

[Video] ESP32 - DS1307 real time clock

Posted: Sat Feb 11, 2017 4:52 am
by kolban
In this tutorial video we illustrate the attachment of a DS1307 real time clock module to an ESP32 and how to read and write from it using I2C.

https://www.youtube.com/watch?v=irtxFEZPRrg

Re: [Video] ESP32 - DS1307 real time clock

Posted: Mon Feb 13, 2017 3:27 am
by onehorse
This seems like an odd application for the ESP32 since when supplied with a 32kHz crystal it should be capable of time and date keeping with its embedded RTC. Has anyone been able to do so with the Arduino IDE yet? or the SDK?

Re: [Video] ESP32 - DS1307 real time clock

Posted: Mon Feb 13, 2017 3:27 am
by onehorse
This seems like an odd application for the ESP32 since when supplied with a 32kHz crystal it should be capable of time and date keeping with its embedded RTC. Has anyone been able to do so with the Arduino IDE yet? or the SDK?

Re: [Video] ESP32 - DS1307 real time clock

Posted: Mon Feb 13, 2017 5:45 am
by kolban
Howdy,
I fully agree ... a real time clock attached to an ESP32 does seem redundant since the ESP32 has a real time clock in it. However, that said, the DS1307 module can apparently remember time for years on a simple coin cell battery whereas the ESP32 will "forget" the time if the power is removed. If knowing the correct wall clock time was super important, the DS1307 might be a backup mechanism to losing power. Alternatively, if knowing the real time is important and the ESP32 is to be powered on and off as normal operation, the DS1307 might be useful, especially if there is no Internet access for SNTP.

Re: [Video] ESP32 - DS1307 real time clock

Posted: Mon Apr 03, 2017 4:50 pm
by gregstewart90
Great video! I am currently using the same code with a DS1339U-33. Everything is working fine, except how do I set the time of the ESP32 after I read it from the RTC after a boot?

Thanks.

Re: [Video] ESP32 - DS1307 real time clock

Posted: Mon Apr 03, 2017 6:13 pm
by kolban
What we will have to do is read the time from battery backed up RTC and then call "settimeofday". If that isn't enough info (and it does seem pretty short) ... ping back and I'll be delighted to partner with you for a sample.

Neil

Re: [Video] ESP32 - DS1307 real time clock

Posted: Tue Apr 04, 2017 12:06 am
by gregstewart90
I've tried a couple of different ways, but I'm not sure how to get seconds since epoch (1970).

Code: Select all

struct tm tm;
	tm.tm_sec  = bcdToInt(data[0]);
	tm.tm_min  = bcdToInt(data[1]);
	tm.tm_hour = bcdToInt(data[2]);
	tm.tm_mday = bcdToInt(data[4]);
	tm.tm_mon  = bcdToInt(data[5]) - 1; // 0-11 - Note: The month on the DS1307 is 1-12.
	tm.tm_year = bcdToInt(data[6]) + 100; // Years since 1900
	time_t readTime = mktime(&tm);

	struct timeval tv = { .tv_sec = readTime, .tv_usec = 0};
	settimeofday(&tv, NULL);
	
I get a value of 1491262995 for readTime from my RTC. That number, of course, increases with time. I think I'm only missing the final step.

Thanks.

Re: [Video] ESP32 - DS1307 real time clock

Posted: Tue Apr 04, 2017 5:19 am
by kolban
Your number of seconds since the epoch looks right ... see:

https://www.epochconverter.com/

Just a wild guess but what if you change your code to:

Code: Select all

struct timeval tv;
tv .tv_sec = readTime;
tv.tv_usec = 0;
settimeofday(&tv, NULL);
Another dumb question from me .... how do you know its NOT working?

Re: [Video] ESP32 - DS1307 real time clock

Posted: Tue Apr 04, 2017 1:34 pm
by gregstewart90
Good idea, but still no go. Below I have my code. I print the time before and after.

Code: Select all

char buff[100];
	time_t now;
	strftime (buff, 100, "%Y-%m-%dT%H:%M:%S.000", localtime (&now));
	printf("Time at first: %s\n",buff);

	int doWrite = 1;
	ESP_LOGD(tag, ">> ds1307");

	time_t readTime = time(NULL);
	ESP_LOGD(tag, "time: %ld", readTime);
	readTime = readValue();
	ESP_LOGD(tag, "Read from DS1307: %ld", readTime);
	printf("Read: %ld\n", readTime);
	struct timeval tv;
	tv .tv_sec = readTime;
	tv.tv_usec = 0;
	settimeofday(&tv, NULL);
	vTaskDelay(1000/portTICK_PERIOD_MS);

	time_t now_after;
	char buff_after[100];
	strftime (buff_after, 100, "%Y-%m-%dT%H:%M:%S.000", localtime (&now_after));
	printf("Time after read: %s\n",buff_after);
This is the result.

Code: Select all

Time at first: 2004-01-07T05:39:28.000
Read: 1491312072
Time after read: 1970-01-01T00:00:00.000
I also tried hard coding the seconds value, but I got the same results.

Code: Select all

struct timeval tv;
	tv .tv_sec = 1491312078;
	tv.tv_usec = 0;
	settimeofday(&tv, NULL);

Code: Select all

Time at first: 2004-01-07T05:39:28.000
Read: 1491312072
Time after read: 1970-01-01T00:00:00.000

Re: [Video] ESP32 - DS1307 real time clock

Posted: Tue Apr 04, 2017 7:41 pm
by kolban
I think I might have a guess at the problem ... I think you may be calling "localtime" (see https://linux.die.net/man/3/localtime) thinking that it returns the current local time. It doesn't ... it calculates the local time given the current time in UTC as input. I think you need to call gettimeofday() to get the current time and THEN manipulate that time through one of the time functions. To the best of my knowledge, settimeofday() sets the UTC time and gettimeofday() gets the UTC time and all the other time functions convert to different representations, encoding, time zones and other time goodies.