Saturday, 21 March 2015

RaspberryPi: Simple Time Server

Both my birdbox systems and my Portable (garden) PiCam need to know the time & date in order to properly time-stamp video clips and events.


Up until now I have relied upon an old HP Compaq D510 to help set the correct time, but I've finally got around to replacing this with a not quite so old Pi B v1 Rev1.


Although the D510 was quite economical, my Pi replacement should consume less than 5% of the power of its predecessor.

My garden Pi camera systems are on a closed network, so cannot take advantage of any internet time services. I solved this problem last year by manually setting the time/date on an old D510 located in our garage, and set the system to write the current date/time into a text file every 60 seconds using cron (see: Updating Time).

I have six D510s that I saved from a skip back in 2009, but the time has now come to send them all to calculator heaven.

I need a Real Time Clock


The first step was to fit an RTC to my oldest Pi. I already had a DS1302 RTC module, so I loaded this to a small piece of strip-board and added a long pin header.




The wiring details and basic software are the same as for my earlier RTC post. I renamed the RTC compiled C code "setClock" and copied it to a location on the system path (i.e. /usr/bin).

However, I wanted a GUI to make it easy to adjust the time when I need to remote into this headless server and put the clock right.


The Gambas Interface



I wrote a very simple Gambas app which looks like this:-


When the Pi time server starts, this Gambas app sets the Pi to the current RTC time using the setClock C program from my earlier post.

Public Sub _new()
  Exec ["setClock"]  'run the RTC "C" program setClock
  'this sets the system time to the current RTC time
End



The user interface allows me to change the date and/or time.

Public Sub btnResetTime_Click()
'User can manually update RTC time

  strTimeDateSetting = Format(DateBox.Value, "yyyymmdd") & Format(TimeBox.Value, "hhnnss")
  lblStatus.Text = "Time reset to: " & DateBox.Value & " " & TimeBox.Value
  lblStatus.Text = lblStatus.Text & " >>> String: " & strTimeDateSetting
  SetPiClock
  btnResetTime.Enabled = False

End

Public Function SetPiClock() As Boolean
 
  Exec ["setClock", strTimeDateSetting]
 
End


However, I don't expect the DS1302 RTC to drift too far between now and the end of summer.

Pi Camera System Config


The original configuration is described in this earlier post. The only change to the Raspberry Pi camera system is to edit the IP address in /etc/fstab so that they now mount the Pi rather than the D510.

Pi Time Server Config


Again this follows the earlier post, but initially I found that the birdbox systems could not mount the location on the Pi server. The difference between my D510 running Lubuntu and the Pi running Raspbian Wheezy is that the rpcbind service is not automatically started on Wheezy. This is easily fixed by running from the terminal:-

sudo update-rc.d rpcbind enable

Now you can either reboot to get it to start, or just run:-

sudo service rpcbind start

If this does not work, make sure the following packages are installed on the Pi server:-

rpcbind
nfs-kernel-server
nfs-common

Daylight Saving


One remaining problem is that when the clocks go forward (GMT + 1hr), the time on my camera systems will probably be one hour ahead. This is because the server will adjust the time and the clients (camera systems) will also adjust the time.

I think the solution is to use GMT (UTC) in the server file. This can be done by modifying the UpdateTime.sh script by adding the "-u" switch. For example from this:-

#!/bin/bash
date +%D_%T > /home/garage/clock/TheTime


...to this:-

#!/bin/bash
date -u +%D_%T > /home/garage/clock/TheTime


...I guess we will find out next Sunday whether this has worked!


>>> EDIT: 3-Apr-2015

Nope! This didn't give me the correct locale time. If you create a script containing:-

#!/bin/bash
date +%D_%T > /home/garage/clock/TheTime


...and run it, you get the correct local time in the "TheTime" file. But if you create a cron job to run the script, it gives you GMT/UTC (i.e. BST - 1hr).

So I just modified my Gambas code to write the current time to "TheTime" file with a routine like this:-

Public Function TimeStampFile() As Boolean
Dim hFile As File
Dim strTimeStamp As String
 
  strTimeStamp = Format(Now(), "dd-mmm-yy_hh:nn:ss")
  lblTimeStamp.Text = strTimeStamp
  hFile = Open "/home/pi/clock/TheTime" For Create
  Write #hFile, strTimeStamp, Len(strTimeStamp)
  Close #hFile 
 
End


Initially I used:-

  strTimeStamp = Format(Now(), "dd/mm/yy_hh:nn:ss")

...which was interpreted by the bird box pi as the American date format: mm/dd/yy

No comments:

Post a Comment