Thursday, April 04, 2013

Updating Time over HTTP on Raspberry Pi

The Raspberry Pi is a wonderful device that lends itself to creating all sorts of projects in remote locations. Network connectivity is the way to make the Pi sing and with the addition of a cheap wireless adapter based on a RTL8188CUS chipset, you can shove the Pi into some very non traditional installations.  Unfortunately, the Pi does not include a realtime clock due to cost and complexity concerns so network connectivity is required to bootstrap the current time and date.  By default, Raspbian ships with a NTP (Network Time Protocol) dameon which will fetch and set the current time and date from the configured network time servers.  The challenge arises if you need to set the time while connected behind a very restrictive proxy that doesn't allow for anything other than HTTP (TCP 80).  Welcome htpdate to the rescue which will allow you to set the system time within a second or two based on the http response headers of ordinary websites.  Unfortunately, there is no ARM based debian package for htpdate currently so apt-get install htpdate will say package not found.  I will show you a good way to get htpdate up and running on a raspberry pi running raspbian by building your own debian package which makes for easy uninstalls.  I recommend SSHing into your Raspberry Pi to make this as easy as possible.

Building and Installing htpdate as a Package

After ensuring you have a network connection, install checkinstall

sudo apt-get install checkinstall

Create a directory to download htpdate

mkdir ~/download/htpdate && cd  ~/download/htpdate

Find the URL to the current version of htpdate-xxx.tar.gz here.  I am currently using htpdate-1.0.5.tar.gz for this example.

Now download the current version using the URL you discovered above and extract it (of course change the version number if you are not downloading 1.0.5)

wget && tar -xvfz htpdate-1.0.5.tar.gz

Lets change into the extracted directory and compile the project (takes about 15s on the Model B)

cd htpdate-1.0.5 && make

Now we are ready to build the debian ARM package.  (Running this as root is very important)

sudo checkinstall -D make install

Accept the default set of package docs when asked

Should I create a default set of package docs?  [y]: y

You will be asked to provide a short description; I chose HTTP Time Protocol. (Note you have to hit ENTER twice)

Please write a description for the package.
End your description with an empty line or EOF.
>> HTTP Time Protocol

You will be asked if you want to change any of the default package values.  I opted not to since I am not publishing this package so just hit ENTER to continue.

Congrats.  The package has been built and installed.  You could even copy the resulting .deb file to another Pi and install it, changing the dir to the dir of your copied deb file.

sudo dpkg -i /home/pi/download/htpdate/htpdate-1.0.5/htpdate_1.0.5-1_armhf.deb

Testing and Verification of htpdate

Now that we have a package and its installed, we can move on to testing that htpdate does what we need.
Lets ensure that htpdate can reach out and fetch the dates from some websites that are geographically close to you.  -d infers debug and -q says to merely query the sites, don't set the time.

sudo htpdate -d -q

You should see some output similar to the below

#: 3 mean: 0 average: 0.333 Timezone: GMT-6 (CST,CDT) Offset 0.333 seconds

Looks good.  Now lets commit and set the date and keep it up to date by running this as a dameon. -D runs htpdate as a dameon, -s will force change the clock on the first poll and then provide adjustments there after, and -l will log the status to the syslog (/var/log/syslog)

sudo htpdate -D -s -l

Making htpdate Startup on Boot

So far we have downloaded the source code, compiled it, created a nice installable and removable debian package, and shown how to query and set the date using htpdate.  Unfortnately, if you reboot or have the pi off for long periods of time, your system time will quickly drift.  To fix that, we can start the htpdate in dameon mode on boot.  This section assumes you have familarity editing text files on linux and I will use nano for my examples but feel free to use your favorite text editor such as vim or emacs.

Lets become root

sudo -s

Enter into the init.d directory, and create a new startup script for htpdate, and make it executable

cd /etc/init.d/ && nano htpdate && chmod +x htpdate

Paste the below into your text editor (modify HTPHOSTS with a list of your own geographically close sites if you wish) and then save (CTRL + O) and exit (CTRL + X)


# Provides:        htpdate
# Required-Start:  $network $remote_fs $syslog
# Required-Stop:   $network $remote_fs $syslog
# Default-Start:   2 3 4 5
# Default-Stop:    0 1 6
# Short-Description: Start htpdate in daemon mode

# RUNASUSER will generally be pi, unless you've created your specific user account
export RUNASUSER='pi'
# Space delimited list of sites to pull time info from without the http://
export HTPHOSTS=''

case "$1" in
    # if it's start, then start htpdate
    su $USER -c "/usr/bin/htpdate -D -s -l -i /tmp/ -u nobody:nogroup $HTPHOSTS"
    echo "Started htpdate for $RUNASUSER using /usr/bin/htpdate -D -s -l -i /tmp/ -u nobody:nogroup $HTPHOSTS"
    # if it's stop, then just kill the process and delete the pid file
 pkill -F /tmp/
 rm -rf /tmp/
 #kill any rogue instances of htpdate 
 killall -q -w htpdate 
    echo "htpdate stopped"
    echo "Usage: /etc/init.d/htpdate {start|stop}"
    exit 1
exit 0

Make sure that you have no mistakes by stopping and starting htpdate

/etc/init.d/htpdate stop && /etc/init.d/htpdate start

You should see that the process started Started htpdate...

Now lets add it to the startup process

update-rc.d htpdate defaults

Congratulations!  You can now reboot and htpdate will automatically update the time even if you have a restrictive firewall and will only do so after the network connection has been established.


calibration said...

My cousin recommended this blog and she was totally right keep up the fantastic work!

Mechanical Calibration

Ivo Mans said...

Thanks for sharing your clear explanation. Your explanation also works for a Beaglebone Black.

Anonymous said...

Hello, I've got a problem, if I shutdown the raspi.. After rebooting the raspi the time is still right. But if I disconnect the voltage supply, after shutting the device down, it always starts with the old time. I tried to follow your instructions and it seems like everything is going right.. just doesn't start on boot. So it isn't really added to the start process. Any suggestions, what I could do? Thanks!
Greetings from Germany,

Milo Oien-Rochat said...
This comment has been removed by the author.
Milo Oien-Rochat said...

Debian package available for pi3 maybe others. Need to add packages to the apt source list before install will work. The address below has instructions for Raspbian Stretch on a pi3.