[Video] ESP32 - DS1307 real time clock

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

[Video] ESP32 - DS1307 real time clock

Postby kolban » Sat Feb 11, 2017 4:52 am

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
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

onehorse
Posts: 70
Joined: Mon Feb 15, 2016 1:35 am

Re: [Video] ESP32 - DS1307 real time clock

Postby onehorse » Mon Feb 13, 2017 3:27 am

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?

onehorse
Posts: 70
Joined: Mon Feb 15, 2016 1:35 am

Re: [Video] ESP32 - DS1307 real time clock

Postby onehorse » Mon Feb 13, 2017 3:27 am

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?

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: [Video] ESP32 - DS1307 real time clock

Postby kolban » Mon Feb 13, 2017 5:45 am

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.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

gregstewart90
Posts: 59
Joined: Thu Jan 19, 2017 5:17 pm

Re: [Video] ESP32 - DS1307 real time clock

Postby gregstewart90 » Mon Apr 03, 2017 4:50 pm

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.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: [Video] ESP32 - DS1307 real time clock

Postby kolban » Mon Apr 03, 2017 6:13 pm

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
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

gregstewart90
Posts: 59
Joined: Thu Jan 19, 2017 5:17 pm

Re: [Video] ESP32 - DS1307 real time clock

Postby gregstewart90 » Tue Apr 04, 2017 12:06 am

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.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: [Video] ESP32 - DS1307 real time clock

Postby kolban » Tue Apr 04, 2017 5:19 am

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?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

gregstewart90
Posts: 59
Joined: Thu Jan 19, 2017 5:17 pm

Re: [Video] ESP32 - DS1307 real time clock

Postby gregstewart90 » Tue Apr 04, 2017 1:34 pm

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

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: [Video] ESP32 - DS1307 real time clock

Postby kolban » Tue Apr 04, 2017 7:41 pm

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.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

Who is online

Users browsing this forum: No registered users and 29 guests