{"id":593,"date":"2025-07-16T12:47:39","date_gmt":"2025-07-16T10:47:39","guid":{"rendered":"https:\/\/it4home.dk\/?p=593"},"modified":"2025-07-16T13:58:06","modified_gmt":"2025-07-16T11:58:06","slug":"automatically-switch-raspberry-hdmi-between-magicmirror2-and-a-desktop","status":"publish","type":"post","link":"https:\/\/it4home.dk\/index.php\/2025\/07\/16\/automatically-switch-raspberry-hdmi-between-magicmirror2-and-a-desktop\/","title":{"rendered":"Automatically switch Raspberry\u202fHDMI between MagicMirror2 and a desktop"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-post pdfprnt-top-right\"><a href=\"https:\/\/it4home.dk\/index.php\/wp-json\/wp\/v2\/posts\/593?print=pdf\" class=\"pdfprnt-button pdfprnt-button-pdf\" target=\"_blank\" ><\/a><a href=\"https:\/\/it4home.dk\/index.php\/wp-json\/wp\/v2\/posts\/593?print=print\" class=\"pdfprnt-button pdfprnt-button-print\" target=\"_blank\" ><img decoding=\"async\" src=\"https:\/\/it4home.dk\/wp-content\/plugins\/pdf-print\/images\/print.png\" alt=\"image_print\" title=\"Print Content\" \/><\/a><\/div>\n<p class=\"wp-block-paragraph\">Running 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<ul class=\"wp-block-list\">\n<li><strong>Blanks<\/strong> the Pi\u2019s HDMI output whenever your desktop (IP <strong>192.168.0.114<\/strong>) is up<\/li>\n\n\n\n<li><strong>Restores<\/strong> the Pi\u2019s HDMI when the desktop shuts down<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Prerequisites<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Raspberry\u202fPi OS<\/strong> with <code>vcgencmd<\/code> working (make sure you\u2019re using the FKMS driver:<br>edit <code>\/boot\/config.txt<\/code> and set <code>dtoverlay=vc4-fkms-v3d # (comment out any dtoverlay=vc4-kms-v3d)<\/code> then reboot)<br><em>Source:<\/em> Raspberry\u202fPi docs \u2013 <code>vcgencmd display_power<\/code> \ue202<a href=\"https:\/\/www.raspberrypi.com\/documentation\/computers\/os.html#display_power-0-1-1-display%EE%88%81\">https:\/\/www.raspberrypi.com\/documentation\/computers\/os.html#display_power-0-1-1-display\ue201<\/a><\/li>\n\n\n\n<li><strong>Ping<\/strong> utility (built into Linux) \u2013 exit code\u202f0 means host reachable \ue202<a href=\"https:\/\/ss64.com\/bash\/ping.html%EE%88%81\">https:\/\/ss64.com\/bash\/ping.html\ue201<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">1. Create the Bash Script<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Save the following as <code>\/home\/pi\/autoHDMI.sh<\/code> and make it executable:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/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\" &amp;&gt;\/dev\/null; then\n    if &#91;&#91; $hdmi_off -eq 0 ]]; then\n      \/usr\/bin\/vcgencmd display_power 0    # blank HDMI\n      hdmi_off=1\n    fi\n  else\n    if &#91;&#91; $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\n<pre class=\"wp-block-code\"><code>chmod +x \/home\/pi\/autoHDMI.sh\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. Test the Commands Manually<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before automating, confirm the blank\/unblank work:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Blank the HDMI output\nsudo vcgencmd display_power 0\n\n# Restore it\nsudo vcgencmd display_power 1\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If 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<h3 class=\"wp-block-heading\">3. Automate with systemd<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create the service file at <code>\/etc\/systemd\/system\/autoHDMI.service<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Unit]\nDescription=Auto\u2011HDMI switching (Pi \u2194 Desktop)\nAfter=network-online.target\nWants=network-online.target\n\n&#91;Service]\nUser=pi\nExecStart=\/home\/pi\/autoHDMI.sh\nRestart=always\nRestartSec=5\n\n&#91;Install]\nWantedBy=multi-user.target\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Enable and start it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl daemon-reload\nsudo systemctl enable --now autoHDMI.service\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. How It Works<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Ping check<\/strong> every 10\u202fs (<code>ping -c1 -W1<\/code>) returns exit code\u202f0 if the desktop answers \ue202<a href=\"https:\/\/ss64.com\/bash\/ping.html%EE%88%81\">https:\/\/ss64.com\/bash\/ping.html\ue201<\/a><\/li>\n\n\n\n<li><strong>Blank<\/strong>: when the desktop is up, the script runs <code>vcgencmd display_power 0<\/code><\/li>\n\n\n\n<li><strong>Restore<\/strong>: when the desktop goes down, it runs <code>vcgencmd display_power 1<\/code><\/li>\n\n\n\n<li>Your monitor, set to <strong>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<h3 class=\"wp-block-heading\"><br>5. <strong>We run an X\u2011screen \/ LXDE<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Source: Raspberry&nbsp;Pi forum thread on disabling blanking with <code>xset<\/code> (<a href=\"https:\/\/forums.raspberrypi.com\/viewtopic.php?t=249837&amp;utm_source=chatgpt.com\">forums.raspberrypi.com<\/a>)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I stopped my MagicMirror Pi\u2019s X&nbsp;screen from going black by editing the LXDE <em>autostart<\/em> file and adding three\u202f<code>xset<\/code> lines.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Full file with the changes:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \/etc\/xdg\/lxsession\/LXDE-pi\/autostart        \n@lxpanel --profile LXDE-pi\n@pcmanfm --desktop --profile LXDE-pi\npoint-rpi\n\n# keep the X\u2011screen awake\n@xset s noblank     # no blanking\n@xset s off         # disable screensaver\n\n\nReboot and the display stays on permanently.<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">With this setup, your MagicMirror display takes over whenever your desktop is off, and your PC display reclaims the screen as soon as it\u2019s available\u2014no manual input switching needed!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Running 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: Prerequisites 1. Create&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":595,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[30,7],"tags":[],"class_list":["post-593","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","category-raspberry"],"_links":{"self":[{"href":"https:\/\/it4home.dk\/index.php\/wp-json\/wp\/v2\/posts\/593","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/it4home.dk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/it4home.dk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/it4home.dk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/it4home.dk\/index.php\/wp-json\/wp\/v2\/comments?post=593"}],"version-history":[{"count":3,"href":"https:\/\/it4home.dk\/index.php\/wp-json\/wp\/v2\/posts\/593\/revisions"}],"predecessor-version":[{"id":598,"href":"https:\/\/it4home.dk\/index.php\/wp-json\/wp\/v2\/posts\/593\/revisions\/598"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/it4home.dk\/index.php\/wp-json\/wp\/v2\/media\/595"}],"wp:attachment":[{"href":"https:\/\/it4home.dk\/index.php\/wp-json\/wp\/v2\/media?parent=593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/it4home.dk\/index.php\/wp-json\/wp\/v2\/categories?post=593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/it4home.dk\/index.php\/wp-json\/wp\/v2\/tags?post=593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}