Automate a RPi kiosk

Automating a RPi for Grafana dashboards

Posted by Giulio Magnifico on Monday, March 10, 2025

Problem to solve:

Turn on and off remotely a Raspberry Pi 4B with an external HDMI display that acts as a kiosk to show Grafana dashboards in full screen using Firefox. And clear the RAM every 3/4 hours to avoid the Firefox crash.

More details:

The major problem is due to the fact that, after 5/6 hours of Firefox being open and continuously updating Grafana statistics, Firefox crashes because it completely fills up the RAM of the Raspberry Pi (4GB).

I tried using some common commands to clear the RAM but they are not useful, as they only release the available RAM, while Firefox “keeps it used”.

The solution is therefore inevitably to close Firefox and reopen it every 3/4 hours. With the old versions of Grafana, a page reload was sufficient, but unfortunately, with the latest ones, it no longer works. I don’t know if it’s a bug in Grafana or intended behavior, I don’t see any errors in the logs, so I think it’s behavior specific to the latest versions. Which obviously have other graphical improvements (thankfully).

Anyway, reloading a tab or closing and reopening Firefox doesn’t make much difference, so for now I’ll close and reopen it, plus a cache clean. And if it goes back to how it was before, that’s better and I’ll change the script.

Result

A Video showing the startup and shutdown process. It’s a 2x gif ~25MB (wait for it to load if you don’t see it):

video

Hardware

I used a spare Raspberry Pi 4B with 4GB of RAM, a 2.5" SSD, and a GeekPi heat dissipator: Ultra-Thin Ice Tower Cooler (€17.99)

Raspberry Pi 4B Kiosk Kiosk entrance

The display is a 14-inch touch IPS panel with 1920x1200 pixels from a unknow brand called Prechen: Link to Amazon (€119.99). For more info, like enabling touch input, here’s my previous post with details: My Network Home Setup v5 | Kiosk 4in Touch Monitor.

Kiosk

Use any smart plug you prefer. I’m using all Meross plugs in my home.

First, you need to connect the power supply of the Raspberry PI and the monitor to a power outlet that can be controlled remotely, in order to be able to turn it off completely and turn it on again.

For the OS this time I used Raspbian, which already includes a graphical desktop (LXDE) and other useful tools.

Software

To launch Firefox at boot I created a startup service:

[Unit]
Description=Start Kiosk Mode
After=graphical.target network.target

[Service]
Type=simple
User=pi
Group=pi
Environment=DISPLAY=:0
Environment=XAUTHORITY=/home/pi/.Xauthority
ExecStart=/home/pi/Documents/browser-kiosk.sh
Restart=no

[Install]
WantedBy=graphical.target

Where the browser-kiosk.sh script opens Firefox in full screen:

#!/bin/bash
sleep 5 
firefox —kiosk

Clear Firefox’s RAM

At the beginning, I used to only reload the page using xdtool:

#!/bin/bash
export DISPLAY=:0
WID=$(xdotool search --name "Mozilla Firefox" | head -1)
xdotool windowactivate $WID
xdotool key F5

Saved it as reload_firefox.sh and added to cron:

0 * * * * /bin/bash /home/pi/reload_firefox.sh

But, as I said, with the newer Grafana releases, it’s better to completely close and reopen Firefox after some hours.

And to completely close Firefox and reopen it on the same page, simply set the Firefox home with URL and parameter queries of the dashboard and then use this script:

#!/bin/bash
export DISPLAY=:0
pkill -9 firefox
sleep 3 
firefox --new-instance --kiosk &

Then add the script to cron and set it to run every 3/4 hours:

0 */4 * * * /bin/bash /home/pi/Documents/restart_firefox.sh

If you want, you can also add a line to release the RAM but, as I explained at the beginning, it doesn’t do much useful, because the system doesn’t see “purgable” RAM, so I preferred to remove it. If it might be useful to you here it’s:

0 */2 * * * sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches

And there’s also another useful way to clean the Firefeox cache using an extension called: Clear Cache, that uses the key F9 to clean the cache and reload the tab (you can customize the actions).

I set up a script to execute ‘F9’ every hour, mainly for security and to avoid issues when Firefox closes every 4 hours. To do this, just (obviously install the extension) and then use a script like this (always thanks to xdtool):

#!/bin/bash

export DISPLAY=:0

WID=$(xdotool search --name "Mozilla Firefox" | head -1)

if [ -n "$WID" ]; then
    xdotool windowactivate "$WID"
    sleep 1
    xdotool key F9  

fi

Set to run every hour on cron:

*/60 * * * * /bin/bash /home/pi/Documents/reload_firefox.sh

Integrated into home life

Leaving the display on even when I’m out of the house is not useful, because it consumes power and wears out itself. So, I added a Shortcut action to deactivate and reactivate the HDMI output when I leave and return home, using vcgencmd:

HomeKit automation HomeKit full

(the second script turn off also the display with the Pi-Hole stats and PADD, here more info and pics: PADD Hyper )

The Raspberry Pi can remain on because, as it doesn’t use the HDMI output, the energy usage drops significantly (to as low as few ~2 watts).

If I want to turn on or off the Raspberry PI and display, for example during the night or I stay outside for a long time, I have a simply shortcut that closes Firefox, turn off the raspberry PI and the smart plug.

turn off kiosk

To turn it on, obviously just turn on the kiosk smart plug and all startup automation will do the rest:

turn on kiosk

I’m using these shortcuts on the kiosk iPad I use for the home automations near the entrance, so I can simply tap on these shortcuts if I want to turn on off something :

Kiosk entrance Kiosk entrance

or from the iPhone Control Center:

iPhone control center

Conclusion

This is it. So, it’s easily to automate the whole process of using a RPi device as a kiosk with a big external HDMI display. Having this dashboards is very useful to me, particularly for temperatures, home energy consumption, and data usage. But leaving it always on, or having to manually turn it on and off, would be a bit stressful. Now it’s not, I can completely forget about it, but if I need to give a glance of the dashboards.

Tip: if for some reason I forgot to turn off the Raspberry PI before going to sleep, I configured cron to turn off the display (and on in the morning of course):

#display off at 23
00 23 * * * vcgencmd display_power 0

#display on at 7
00 07 * * * vcgencmd display_power 1

In summary, the complete cron file is:

#display off ore 23
00 23 * * * vcgencmd display_power 0

#display on ore 8
00 08 * * * vcgencmd display_power 1

#reload Firefox tab and clear the cache  every 1h (F9)
*/60 * * * * /bin/bash /home/pi/Documents/reload_firefox.sh

#restart firefox every 4h
0 */4 * * * /bin/bash /home/pi/Documents/restart_firefox.sh

And have a happy kiosking!