Dranoweb
Posts: 147
Joined: Thu Mar 20, 2014 3:01 pm

Hwclock custom script (DS3231)

Sun Jul 12, 2015 7:37 am

Hi,

I have been researching for the last few days to no avail - and have resolved to just ask the question.
I'm building a museum light and sound show on a tight budget.

I have had trouble getting my DS3231 to initialize in both RC.local and Init.d
so I ended up doing it in an SH script and triggering it @reboot in root user's cron - which works.
at boot I get the following:

Code: Select all

pi@raspberrypi ~ $ sudo hwclock
Sun 12 Jul 2015 17:32:18 AEST  -0.387504 seconds
I have installed ntdate, but it clashes with some other NTP service that's already running (whatever the default service is)
as I'm using cron to trigger various mp3 playback events for the show, I had hoped to make a script to run at boot/reboot that does the following:

My current situation and config:

Situation:
Have installed a DS3231 RTC module and wish to have it do the following:
1: Initialize on boot, then sleep 5 minutes. (resolved by means of a script running @reboot in cron)
2: Check for internet connection
3: If no connection present, update system time from hwclock
4: If connection present, check NTP, then update system clock - then hwclock from system.

Setup:
Pi B+
Raspbian Kernel: 3.18.10+ (booting to GUI on touch screen)

Hardware:

*3 way Multiplexer board
*DS3231 RTC
*Prototype expansion board
*Waveshare Spotpear 3.2 inch Touchscreen
*4 way Relay board (opto-isolated)
*TP-link WN727 USB wifi (not presently working)
*Wired lan connection (during setup only)

Root Cron entry:

Code: Select all

@reboot sh /home/pi/loadclock.sh
loadclock.sh

Code: Select all

echo ds1307 0x68 | sudo tee /sys/class/i2c-adapter/i2c-1/new_device
sudo modprobe rtc-ds1307

Dranoweb
Posts: 147
Joined: Thu Mar 20, 2014 3:01 pm

Re: Hwclock custom script (DS3231)

Sun Jul 12, 2015 11:29 am

Temporary fix is this:

modified my loadclock.sh script to include hwclock -s:
then manually stopping ntpd, manually requesting ntp sync,
then starting ntpd again.

Code: Select all

echo ds1307 0x68 | sudo tee /sys/class/i2c-adapter/i2c-1/new_device
sudo modprobe rtc-ds1307
sudo hwclock -s
sudo service ntp stop
sudo ntpd -gq
sudo service ntp start
Hopefully, it will load the clock driver, set the system time from the hwclock.
then check NTP and update again, if a net connection is present, if not it will fail, and move to the next line... I hope.

This should hold until I find a proper method.

User avatar
allfox
Posts: 452
Joined: Sat Jun 22, 2013 1:36 pm
Location: Guang Dong, China

Re: Hwclock custom script (DS3231)

Sun Jul 12, 2015 1:20 pm

Greetings.

I have a nixie table clock driving by Pi.

1 - You don't need to sync time from RTC by manually issuing "hwclock" command.
2 - You don't need to disable ntpd, it is clever enough: when got Internet time, it would enable kernel to write that time to RTC.
3 - You don't need to write your own script or service. You could just tell Pi that you have a RTC.

Try this: viewtopic.php?p=775626#p775626

Dranoweb
Posts: 147
Joined: Thu Mar 20, 2014 3:01 pm

Re: Hwclock custom script (DS3231)

Sun Jul 12, 2015 1:50 pm

The problem is, that the hwclock is not being initialised at boot.

despite adding the commands to both init.d and rc.local.

hence the need to run a script from cron.

User avatar
allfox
Posts: 452
Joined: Sat Jun 22, 2013 1:36 pm
Location: Guang Dong, China

Re: Hwclock custom script (DS3231)

Sun Jul 12, 2015 2:19 pm

Dranoweb wrote:The problem is, that the hwclock is not being initialised at boot.

despite adding the commands to both init.d and rc.local.

hence the need to run a script from cron.
/etc/init.d/hwclock.sh is a system service, it would work on boot. But by default, Pi don't have a RTC, so it is disabled. You need to disable "fake-hwclock" service, then enable "hwclock.sh" service.

If you use @reboot in cron, the /var/log/syslog's first few lines would get a wrong time stamp. Because @reboot would be executed at the very last step on boot. Using /etc/init.d/hwclock.sh would get the right result.

User avatar
allfox
Posts: 452
Joined: Sat Jun 22, 2013 1:36 pm
Location: Guang Dong, China

Re: Hwclock custom script (DS3231)

Sun Jul 12, 2015 2:23 pm

And, if you mean to "write Internet time to RTC" on boot. I think you don't need that.

When ntpd is able to get time from Internet, it would tell kernel, so kernel would write time to RTC every 11 minutes.

Please read that link, it's a good one.

User avatar
allfox
Posts: 452
Joined: Sat Jun 22, 2013 1:36 pm
Location: Guang Dong, China

Re: Hwclock custom script (DS3231)

Sun Jul 12, 2015 2:34 pm

Dranoweb wrote: Have installed a DS3231 RTC module and wish to have it do the following:
1: Initialize on boot, then sleep 5 minutes. (resolved by means of a script running @reboot in cron)
2: Check for internet connection
3: If no connection present, update system time from hwclock
4: If connection present, check NTP, then update system clock - then hwclock from system.
Your wished steps just like a program:
Check Internet -> Read RTC if no connection -> Read NTP if connected

But think again like this:
Read RTC -> Check Internet -> Read NTP if connected

"Read RTC" would be done by /etc/init.d/hwclock.sh
"Check Internet" and "Read NTP" would be done by ntpd
and if ntpd is successed, it would enable kernel to write system time to RTC every 11 minutes.

So with a default Raspbian installation, I think you just need to:
1 - Plugin DS3231 and set it up.
2 - Disable "fake-hwclock", enable "hwclock.sh"

Dranoweb
Posts: 147
Joined: Thu Mar 20, 2014 3:01 pm

Re: Hwclock custom script (DS3231)

Mon Jul 13, 2015 1:26 am

/etc/init.d/hwclock.sh is a system service, it would work on boot. But by default, Pi don't have a RTC, so it is disabled. You need to disable "fake-hwclock" service, then enable "hwclock.sh" service.

If you use @reboot in cron, the /var/log/syslog's first few lines would get a wrong time stamp. Because @reboot would be executed at the very last step on boot. Using /etc/init.d/hwclock.sh would get the right result.
This would explain why any changes to hwclock.sh are not making any difference.

Knowing that the system checks NTP every 11 minutes is handy - part of the information I was trying to find.

I'm hesitant to disable the fake hw clock, as the RTC module is connected to a multiplexer board that could potentially be disconnected when the system's case is open.

I'd like it to be a little bomb proof, as I'm not the only one that may be called upon to service it.

I'l do some reading, and better inform myself - and report back the results.

Return to “Advanced users”