Wednesday 23 October 2013

RaspberryPi: keeping time without the internet

The RaspberryPi does not have a real time clock (rtc) so normally requires an internet connection to reset the time and date from a time server.

 

For my BirdBox 2014 project I need to be able to time-stamp files & data, but it would not matter if the system time was a few minutes out either way, so I have been considering the options.


One popular solution is to connect a small rtc board to the RaspberryPi. These boards are small, low cost, and include a lithium backup battery.

However, for my BirdBox project, I'd rather avoid adding more electronics if I can find an alternative solution. The RaspberryPi will be connected to a closed local network (i.e. no internet connection) but there will be a network computer running in the garage whenever the RaspberryPi is running. This garage computer has its own rtc, so why not make use of this?


Some Background

When first switched on, the RaspberryPi uses the time saved from the last session (so it may think its 22:45 on Tuesday 1st October when you switch it on at 9:00 2nd October). The RaspberryPi seems to keep time when running even though the actual time is wrong. So it needs to be synchronised when first turned on.

This synchronisation is handled by ntp (network time protocol) service. The RaspberryPi has been setup to look for an internet time server and use this to continually correct any errors in its own timekeeper.

Without Internet Access

So on a network without internet access, we can simply use a local network server to act as a time server. This won't be as accurate as internet time, but for my project this is not a problem. All I need is for video clips and reports to include a reasonably accurate indication of time.

This system will only run from early March to late July, and I don't expect my garage computers clock to drift too much in that time.

It turns out that ntp setup for this project is a lot easier than I expected. We basically modify our server ntp.config file to specify a special IP address (127.127.1.0).

Then we "fudge" the system to say that our time service is good enough to use in the absence of anything better (low stratum numbers are very good, higher numbers are not so good).

For the garage computer (running Lubuntu)

  • Backup the original /etc/ntp.conf file (save as ntp.conf.org)
  • In ntp.conf comment out all server lines using # (e.g. #server) and add:-
server 127.127.1.0
fudge 127.127.1.0
stratum 10
  • Now restart ntp service from terminal:-
sudo service ntp restart

Now on the RaspberryPi

  • also edit /etc/ntp.conf
  • comment out all server lines (as before) and add:-
server 192.168.0.12  {or whatever your server IP address is}
  • restart RaspberryPi ntp:-
sudo service ntp restart

...if you get an error, try:- 
 
sudo /etc/init.d ntp restsart

You can now test that this is working. Each time you power-up your RaspberryPi it should take 5 minutes to synchronise to the correct time.

But I can't wait 5 minutes!

Now I'm not sure how annoying the 5 minute wait will be when I come to use this system. If it turns out to be a problem I may automatically reset the RaspberryPi clock as it boots via a script or program like this:-

 service ntp stop
 ntpd -gq
 service ntp start


...which should correct time in one jump.

...and if this doesn't do it for you, there's always the DS1302 rtc.


Edit 14th March 2014:-
Using NTP for my bird box (where the system is powered on and off many times per day) turned out to have a few problems. So this has been replaced by this new method.

3 comments:

  1. Hi Steve, as you might have spotted under the wires in my last blog post my prototype is using the adafruit DS1302 board... although it only has 4 components on it! The beauty of it is that the Arduino I schedule my camera with happily sits on the same I2C bus.

    Anyway, clocks aside, for M12 lens holders try stock-optics; http://www.stockoptics.com/board-lenses/board-lens-holder.html

    Scorpion Vision are another company that stock holders - but I think they are dearer; their lenses might be of interest though;

    http://scorpionvision.co.uk/catalogue-index/camera-lenses/board-camera-lenses-m12


    ReplyDelete
  2. Thanks Michael. I've ordered a lens mount and a 100deg lens from Stock Optics, and the they should arrive mid-week.

    The rtc looks good, so may consider for future. Right now I've still got a lot to prepare, build and test between now & February.

    ReplyDelete
  3. I can relate to that; aside from developing a pi camera - I'm building more GoPro controllers for spring, they are pretty fiddly. Suffice to say I'll be designing for some degree manufacture this time... rather than fiddly cable looms I'll have a PCB connected to the pi GPIO with headers for Vin and the IR LEDs... If that's of interest let me know as making a few more should bring the unit costs down.

    ReplyDelete