Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script to auto-set local time on reboot/daily with timedatectl #6

Open
jezdez opened this issue Aug 1, 2019 · 3 comments
Open
Assignees
Labels

Comments

@jezdez
Copy link

jezdez commented Aug 1, 2019

Hey, so maybe I'm doing this wrong, but I wondered how to use the RV3028 to have my Raspi auto-set the time on boot using the RTC and also handle the case in which it doesn't have network connection to an optionally configured NTP server.

Here's the script I use for this (via /etc/crontab), does that make any sense?

#!/usr/bin/env python3
"""
A script to run on reboot to automatically set the system time to
the RTC time. Will disable Automatic time synchronization while running
(if enabled) and re-enable it again. This is to ensure that setting from RTC
works without network connection.

Add to the system crontab:

$ sudo nano /etc/crontab

And then add:

@daily root python3 /path/to/timedatectl-set-time-from-rtc.py

(c) Jannis Leidel 2019
"""
import time
from subprocess import run

import rv3028

TIMEDATECTL = "/usr/bin/timedatectl"

# Create RV3028 instance
rtc = rv3028.RV3028()

# Switches RTC to backup battery if VCC goes below 2V
# Other settings: 'switchover_disabled', 'direct_switching_mode', 'standby_mode'
rtc.set_battery_switchover('level_switching_mode')

rtc_time = rtc.get_time_and_date()
iso_rtc_time = rtc_time.isoformat(" ")
print("The RTC time is: {}".format(iso_rtc_time))

# Disabling NTP now so we can set the time manually
# in case network connection isn't there right now
timedatectl_output = run([TIMEDATECTL], capture_output=True)
ntp_enabled = b"NTP service: active" in timedatectl_output.stdout
if ntp_enabled:
    run([TIMEDATECTL, "set-ntp", "false"])

time.sleep(1)

try:
    # run timedatectl set-time with the time returned from the RTC
    # The time may be specified in the format "2012-10-30 18:17:16".
    run([TIMEDATECTL, "set-time", iso_rtc_time])
finally:
    print("Successfully set local time using the RV3028.")
    # definitely re-enable NTP in case something goes wrong with setting
    # the time.
    if ntp_enabled:
        run([TIMEDATECTL, "set-ntp", "true"])
@jezdez jezdez changed the title Add to auto-set local time on reboot/daily with timedatectl Add script to auto-set local time on reboot/daily with timedatectl Aug 1, 2019
@Gadgetoid Gadgetoid self-assigned this Aug 20, 2019
@Gadgetoid
Copy link
Member

This seems sensible to me. Is it working for you?

It wont handle your ntp case, but you might have some luck configuring the rtc-rv3028 kernel module to handle this. I'd totally forgotten that I'd submitted an upstream patch for battery switchover support, but just noticed that the module is available on my Pi 4 with kernel 4.19.58.

See - https://github.com/raspberrypi/linux/blob/8222f38b1ceadd0642d49812fd34a3a6cb00e264/arch/arm/boot/dts/overlays/i2c-rtc-gpio-overlay.dts

Something like: dtoveray=i2c-rtc-gpio-overlay:rc3028,backup-switchover-mode=1

@jezdez
Copy link
Author

jezdez commented Aug 20, 2019

Woah! I didn't realize there is a kernel module to do that. The script works for me so far, but I would probably accept a bit of drift in favor of a kernel module.

That would require to add rtc-rv3028 to /etc/modules right? Never done this before, any chance you could update the docs how to do that?

@Gadgetoid
Copy link
Member

See above regarding the dtoverlay line which you should add to /boot/config.txt- this makes sure the module is configured correctly. I don't have hardware with me to double-check that it works, but it should.

IIRC there are also tools for reading the RTC - timedatectl and hwclock as mentioned here - https://developer.toradex.com/knowledge-base/how-to-use-the-real-time-clock-in-linux

I've done the whole rounds of figuring this stuff out, raising the patch for the kernel module, and forgetting it all again. Story of my life!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants