nichollsg
Posts: 6
Joined: Sun Oct 07, 2018 2:26 am

Periodically update DS3231 RTC with NTP

Sun Oct 07, 2018 2:39 am

I am working on a pi hat that will cut the Pi's power in the evening when the pi isn't being used in my application. Anyways, I have a couple questions about using an RTC since it is no guarantee that there will always be an internet connection.

By switching the Pi over to using the RTC for timekeeping, how do I periodically update the RTC time with NTP when the pi recognizes that it has a connection again? I want to ensure that the RTC time doesn't drift too much as this application is expected to last autonomously for ~8-10 years. I have a python class to monitor system attributes like internet connection, so this will be run every morning to check. If it is connected to the internet, I would like to issue a command line argument with Python to write the NTP time to the RTC.

I am trying to figure out how to set an alarm for the DS3231 since the alarm pin will be used as an input to a uC that controls the power to the pi (using ultra low-power uC as glue logic for other things along with delaying when power is pulled to prevent damage to SD card). Is there a way to easily set and clear DS3231 alarm flags from the command line or is it best to just use Python? How do I make sure that these I2C commands don't interfere with the kernel when the system clock is being updated by using the RTC as the hwclock? Is this even an issue that I should care about?

Finally, is anyone aware of a Python library to control the DS3231? I looked around a bit and couldn't find anything besides instructions on how to switch over entirely to an RTC with the command line. Nothing though on updating the RTC time or using alarms with the raspberry pi.

Idahowalker
Posts: 445
Joined: Wed Jan 03, 2018 5:43 pm

Re: Periodically update DS3231 RTC with NTP

Sun Oct 07, 2018 9:49 pm

I use a DS3231 RTC.

I have programmed the RTC to provide a 1PPS interrupt to a Python application.

I recommend that, before the RTC is set to be used by the OS set up the alarms or interrupts before the OS locks you out. Once the RTC has its settings set then add it to the OS.

This link should answer a few of the questions you ask. Do add the battery to the module. Stop at the " I2C Device Setup" and use your favorite programming language to configure the DS213's interrupts and alarms.

I use

Code: Select all

import smbus

bus = smbus.SMBus(1)
address = 0X68
bus.write>byte_data(address, 0x0E, 0x40)
to send configuration commands to the RTC. Remember once the RTC has been handed over to the OS, you will not be allowed to reprogram unless you take the RTC back from the system.

address is the address of the RTC.
The info 0x0E is the address of the register of the RTC you will be writing to, 0x40 is the data you will send to the RTC. Differnt registers will require you to edit the register address and data.

After setting up the RTC, continue on with the instructions.

This is a link to the datasheet https://datasheets.maximintegrated.com/en/ds/DS3231.pdf that provides great info on the registers.

Most of the DS3231 RTC modules come with a small EEPROM that can be used to store data. I use the EEPROM to add data to and read from the EEPROM. You may notice, when you see the address of the RTC during setup, another address show up. I'd suggest you check, before wiring the RTC to the RPi, if any other i2C device is already located on your system.

I have a MPU6050 installed and that MPU 6050 uses, default, the same address as the DS3231. With the MPU and the RTC using the same addres,it creates a mess. By checking beforehand you'll have the heads up on needing to change a I2C address.

Oh, I use 2.something K, I think 2.2K pull-up resistors on the I2C buss, I found, when I was messing with the RTC, that things ran a lot smoother with the RTC after I added the resistors.

As far as keeping system time, works great.
Without knowing why you are deleting my postings, I will not know how...

danjperron
Posts: 3431
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Periodically update DS3231 RTC with NTP

Sun Oct 07, 2018 11:11 pm

I made a class with the ds1307. The ds3231 is very similar to the ds3231. So similar that the driver for the ds3231 is the ds1307!

check my post in french, (use google translate).
https://www.raspberrypi.org/forums/view ... lit=ds1307


If you want to play with the alarm.

1- Wait until RTC time is read from the OS.
2 - Disable the RTC module. (sudo rmmod rtc_ds1307)
3- Access to the RTC module and change the Alarm. With the module removed from the OS you have access to the RTC I.C.
4- Re-enable the RTC module. (sudo modprobe rtc_ds1307).

danjperron
Posts: 3431
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Periodically update DS3231 RTC with NTP

Sun Oct 07, 2018 11:27 pm

For the ntp sync .

maybe you should ping and restart systemd.

Code: Select all

until ping -nq -c3 8.8.8.8; do
   echo "Waiting for network..."
done
sudo systemctl restart systemd-timesyncd

nichollsg
Posts: 6
Joined: Sun Oct 07, 2018 2:26 am

Re: Periodically update DS3231 RTC with NTP

Thu Oct 11, 2018 9:28 pm

danjperron wrote:
Sun Oct 07, 2018 11:27 pm
For the ntp sync .

maybe you should ping and restart systemd.

Code: Select all

until ping -nq -c3 8.8.8.8; do
   echo "Waiting for network..."
done
sudo systemctl restart systemd-timesyncd
Unfortunately, this is not possible. It could be days that the pi does not have a connection, and in this case, our panel would not be tracking the sun if it is simply ignoring its work and going back to sleep while waiting for a connection.

nichollsg
Posts: 6
Joined: Sun Oct 07, 2018 2:26 am

Re: Periodically update DS3231 RTC with NTP

Thu Oct 11, 2018 9:30 pm

danjperron wrote:
Sun Oct 07, 2018 11:11 pm
I made a class with the ds1307. The ds3231 is very similar to the ds3231. So similar that the driver for the ds3231 is the ds1307!

check my post in french, (use google translate).
https://www.raspberrypi.org/forums/view ... lit=ds1307


If you want to play with the alarm.

1- Wait until RTC time is read from the OS.
2 - Disable the RTC module. (sudo rmmod rtc_ds1307)
3- Access to the RTC module and change the Alarm. With the module removed from the OS you have access to the RTC I.C.
4- Re-enable the RTC module. (sudo modprobe rtc_ds1307).
Okay, this might be very useful to me! I have code written already for ~98% of the DS3231 functionality, so this has been answered. Thank you for the reference though! As for step 1, how do I monitor this with Python? I think your suggestion could work very nicely, but I'm just not sure how to monitor whether the RTC has just been read from the OS using Python. Out of curiosity, does modprobe require a system reboot?

danjperron
Posts: 3431
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Periodically update DS3231 RTC with NTP

Thu Oct 11, 2018 10:13 pm

does modprobe require a system reboot?
I do have a DS3231. I just check it and the modprobe works perfectly after Ia rmmod.
No reboot is needed!

For the wait until RTC I'm not sure . I'll check the message log to see if it is displayed!

Idahowalker
Posts: 445
Joined: Wed Jan 03, 2018 5:43 pm

Re: Periodically update DS3231 RTC with NTP

Thu Oct 11, 2018 10:50 pm

The DS3231 is, supposed to be accurate to +- 2min per year. I'd say if this is not accurate enough for a week or two, not connected to the internet, use a GPS module instead.

Something like the Adafruit Ultimate GPS, which has a RTC, just add battery, has a low operating current requirement. I run a Ultimate GPS, with display on, continuously connected to Sats.., and running 4 other sensors, for 30+ hours straight, whiles the Pro Mini does its thing on two series wired 18650 2600mAh batteries.

As soon as the Ultimate GPS is powered on the RTC is available for use as a clock, when the GPS has connected to a few satellites, the RTC is automatically updated. What time the OS or the RPi thinks it is, is irrelevant to your project because your project would be using GPS time as its reference.

So, seriously, if +- 2 minutes per year is way to much of an error, whiles running, for weeks off line, think about going the GPS route. Also, the Adafruit Ultimate GPS Python library is already wrote for you with easy to follow instructions on getting the Ultimate GPS up and running.

1 year = 31536000 seconds / 240 seconds is not really a huge error over a few weeks time, and when connected back to the Internet, the error gets corrected.

As a note, I get 6 Sats. without an external antenna, with the Ultimate GPS sitting downstairs on the living room table, 15+ feet away from a window.
Last edited by Idahowalker on Thu Oct 11, 2018 10:59 pm, edited 1 time in total.
Without knowing why you are deleting my postings, I will not know how...

ericcooper
Posts: 129
Joined: Sat Apr 08, 2017 6:23 pm

Re: Periodically update DS3231 RTC with NTP

Thu Oct 11, 2018 10:51 pm

If the DTS file for your RTC specifies that it's a "wakeup-source", you should have a /sys/class/rtc/rtc0/wakealarm file to which you can write the desired number of seconds until it generates a wakeup alarm. The rtcwake command from the util-linux package may also be useful.

danjperron
Posts: 3431
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Periodically update DS3231 RTC with NTP

Thu Oct 11, 2018 10:59 pm

If the DTS file for your RTC specifies that it's a "wakeup-source", you should have a /sys/class/rtc/rtc0/wakealarm
Interesting but,

Code: Select all

[email protected]:/sys/class/rtc/rtc0 $ ls
date  dev  device  hctosys  max_user_freq  name  power  since_epoch  subsystem  time  uevent
Nope the DTS doesn't have it set.
I think that it is normal. The Interrupt pins is not connected !

danjperron
Posts: 3431
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Periodically update DS3231 RTC with NTP

Thu Oct 11, 2018 11:11 pm

ut I'm just not sure how to monitor whether the RTC has just been read from the OS
Found it

When you insert the module with modprobe, you should restart the time system using
sudo systemctl restart systemd-timesyncd

and you could check if the time has been update using

Code: Select all

[email protected]:~ $ sudo systemctl status systemd-timesyncd
● systemd-timesyncd.service - Network Time Synchronization
   Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/systemd-timesyncd.service.d
           └─disable-with-time-daemon.conf
   Active: active (running) since Thu 2018-10-11 18:47:14 EDT; 24min ago
     Docs: man:systemd-timesyncd.service(8)
 Main PID: 11415 (systemd-timesyn)
   Status: "Synchronized to time server 5.135.3.88:123 (0.debian.pool.ntp.org)."
   CGroup: /system.slice/systemd-timesyncd.service
           └─11415 /lib/systemd/systemd-timesyncd

oct 11 18:47:14 Pi2 systemd[1]: Starting Network Time Synchronization...
oct 11 18:47:14 Pi2 systemd[1]: Started Network Time Synchronization.
oct 11 18:47:14 Pi2 systemd-timesyncd[11415]: Synchronized to time server 5.135.3.88:123 (0.debian.pool.ntp.org).
[email protected]:~ $ 

Killertechno
Posts: 189
Joined: Wed Jan 02, 2013 8:28 am

Re: Periodically update DS3231 RTC with NTP

Mon Dec 03, 2018 3:30 pm

Hi to all, I found this thread so I try to resume my old project due I'm confused about ntp sync and RTC on Raspberry pi.....
I'm using one of latest Raspberry images (April or August 2018, I don't remember).


In my application I use DS3231 (I need its PPS for external hardware) and I need to detect Raspberry reboot.
DS3231 at boot is set to 1 Jan 2000 00:00, I can see traffic on i2c bus, but Raspberry date is set to 3 Nov 2016, so my first question is:

Where does Raspberry save date? I would like date would be read from RTC or simply set to fixed date (maybe the same of RTC).

I need to enable square wave output of DS3231, it's said I need to program DS3231 before OS takes control of RTC.
I set to use DS3231 on config.txt, so to program RTC I need to unload kernel module rtc_ds1307, then program RTC, then reload kernel driver, then restart timing service to make RTC work again with OS: sudo systemctl status systemd-timesyncd.

Is this correct?

If you are asking about periodically update RTC with ntp time, I suppose this is not done by default, so I suppose it's automatically done just at shutdown by OS.
But I periodically see traffic (every about 11 minutes) on i2c bus.

So, who is accessing DS3231?

On ntp configuration file /etc/ntp.conf I set:

Code: Select all

minpoll 6
maxpoll 6
just to have fixed periodic ntp update.
Is it the same disabling ntp service and manually update ntp through script?

Code: Select all

sudo ntpdate 127.4.55.11

Thanks.

Return to “Python”