[HOWTO] Add real time clock to OSMC

#1

##Introduction
The advantages of adding a real time clock to a Raspberry Pi is that if you got a bad connection the Raspberry doesnt reset time and set it to 1970 everytime to do this you need a ds3231 pretty much available worldwide.

first off start by adding the RTC clock to your raspberry and placing on the gpio pins per image above.

then run this script, what the script does is installs python-smbus i2c-tools then adds dtparam=i2c_arm=on and dtoverlay=rtc-i2c,ds3231 to /boot/config.txt
then it adds #blacklist i2c-bcm2708 and #blacklist i2c-dev to /etc/modprobe.d/raspi-blacklist.conf
and last but not least i2c-bcm2835, i2c-dev, rtc-ds1307 to /etc/modules.

#!/bin/bash
if [[ "$USER" != 'root' ]]; then
	echo "Sorry, you need to run this as root"
	exit
fi

install_i2c () {
    apt-get install -y python-smbus i2c-tools
    if grep -Fxq "dtparam=i2c_arm=on" /boot/config.txt
        then
	        echo \"dtparam=i2c_arm=on\" is present in config.txt
        else
	        echo "dtparam=i2c_arm=on" >> /boot/config.txt
	        echo \"dtparam=i2c_arm=on\" is added to config.txt
    fi
    if grep -Fxq "dtoverlay=rtc-i2c,ds3231" /boot/config.txt
        then
	        echo \"dtoverlay=rtc-i2c,ds3231\" is present in config.txt
        else
	        echo "dtoverlay=rtc-i2c,ds3231" >> /boot/config.txt
	        echo \"dtoverlay=rtc-i2c,ds3231\" is added to config.txt
    fi
    if grep -Fxq "#blacklist i2c-bcm2708" /etc/modprobe.d/raspi-blacklist.conf
        then
	        echo \"#blacklist i2c-bcm2708\" is present in raspi-blacklist.conf
        else
	        echo "#blacklist i2c-bcm2708" >>/etc/modprobe.d/raspi-blacklist.conf
	        echo \"#blacklist i2c-bcm2708\" is added to raspi-blacklist.conf
    fi
    if grep -Fxq "#blacklist i2c-dev" /etc/modprobe.d/raspi-blacklist.conf
        then
	        echo \"#blacklist i2c-dev\" is present in raspi-blacklist.conf
        else
	        echo "#blacklist i2c-dev" >>/etc/modprobe.d/raspi-blacklist.conf
	        echo \"#blacklist i2c-dev\" is added to raspi-blacklist.conf
    fi
    if grep -Fxq "i2c-bcm2835" /etc/modules
        then
	        printf "i2c-bcm2835\ni2c-dev\nrtc-ds1307\n" is present in modules
        else
	        printf "i2c-bcm2835\ni2c-dev\nrtc-ds1307\n" >>/etc/modules
	        printf "i2c-bcm2835\ni2c-dev\nrtc-ds1307\n added modules\n"
    fi
    reboot
}

install_i2c

next up is getting it started every time whats gonna happen here is that we gonna scan for the device and there is gonna be a number coming up thats gonna look something like this

EXAMPLE:

 sudo 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: -- -- -- -- -- -- -- --

Now the script below is gonna ask for such a number so just enter the number when promted for it.

#!/bin/bash
if [[ "$USER" != 'root' ]]; then
	echo "Sorry, you need to run this as root"
	exit
fi

install_i2c() {
                rm /etc/init.d/hwclock.sh
                wget https://gitlab.com/swe_toast/OSMC-Addons/raw/master/hwclock.sh -O /etc/init.d/hwclock.sh
                if [ $(sudo i2cdetect -y 1 | wc -l) -ge 1 ] ; then
                echo "i2c found on on pin 1"
                i2cdetect -y 1
                echo "Add the number in the from the test:"
                read number
                echo "ds3231 0x$number" | sudo tee /sys/class/i2c-adapter/i2c-1/new_device
                hwclock -r
                sed -i -e '13imodprobe i2c-bcm2708\' /etc/rc.local
                sed -i -e '14echo ds3231 0x$number > /sys/class/i2c-adapter/i2c-1/new_device\' /etc/rc.local
                sed -i -e '15ihwclock -s\' /etc/rc.local
            else
                if [ $(sudo i2cdetect -y 0 | wc -l) -ge 1 ] ; then
                    echo "i2c found on on pin 0"
                    i2cdetect -y 0
                    echo "Add the number in the from the test:"
                    read number
                    echo "ds3231 0x$number" | sudo tee /sys/class/i2c-adapter/i2c-0/new_device
                    hwclock -r
                    sed -i -e '13imodprobe i2c-bcm2708\' /etc/rc.local
                    sed -i -e '14echo ds3231 0x$number > /sys/class/i2c-adapter/i2c-0/new_device\' /etc/rc.local
                    sed -i -e '15ihwclock -s\' /etc/rc.local
                fi
                echo "cant find i2c module"
            fi
            reboot
}

install_i2c

now to complete it all

we read the time off the rtc device (its gonna be off)
hwclock -r
then we read the time of the system
date +"%d %b %Y %T"
go ahead and copy the time of the command above we are gonna use it in the next command to set time on rtc device
hwclock -w
and last but not least we verify that it has the right time
hwclock -r

And thats how you add a RTC Clock so if no net is detected you still get the right time on your Raspberry Pi.

Hopefully something like this could be automated in myosmc instead of having to do shellscripting

3 Likes
#2

I just tried this method using putty ssh. No joy any ideas?

when i paste it - putty closes/crashes…

#3

change ssh client ?

#4

Downloaded “Remote Terminal” from the windows app store - same affect script made the connection terminate. I read that OSMC does NOT have a root level. How could i do this without root? Sudo?

#5

Can you please show where you read those wrong information? Compared to some other mediacenter systems OSMC has full supported root access. By using either sudo as user osmc or setting a password for root (via sudo) and then login as root.

But issuing a command crashing the ssh connection must be a different problem.
I suggest you provide logs with grab-logs -A

#6

Finally, I found this:
https://osmc.tv/wiki/general/usernames-and-passwords/

I think the script will work fine with root access lol I have been racking my brain trying to figure out why it wasn’t working…

#7

I miss-read it somewhere i guess. Never saw that part of the forum - I researched most of this while out to sea - very slow internet and sometimes non-existent.

#8

Link to logs:

http://paste.osmc.io/enogokugag

Update:
I enabled root access and the script has worked!

update:
Ran both scripts and entered the number it read.
after reboot - I did the hwclock -r and it came back
“osmc@osmc:~$ sudo hwclock
hwclock: Cannot access the Hardware Clock via any known method.
hwclock: Use the --debug option to see the details of our search for an access method.
root@osmc:~# hwclock --debug
hwclock from util-linux 2.25.2
hwclock: cannot open /dev/rtc: No such file or directory
No usable clock interface found.
hwclock: Cannot access the Hardware Clock via any known method.”

update:
re-ran the 2nd script - ends with
“ds3231 0x68
Thur Apr 21 21:54:10 2016 -0.067806 seconds
sh: 1: cho: not found”

I really appreciate all the help - I am still learning and everything helps!

#9

https://nicegear.co.nz/blog/using-an-i2c-real-time-clock-rtc-with-a-raspberry-pi/

this guide might help you better then my script since your a newbie

#10

Ok after following that how-to you linked. Running hwclock returns the date and seems correct. However when i reboot osmc from terminal - i noticed that my rc.local is failing. i nano /etc/rc.local
this is whats in rc.local I have tried adding “exit 0” but doesnt fix rc.local not starting.

Here is the rc.local fie minus comments:
modprobe i2c-bcm2708
hwclock -s

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device

#11

ok so my real issue is /etc/rc.local is not running.

“rc.local failed to start compatibility”

I have deleted everything out of rc.local and still has the same result.

Update: So i got frustrated and deleted OSMC off my MicroSD card. Reinstalling it now.

Update: Tried this script on my RPI3 with OSMC freshly installed - same result - wont detect hwclock. and i noticed after running the second script and it makes the rpi3 reboot thats when the rc.local stops running due to compatibility.