Recently i was asked to configure a TV to automatically turn off and on on certain times and then display content in form of a website. A so called “kiosk screen”. I utilized a Raspberry 3b for this, set up Raspbian and wrote a short bash script that runs as a service on boot up.
Part 1: Setup Raspberry Pi
In order to have not only terminal output on our screen we need to install the full version of Raspbian, which comes with a GUI.
First, download the full version of Raspbian here
Once downloaded, unzip the zip file, so you’re left with just a .img file.
Now you will need to download & install Etcher, the software that will write our image onto the SD card. It works on all platforms (Linux, Mac, and Windows).
Once you have downloaded & installed Etcher, flash the image onto the SD card.
Now enable SSH by placing a file name “ssh” (without any extension) onto the boot partition of the SD Card
Insert the card into your Raspberry Pi and any other extra cords such as lan, power (mouse, keyboard) and the HDMI cord and boot the Pi.
If you choose to ditch keyboard and mouse and to configure your Pi over SSH, you need the IP address. You can find this in your Router’s DHCP allocation table.
Part 2: Setting up Raspbian and additional programs
To make our Pi run faster we will remove some additional software that comes with the Raspbian full image. To do so run the following commands in the terminal and follow any additional instruction on the screen.
sudo apt-get purge wolfram-engine scratch scratch2 nuscratch sonic-pi idle3 -y
sudo apt-get purge smartsim java-common minecraft-pi libreoffice* -y
sudo apt-get clean
sudo apt-get autoremove -y
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install xdotool unclutter sed
sudo apt-get install cec-utils
If you have set a password for your pi you need to setup auto login with sudo raspi-config
(go to boot options => Desktop/CLI => Desktop Autologin)
Part 3: The kiosk.sh script
The following script turns on the tv (it must be working with the CEC protocol, but most modern tvs nowadays are) in a certain time frame and automatically turns it off afterwards.
You can change its turn on and off time by changing the variables in the script:
#!bin/bash
xset s noblank
xset s off
xset -dpms
unclutter -idle 0.5 -root &
while true; do
sed -i 's/"exited_cleanly":false/"exited_cleanly":true/' /home/pi/.config/chromium/Default/Preferences
sed -i 's/"exit_type":"Crashed"/"exit_type":"Normal"/' /home/pi/.config/chromium/Default/Preferences
#change turnon and turnofftime
TURNONTIME=7
TURNOFFTIME=19
CD=$(date +%a)
CH=$(date +%H);CH=$((10#$CH))
CM=$(date +%M);CM=$((10#$CM))
SHUTDOWNTIME=$((TURNOFFTIME-1))
RELOADSECONDS=300
if [ $CH -ge $TURNONTIME ] && [ $CH -le $SHUTDOWNTIME ]; then
#change days the screen should be off
if [ "$CD" != "SON" ]; then
echo 'on 0' | cec-client -s -d 1
if ! pgrep "chromium" > /dev/null
then
/usr/bin/chromium-browser --noerrdialogs --disable-infobars --kiosk http://welcomescreen.nlsw.de/info.php &
SLEEPTIME=$(((($TURNOFFTIME*3600))-(($CH*3600))-(($CM*60))))
i=0
while [$i -le $SLEEPTIME]
do
xdotool search --class Chrome windowactivate --sync %1 key F5 windowactivate $(xdotool getactivewin$ i=$((i + 300))
sleep 300
done
fi
else
SLEEPTIME=$((((24*3600))-(($CH*3600))-(($CM*60))+(($TURNONTIME*3600))))
sleep $SLEEPTIME
reboot -f
fi
else
echo 'standby 0' | cec-client -s -d 1
pkill --oldest chromium
if [ $CH -gt $TURNONTIME ]; then
SLEEPTIME=$((((24*3600))-(($CH*3600))-(($CM*60))+(($TURNONTIME*3600))))
else
SLEEPTIME=$(((($TURNONTIME*3600))-(($CH*3600))-(($CM*60))))
fi
sleep $SLEEPTIME
reboot -f
fi
done
You can modify this script to your liking.
Part 4: Finalize setup
We now need to tell the raspberry to start our script at boot. Enter the following commands to create and start our kiosk service.
Create and open the service file
sudo nano /lib/systemd/system/kiosk.service
Copy these lines of text into the file and save it by pressing CRTL +X followed by Y and ENTER
[Unit]
Description=Chromium Kiosk
Wants=graphical.target
After=graphical.target
[Service]
Environment=DISPLAY=:0
Environment=XAUTHORITY=/home/pi/.Xauthority
Type=simple
ExecStart=/bin/bash /home/pi/kiosk.sh
Restart=on-abort
User=pi
Group=pi
[Install]
WantedBy=graphical.target
Now enable the service by running sudo systemctl enable kiosk.service
Finally check your screen resolution again with sudo raspi-config
in 7 Advanced Options
Now sudo reboot
and enjoy.
Sources: