Install the RTC

When installing the RTC, please note to skip the first pin. The RTC starts at PIN 2 (3.3 Volt).
Otherwise the RTC will be destroyed.

Interface

install-rtc Install the RTC

Installation

Install as Root !

# install RTC
chmod +x /etc/rc.local 
apt-get install -y i2c-tools 
apt-get purge -y fake-hwclock 
apt-get remove fake-hwclock -y 
dpkg --purge fake-hwclock 
rm -f /etc/adjtime. 
cp /usr/share/zoneinfo/Europe/Berlin /etc/localtime
ln -s /home/pi/bin/ntp2hwclock.sh /etc/cron.hourly/ntp2hwclock

Reboot here !

# Test RTC
i2cdetect -y 1
hwchwclock -w
hwclock -r

 

pi@raspberrypi:~ $ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
pi@raspberrypi:~ $

ntp2hwclock

This script sets the NTP Time to the HWClock as long a NTP connection can established.
Place this script at /home/pi/bin/ntp2hwclock.sh (for example, see above)

#!/bin/bash

# Location of logfile
LOGFILE="/usr/local/oeebox/etc/log/ntp.log"

if [ ! -f $LOGFILE ]; then
  touch $LOGFILE
fi

# Set the maximum allowed difference in seconds between Hw-Clock and Sys-Clock
maxDiffSec="2"

msgNoConnection="No connection to time-server"
msgConnection="Connection to time-server"

# Check for NTP connection
if ( ntpq -p | grep -q "^*"  ); then
        echo $msgConnection >> $LOGFILE
        echo "---------------------------------"  >> $LOGFILE

        secHwClock=$(sudo hwclock --debug | grep "^Hw clock time" | awk '{print $(NF-3)}')
        echo "HwClock: $secHwClock sec" >> $LOGFILE

        secSysClock=$(date +"%s")
        echo "SysClock: $secSysClock sec" >> $LOGFILE
        echo "---------------------------------" >> $LOGFILE

        secDiff=$(($secHwClock-$secSysClock))

        # Compute absolute value
        if ( echo $secDiff | grep -q "-" ); then
            secDiff=$(echo $secDiff | cut -d "-" -f 2)
        fi

        echo "Difference: $secDiff sec" >> $LOGFILE

        msgDiff="HwClock difference: $secDiff sec"
        if [ "$secDiff" -gt "$maxDiffSec" ] ; then
                echo "---------------------------------" >> $LOGFILE
                echo "The difference between Hw- and Sys-Clock is more than $maxDiffSec sec." >> $LOGFILE
                echo "Hw-Clock will be updated" >> $LOGFILE

                # Update hwclock from system clock
                sudo hwclock -w
                msgDiff="$msgDiff --> HW-Clock updated." >> $LOGFILE
        fi
        if !(awk '/./{line=$0} END{print line}' $LOGFILE | grep -q "$msgConnection") || [ "$secDiff" -gt "$maxDiffSec" ]; then
                echo $(date)": "$msgConnection". "$msgDiff >> $LOGFILE
        fi
else
        # No NTP connection
        echo $msgNoConnection
        if !(awk '/./{line=$0} END{print line}' $LOGFILE | grep -q "$msgNoConnection"); then
                echo $(date)": $msgNoConnection" >> $LOGFILE
        fi
fi