A few weeks ago I bought the official Raspberry Pi 7″ touchscreen display for a project which sadly didn’t work as intended, this left me with the unused display and a case.
I started thinking about things I could use it for and settled on a shack clock. Long term readers of my site will recall that some years ago I built a couple of Nixie tube clocks but over the years I managed to drop something on one of them, breaking a tube and on the other, a couple of the larger tubes have gradually failed. I ran an old iPad with a Nixie tube clock display for some time but switched that off a few months ago.
I spent some time searching around the internet for help and advice on using a Raspberry Pi for a shack clock and found a couple of options. I’ve combined and tweaked guides from a few places and have put them all together to write these instructions. I’m not sure I’d specifically go out and buy the display for this but if you already have one that you’re not using then it’s better than it sitting around gathering dust.
Here’s how it looks.
It works by installing a basic local web server on your Pi, setting up a clock within that web server and then running a full screen version of the Chromium browser in kiosk mode to display that clock. It’s very straightforward and only needs a simple Pi. I’m running mine on an old Pi2B.
Please don’t even consider using a Pi4 for this, it’s serous overkill and as it’s using Raspberry Pi OS Stretch, it won’t work on a Pi4 anyway.
First, download Raspbian Stretch Lite and write it to an SD card. I highly recommend balanaEtcher to write the image out to an SD card. Note that we’re using the ‘Lite’ version of Raspbian so you won’t have any graphical user interface and all the work will be done from the terminal.
You will need to either give your Raspberry Pi a static IP address or set up a reservation in your router. I do this by looking at my router and checking what devices have connected and then set up a DHCP reservation so each particular Raspberry Pi I own will always have the same address each time it reboots.
Open up a terminal/dos prompt or whatever client software you’re going to use to connect to the Raspberry Pi and log in. In my case, the Raspberry Pi is on 192.168.1.203 so I use the command
ssh pi@192.168.1.
203
You’ll be prompted for a password – It’s currently set to raspberry.
Use the following command to enter the Raspberry Pi config utility:
sudo raspi-config
Now select the first option Change User Password and press enter. Press enter again and you will be prompted to Enter new UNIX password. Type a new password, press enter and re-type your new password again. You will get a message telling you your password has been changed successfully. Make a note of this new password.
Use the arrow keys again to move down to 4 Localisation Options and press enter. Use the arrow keys again to select I2 Change Timezone and press enter. First select your Geographical area and press enter and then select your Time zone and press enter. This will set your clock to display your local time. If you want it to show UTC then when selecting your Geographical area, scroll down to None of the above and you can pick UTC there.
Now you should set your Wi-Fi country – By default, this image is set to GB (Great Britain) so if you’re in a different country you should change this to ensure that the Pi uses the correct frequencies. Select 4 Localisation Options and press enter. Scroll down to I4 Change Wi-fi Country and press enter. Find your country, press enter and then press enter again to return to the main screen.
Finally, select 2 Network Options and press enter. Press enter again on N1 Hostname and give your Raspberry Pi a suitable name. I’ve called mine ‘shack-clock’.
You will now be returned to the main Raspberry Pi Software Configuration Tool main screen. Use the tab key to select <Finish> and press enter. You will be asked if you would like to reboot now. Press enter to reboot and once your Pi has rebooted, log back in using the new password you set earlier. Make a note of this password because if you forget it, you’ll need to start from scratch.
If you’re using a Mac and connecting to your Raspberry Pi via the terminal you may notice the screen flashes badly. You can fix this easily and quickly by going into the terminal Preferences/Profiles and make sure the box “Allow blinking text” is not ticked.
Assuming your Raspberry Pi now has either a static or reserved address, you can start with the actual setup.
Log in to your Pi with the new password you set above and type the following commands.
sudo apt update
sudo apt upgrade -y
This is making sure that Raspbian Stretch is fully patched as far as it will go. The first will go through quickly, the second command may take a little longer and if you’re using an older Pi, it will take a good few minutes. When it has completed, reboot the Pi.
Log back in and the first thing we will do is create a user account to run the clock.
sudo adduser clock
You will be promoted to enter a password for the clock user account. Make sure you remember what this password is as you’ll need it shortly. Press enter all the way through the following questions and then add the clock user to the sudo group.
sudo usermod -aG sudo clock
Log out from your Pi and log back in using the clock user account you’ve just created, remember to change the IP address as appropriate.
ssh clock@192.168.1.203
The following commands will install everything that’s needed to run the clock.
sudo apt install xorg openbox -y
sudo apt install xserver-xorg xinit -y
sudo apt install chromium-browser -y
sudo apt install unclutter -y
sudo apt install lighttpd -y
These will take some time to complete. When they’re all done, enter the following.
sudo nano /etc/X11/Xwrapper.config
Change the last tine so it reads.
allowed_users=anybody
Press ctrl-x followed by the Y key to save.
Now we need to edit a couple of configuration files.
sudo nano /home/clock/.xserverrc
Enter the following into the file.
#!/bin/sh
# Start the X server session with no power management so the display never sleeps
exec /usr/bin/X -s 0 -dpms -nolisten tcp "$@"
Again, use ctrl-x followed by the Y key to save and exit.
sudo nano /home/clock/.xsession
Enter the following into this file.
#!/bin/sh
# Start Chromium at startup
chromium-browser --start-fullscreen --window-size=800,480 --disable-infobars --noerrdialogs --incognito --kiosk http://localhost
Note the window size. The Raspberry Pi 7″ touch screen has a resolution of 800×480. If you are using a different screen with a different resolution, change 800×480 in this file to the resolution of your display. When you’re done, use ctrl-x followed by the Y key to save and exit.
There’s one more file to write. This creates the actual service to run the clock. It also monitors the service and if it fails, will restart it after ten seconds.
sudo nano /etc/systemd/system/clock.service
Add the following text.
[Unit]
Description=Clock
After=network-online.target
DefaultDependencies=no
[Service]
User=clock
ExecStart=/usr/bin/startx
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
As always, ctrl-x then Y to save and exit.
Now there are some files to download.
cd /var/www/html
sudo wget https://qsl.net/g6nhu/clock/index.html
sudo wget https://qsl.net/g6nhu/clock/index.html.day
sudo wget https://qsl.net/g6nhu/clock/index.html.night
You will notice that there are three files there, one live, one for day and one for night. The daytime file has a vibrant green colour font and the night file has a far deeper and less bright green colour. This clock file was originally downloaded from Nayuki’s site here and is used with permission.
If you want to change the colours you can do this by editing /var/www/html/index.html where you’ll find the background colour on line 15 and the main text colour on line 18.
Now enable the service and start the clock.
sudo systemctl enable clock
sudo systemctl start clock
sudo reboot now
Once it’s rebooted, you should see the clock on your touchscreen. There’s one thing left and that’s to set up a couple of cron jobs to change the clock to the duller display overnight and also tweak the display brightness.
Log back in with your clock user account and go into crontab.
sudo crontab -e
The following lines should be self explanatory but I’ll explain them anyway. At 23:00, the index.html.night file replaces index.html and the clock is restarted. An hour later, the Pi touchscreen is dimmed. At 06:00 the screen goes back to full brightness and at 07:00, the index.html.day file is copied back in and the clock is restarted. You can adjust these times as appropriate and if you wish to change the colours, look in the three index files as described above.
# Switch the clock to a less vibrant green from 23:00 to 07:00 every day
# At midnight, reduce the brightness and set it back to max at 06:00
00 23 * * * cd /var/www/html ; cp index.html.night index.html ; systemctl restart clock
00 00 * * * sudo sh -c 'echo "60" > /sys/class/backlight/rpi_backlight/brightness'
00 06 * * * sudo sh -c 'echo "255" > /sys/class/backlight/rpi_backlight/brightness'
00 07 * * * cd /var/www/html ; cp index.html.day index.html ; systemctl restart clock
As an added bonus, this clock can be accessed from anywhere on your home network, simply open a web browser on any device and type your shack clock IP in the address bar.