[Video] ESP32 - DS1307 real time clock
[Video] ESP32 - DS1307 real time clock
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
https://www.youtube.com/watch?v=irtxFEZPRrg
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32
Re: [Video] ESP32 - DS1307 real time clock
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
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
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.
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
-
- Posts: 59
- Joined: Thu Jan 19, 2017 5:17 pm
Re: [Video] ESP32 - DS1307 real time clock
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.
Thanks.
Re: [Video] ESP32 - DS1307 real time clock
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
Neil
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32
-
- Posts: 59
- Joined: Thu Jan 19, 2017 5:17 pm
Re: [Video] ESP32 - DS1307 real time clock
I've tried a couple of different ways, but I'm not sure how to get seconds since epoch (1970).
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.
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);
Thanks.
Re: [Video] ESP32 - DS1307 real time clock
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:
Another dumb question from me .... how do you know its NOT working?
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);
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32
-
- Posts: 59
- Joined: Thu Jan 19, 2017 5:17 pm
Re: [Video] ESP32 - DS1307 real time clock
Good idea, but still no go. Below I have my code. I print the time before and after.
This is the result.
I also tried hard coding the seconds value, but I got the same results.
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);
Code: Select all
Time at first: 2004-01-07T05:39:28.000
Read: 1491312072
Time after read: 1970-01-01T00:00:00.000
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
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 9 guests