[Video] ESP32 - DS1307 real time clock

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

Re: [Video] ESP32 - DS1307 real time clock

Postby gregstewart90 » Fri Apr 14, 2017 6:26 pm

Got it working! Thanks Kolban for your help. My problem was with localtime(). Before using it, you have to call time().

Code: Select all

time_t now_after;
time ( &now_after );
char buff_after[100];
strftime (buff_after, 100, "%Y-%m-%dT%H:%M:%S.000", localtime (&now_after));
I do have one problem. Sometimes the reads are okay and other times I get an error on i2c_master_cmd_begin(). The error is ESP_ERR_TIMEOUT. It will work for a long time and then on a reboot wont work. Eventually it starts working again after several reboots. Kolban I am using your code for the DS1307. I found this https://github.com/espressif/esp-idf/issues/422, but the solution didn't help.

Thoughts?

tbnobody
Posts: 3
Joined: Mon Aug 14, 2017 12:18 am

Re: [Video] ESP32 - DS1307 real time clock

Postby tbnobody » Mon Aug 14, 2017 12:32 am

I am having exactly the same problem. I am using a DS3231 (which is nearly compatible with the DS1307) but I cannot read anything using the ESP-IDF. If I use the Arduino environment (as a component in an IDF project) everything works fine.
The arduino code which I am using is from this site: http://tronixstuff.com/2014/12/01/tutor ... h-arduino/

The IDF code which I am using is from here: https://github.com/nkolban/esp32-snippe ... c/ds1307.c
I am able to write the time but reading just gives me a timeout error:

Code: Select all

D (1108) ds3231: >> ds1307
D (1111) ds3231: >> writeValue: 1502670328
D (1115) ds3231:  - Mon Aug 14 00:25:28 2017

E (2120) ds3231: i2c_master_cmd_begin: 263
D (2120) ds3231: time: 14
ESP_ERROR_CHECK failed: esp_err_t 0x107 at 0x400e3aec

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 » Thu Aug 17, 2017 3:28 am

I've always needed a logic analyzer (about $5-$10 on ebay) to debug I2C or SPI issues. Looking at the logic train on the wire is (for me) the only way to debug these kinds of issues.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

musabaybek
Posts: 3
Joined: Sun Aug 04, 2019 11:17 pm

Re: [Video] ESP32 - DS1307 real time clock

Postby musabaybek » Sat Oct 12, 2019 4:04 pm

The code that is provided here https://github.com/nkolban/esp32-snippe ... c/ds1307.c does not have make file, however I tried to create one and also component.mk files. But I face some errors like; "fatal error: errorhandle_func.h: No such file or directory"
I'm new to esp32 and idf based coding... Can you please help?

cndgeek
Posts: 3
Joined: Sun Jul 17, 2022 1:27 pm

Re: [Video] ESP32 - DS1307 real time clock

Postby cndgeek » Sun Jul 17, 2022 1:29 pm

So much confusion in all the above!!!

This should help everyone sort stuff out:
  1. /* Constructor for the RTC on the logging shield. */
  2. DFRobot_DS1307 DS1307;
  3. char daynames[]="SUN\0MON\0TUE\0WED\0THU\0FRI\0SAT\0";
  4.  
  5. // Set the ESP32 time from the RTC.
  6. bool StartRTC() { // See http://www.rinkydinkelectronics.com/resource/DS1307/DS1307.pdf
  7.   if( !(DS1307.begin()) ){
  8.     Serial.println(F("Communication with DS1307 RTC device failed"));
  9.     return 1;
  10.   }
  11.   struct dsTimeStruct {
  12.     uint16_t tm_sec;         /* seconds,  range 0 to 59          */
  13.     uint16_t tm_min;         /* minutes, range 0 to 59           */
  14.     uint16_t tm_hour;        /* hours, range 0 to 23             */
  15.     uint16_t tm_wday;        /* day of the week, range 0 to 6    */              
  16.     uint16_t tm_mday;        /* day of the month, range 1 to 31  */
  17.     uint16_t tm_mon;         /* month, range 1 to 12 (not 0 to 11!) */
  18.     uint16_t tm_year;        /* The number of years since 1900   */
  19.     uint16_t tm_yday;        /* day in the year, range 0 to 365  */
  20.     uint16_t tm_isdst;       /* daylight saving time             */
  21.   } dstm ={0};
  22.  
  23.   DS1307.getTime((uint16_t *)&dstm); //dstm.tm_mon--; // The month on the DS1307 is 1-12.
  24.  
  25.   Serial.printf("DS1307 time: %s %4d/%02d/%02d %02d:%02d:%02d UTC\r\n", // DS1307 time: SUN 2022/07/17 13:06:10 UTC
  26.   &daynames[dstm.tm_wday*4], //Weekday  &daynames[(getTimeBuff[3])*4], //Weekday  [3]
  27.             dstm.tm_year, // Year [6]
  28.             dstm.tm_mon,  // Month [5]
  29.             dstm.tm_mday, // Day [4]
  30.             dstm.tm_hour, // Hours  dstm.dsRaw[2]
  31.             dstm.tm_min,  // Mins   dstm.dsRaw[1]
  32.             dstm.tm_sec   // seconds dstm.dsRaw[0]
  33.             );
  34.  
  35.  
  36.   time_t seconds;
  37.   time(&seconds); // Put the time into seconds
  38.   struct tm *esptm;
  39.   esptm=gmtime(&seconds);
  40.   Serial.printf("ESP32 internal time: %s",asctime(esptm)); // ESP32 internal time: Thu Jan  1 00:00:00 1970
  41.  
  42.  
  43.   //DS1307 format is not same as linux ( they put the tm_wday in the middle, and don't have yday and isdst).
  44.   struct tm newdstm;  newdstm.tm_sec =dstm.tm_sec; newdstm.tm_min =dstm.tm_min; newdstm.tm_hour=dstm.tm_hour;
  45.   newdstm.tm_wday=dstm.tm_wday; newdstm.tm_mday=dstm.tm_mday; newdstm.tm_mon =dstm.tm_mon-1; newdstm.tm_year=dstm.tm_year-1900;
  46.   // Serial.printf("newdstm time: %s",asctime(&newdstm)); // newdstm time: Mon Jul 17 13:09:32 2022
  47.   struct timeval dstv = { .tv_sec = mktime(&newdstm), .tv_usec = 0}; // &dstm.tm_ds
  48.   settimeofday(&dstv, NULL);  
  49.  
  50.   time(&seconds); // Put the updated-above time into seconds
  51.   esptm=gmtime(&seconds); // Convert epoc-based to D/M/Y...
  52.   Serial.printf("new ESP32 internal time: %s",asctime(esptm)); // new ESP32 internal time: Sun Jul 17 13:14:29 2022
  53.  
  54.   return 0;
  55. } // StartRTC

Who is online

Users browsing this forum: No registered users and 22 guests