<\/a><\/div>\nRunning a MagicMirror on a Raspberry\u202fPi alongside a desktop PC? Tired of manually changing inputs when the PC boots or shuts down? Here\u2019s a simple Bash script plus systemd service that:<\/p>\n\n\n\n
vcgencmd<\/code> working (make sure you\u2019re using the FKMS driver:
edit \/boot\/config.txt<\/code> and set dtoverlay=vc4-fkms-v3d # (comment out any dtoverlay=vc4-kms-v3d)<\/code> then reboot)
Source:<\/em> Raspberry\u202fPi docs \u2013 vcgencmd display_power<\/code> \ue202https:\/\/www.raspberrypi.com\/documentation\/computers\/os.html#display_power-0-1-1-display\ue201<\/a><\/li>\n\n\n\n- Ping<\/strong> utility (built into Linux) \u2013 exit code\u202f0 means host reachable \ue202https:\/\/ss64.com\/bash\/ping.html\ue201<\/a><\/li>\n<\/ul>\n\n\n\n
1. Create the Bash Script<\/h3>\n\n\n\n
Save the following as \/home\/pi\/autoHDMI.sh<\/code> and make it executable:<\/p>\n\n\n\n#!\/bin\/bash\n# autoHDMI.sh \u2014 turn Pi HDMI off\/on based on desktop presence\n\nDESKTOP_IP=\"192.168.0.114\" # \u2190 your desktop\u2019s IP\nINTERVAL=10 # seconds between checks\n\nhdmi_off=0\nwhile true; do\n if ping -c1 -W1 \"$DESKTOP_IP\" &>\/dev\/null; then\n if [[ $hdmi_off -eq 0 ]]; then\n \/usr\/bin\/vcgencmd display_power 0 # blank HDMI\n hdmi_off=1\n fi\n else\n if [[ $hdmi_off -eq 1 ]]; then\n \/usr\/bin\/vcgencmd display_power 1 # restore HDMI\n hdmi_off=0\n fi\n fi\n sleep \"$INTERVAL\"\ndone\n<\/code><\/pre>\n\n\n\nchmod +x \/home\/pi\/autoHDMI.sh\n<\/code><\/pre>\n\n\n\n2. Test the Commands Manually<\/h3>\n\n\n\n
Before automating, confirm the blank\/unblank work:<\/p>\n\n\n\n
# Blank the HDMI output\nsudo vcgencmd display_power 0\n\n# Restore it\nsudo vcgencmd display_power 1\n<\/code><\/pre>\n\n\n\nIf you see your monitor lose signal on \u201c0\u201d and regain it on \u201c1,\u201d you\u2019re good to go.<\/p>\n\n\n\n
3. Automate with systemd<\/h3>\n\n\n\n
Create the service file at \/etc\/systemd\/system\/autoHDMI.service<\/code>:<\/p>\n\n\n\n[Unit]\nDescription=Auto\u2011HDMI switching (Pi \u2194 Desktop)\nAfter=network-online.target\nWants=network-online.target\n\n[Service]\nUser=pi\nExecStart=\/home\/pi\/autoHDMI.sh\nRestart=always\nRestartSec=5\n\n[Install]\nWantedBy=multi-user.target\n<\/code><\/pre>\n\n\n\nEnable and start it:<\/p>\n\n\n\n
sudo systemctl daemon-reload\nsudo systemctl enable --now autoHDMI.service\n<\/code><\/pre>\n\n\n\n4. How It Works<\/h3>\n\n\n\n\n- Ping check<\/strong> every 10\u202fs (
ping -c1 -W1<\/code>) returns exit code\u202f0 if the desktop answers \ue202https:\/\/ss64.com\/bash\/ping.html\ue201<\/a><\/li>\n\n\n\n- Blank<\/strong>: when the desktop is up, the script runs
vcgencmd display_power 0<\/code><\/li>\n\n\n\n- Restore<\/strong>: when the desktop goes down, it runs
vcgencmd display_power 1<\/code><\/li>\n\n\n\n- Your monitor, set to Auto\u2011select<\/strong> input, sees \u201cno signal\u201d on HDMI\u202f1 and switches to your desktop (HDMI\u202f2). When the Pi\u2019s HDMI returns, it flips back to MagicMirror.<\/li>\n<\/ol>\n\n\n\n
5. We run an X\u2011screen \/ LXDE<\/strong><\/h3>\n\n\n\n