Create a smart clock with a Raspberry Pi

Inspired by this article, I’ve decided to create a new clock for my living room.

I built it on top of newest Rasperry Pi model 3B+ with its official 7″ display, so some things are a bit different than those explained in that article and the pages it links to.

The full code is available on GitHub.

Normally, it’ll show the time and weather forecast for the following 24 hours:
Screenshot of the clock displaying weather data

But when I’m playing music (which I usually stream from a Subsonic server with my own Soundwaves), it’ll display the current track:
Displaying the current playing track

Setting the Pi up

The first thing to do is to install Raspbian on the Pi: simply follow the instructions on the official site (I’ve used Etcher, as it seemed the easiest way to do it).

Before moving the SD card into the Rasperry Pi, you might want to create an empty file named ssh in the root of the card: it will enable ssh at the first boot, making it easier to proceed if you don’t have a keyboard to connect to the Pi.

If you’re using the display in a case like the one from OneNineDesign, you’ll also need to add this line to config.txt:

lcd_rotate=2

Since we need a reliable clock, install chrony to keep the time synchronized with NTP servers:

sudo apt install chrony

Booting into the application

From now on, I’ll assume you’re connect to the Pi via ssh. You can find out the ip to use in several different ways: looking at the DHCP leases on your DHCP server/router, using ifconfig in a terminal, etc…

Execute sudo raspi-config and set the Pi to boot to the Desktop, with autologin.

Let’s make sure that there won’t be a visible mouse pointer on the screen. Edit /etc/lightdm/lightdm.conf and look for the line with “xserver-command”. Either uncomment and edit it, or add a new line afterwards:

#xserver-command=X
xserver-command=X -bs -core -nocursor

Install chromium:

sudo apt install chromium-browser ttf-mscorefonts-installer

Create /home/pi/.xinitrc with the following content:

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/sh
while true; do

        killall -9 chromium-browser 2>/dev/null

        # Disable DPMS / Screen blanking
        xset -dpms
        xset s off

        # start without using cache or writing to disk
        chromium-browser --app=http://url.to.your.clock.setup/ --incognito --disk-cache-dir=/dev/null

done;

url.to.your.clock.setup needs to be the address of the site running the code cloned from GitHub, obviously. 🙂 It can be a different machine, or the very same Raspberry Pi, if you install apache + php on it. Follow the instructions in the readme to configure the web app.

Finally, create a link too:

ln -s /home/pi/.xinitrc /home/pi/.xsession

so that the script will be executed either when X gets started by the display manager or startx.

Now reboot, and if everything went ok, you’ll see your new clock 🙂

One last thing… if you find that the display is too bright, you can adjust the brightness with the command:

echo 32 > /sys/class/backlight/rpi_backlight/brightness

with 32 being a number between 0 and 255.

Once you find the level you like, add that command to /etc/rc.local

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.