Migrating to a Raspberry Pi 4 on MagicMirror²

Published by Oliver on

I have run the awesome MagicMirror² software on a Pi Zero W in the past. While this works ok the small Pi is not powerful enough for more complex widgets. Here is how the Pi 4 compares and how I set it up to run my magic mirror.

Setting up a Raspberry Pi 4 for your magic mirror

I have built my own magic mirror last year and ran the software on a Pi Zero W. It was running pretty well but struggled with some of the more intensive tasks like showing a globe. It was also always running at >90% CPU usage.

Then some two weeks ago the Pi Zero W (or rather the SD card I think) died an (un)expected death. SD cards are a known failure point for Pis but instead of just replacing it decided to replace the whole Pi with a more powerful model. It is quite a lot of work to remove the Pi anyways are the prices for the Pi 4 are finally dropping again.

The preparation of the Pi was pretty easy. I got a model with a nice metal heat sink that I mounted. Meanwhile the SD card was flashed using the Raspberry Pi Imager using the newest Pi OS. I also made sure to use the config menu of the imager to provide Wifi details and set up SSH keys. Make sure to test your setup before closing the mirror!

If you want to support the blog consider getting your hardware via these affiliate links. Here is what I used
Raspberry Pi 4
Case/cooler
USB power supply
micro HDMI cable
micro SD card
SD card reader

You will also need a new power supply (USB C) and (micro) HDMI cable. Make sure to have those ready too. The micro SD card can be the same, of course unless it died like in my case.

Monitoring for the Pi

After installing the Pi I made sure to add proper monitoring like I already set up for my home server and the Octopi using the MQTT2HA daemon. The setup is quite simple if you follow my article and the official documentation and provides me with always up-to-date data about CPU/memory/… usage. Very neat for debugging purposes.

Pi 4 with MagicMirro – Grafana dashboard

Of course I integrated all that data into my Grafana dashboard via Home Assistant and InfluxDB. Tracking temperature is quite important, maybe I will create a warning automation in Home Assistant later to get notified if temperatures rise to unexpected values and the Pi 4 can get a bit warmer than the Zero W.

Rotating the display of a Raspberry Pi 4

Before getting to the part of running MagicMirror² there is one annoying problem to solve: my screen is vertically oriented, meaning it is rotate 90° to the right. With older Pi’s this used to be easy to set up: just edit the /boot/config.txt file.

Supposedly the solution I already described for the Pi Zero should work:

DISPLAY=:0 xrandr --output HDMI-1 --rotate right

This does actually rotate the screen but this does not survive a restart. For some weird reason adding this to the mm.sh starter script also did not help. Instead I had to fall back to slightly more complicated solution.

Via sudo raspi-config and then Interface options and VNC you can enable the VNC server (something like a remote desktop on Windows). Then you can connect to that from another machine (I used real VNC viewer here) and get remote access to the actual screen, not just the terminal via SSH.

On the desktop you can go to the main menu, select configuration and screen. There you can change the orientation and restart the Pi. After the restart it will now persistently rotate the screen. It seems like this is done by the /usr/share/dispsetup.sh file. If you want to manually edit that, here is the content of mine:

#!/bin/sh
if ! raspi-config nonint is_pi || raspi-config nonint is_kms ; then
if xrandr --output HDMI-1 --primary --mode 1920x1080 --rate 60.000 --pos 0x0 --rotate right --output HDMI-2 --off --dryrun ; then
xrandr --output HDMI-1 --primary --mode 1920x1080 --rate 60.000 --pos 0x0 --rotate right --output HDMI-2 --off
fi
fi
if [ -e /usr/share/tssetup.sh ] ; then
. /usr/share/tssetup.sh
fi
if [ -e /usr/share/ovscsetup.sh ] ; then
. /usr/share/ovscsetup.sh
fi
exit 0

Installing MagicMirror² on the Raspberry Pi 4

The actual installation of the MagicMirror software on the Pi 4 is quite easy. For the Zero W I described a manual approach but also mentioned the automatic script. The same script was also used to install the software on my Pi 4. Just SSH onto the Pi and run

bash -c  "$(curl -sL https://raw.githubusercontent.com/sdetweil/MagicMirror_scripts/master/raspberry.sh)"

Never trust anything from the Internet though so have a look at the script beforehand ;). The installation will only take a couple of minutes max on the Pi 4. Once this is done it should start the MagicMirror software. You can use the pm2 process manager to control it:

pm2 list # show all running process
pm2 stop MagicMirror # stop the software
pm2 start MagicMirror # start the software
pm2 restart MagicMirror # you get the idea
pm2 logs MagicMirror # show all logs

Then you can start installing all the modules you want to use. You can find a full list here. Usually you have to go to the modules directory, clone the repository of the module and then update the configuration file at config/config.js to include the new module.

Currently I am using these modules:

The WifiPassword module is used to show a QR code giving access to my guest Wifi (separated VLAN), the public transport one shows train/bus times for a station near me and the globe module shows pictures of our earth from a satellite.

The remote control module is used for my motion sensor automation. Similar to the old setup I have a motion sensor nearby that should control when the monitor is on or off to save some energy. Here I ran into another difference to my older setup though. I used to use the cec-client to interact with the monitor but that seems to not be available anymore. Instead cec-ctl can be used.

It was already installed in my case and can be used like:

// to set device as TV
cec-ctl -d/dev/cec0 --tv -S // init the screen as a TV
cec-ctl -d/dev/cec0 --to 0 --standby // turn off
cec-ctl -d/dev/cec0 --to 0 --image-view-on // turn on

First you need to let the system know that it should treat the screen cec0 (which in my case is the HDMI input the screen is connected to) as a TV (–tv flag). -S will show some more information about the device. Then you can use --standby and --image-view-on flags to turn the TV on or off. Unfortunately this takes a couple of seconds even with the Pi 4.

To always have the screen set up properly I added the first line to the installers/mm.sh file which now looks like:

cd ~/MagicMirror
cec-ctl -d/dev/cec0 --tv -S
DISPLAY=:0 npm start

I used the on/off commands in the configuration of the module like this:

{
        module: 'MMM-Remote-Control',
        config: {
                customCommand: {
                        monitorOnCommand: "cec-ctl -d/dev/cec0 --to 0 --image-view-on",
                        monitorOffCommand: "cec-ctl -d/dev/cec0 --to 0 --standby"
                },
                showModuleApiMenu: false,
                secureEndpoints: false, // Optional, See API/README.md
        }
},

Now the /api/monitor/{on/off} endpoints can be used to control the monitor. In Home Assistant I have created a rest_command service for this and automated the screen via the motion sensor. You can find all those details here.

I am pretty happy with the current setup but will for sure keep experimenting.