Last active
December 28, 2025 22:26
-
-
Save Dregu/1163dddcd879d0ed390707a2b3dead30 to your computer and use it in GitHub Desktop.
Scripts to start and stop moonlight systemd service automatically only when the TV is turned on, by connecting the 5V/500mA USB from the TV to Raspberry Pi (3B+) GPIO17 via a 25kΩ/50kΩ voltage divider for a (hopefully) suitable 3.3V signal. Smaller resistors might also work, but this is what I had and it works for me.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # This is for the magical gpio button, ignore if you're not going to solder one | |
| import dbus | |
| from gpiozero import Button | |
| from signal import pause | |
| sysbus = dbus.SystemBus() | |
| systemd1 = sysbus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1') | |
| manager = dbus.Interface(systemd1, 'org.freedesktop.systemd1.Manager') | |
| def on(): | |
| print('TV is ON') | |
| manager.StartUnit('moonlight-qt.service', 'fail') | |
| def off(): | |
| print('TV is OFF') | |
| manager.StopUnit('moonlight-qt.service', 'fail') | |
| tv = Button(pin=17, pull_up=False, bounce_time=1) | |
| tv.when_activated = on | |
| tv.when_deactivated = off | |
| if tv.is_pressed: | |
| on() | |
| else: | |
| off() | |
| pause() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This is for the magical gpio button, ignore if you're not going to solder one | |
| [Unit] | |
| Description=Moonlight GPIO button | |
| After=network.target | |
| [Service] | |
| ExecStart=/home/pi/moonlight-gpio.py | |
| Restart=always | |
| User=root | |
| [Install] | |
| WantedBy=multi-user.target |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This unit is disabled, but is started on boot by the other one if the TV is on, | |
| # or toggled by the GPIO cable when the tv turns on or off. | |
| # Or you can just enable it by default and ignore the other files. | |
| # QT version on RPi 4B | |
| # I'm using RaspiOS trixie Desktop (2025-12-04-raspios-trixie-arm64.img) | |
| # This setup is used as a 1080p60 HEVC remote screen only, over local ethernet. Latency is around 2f @ 60fps. | |
| # I don't really care about audio or input, but they should work too. | |
| # /boot/firmware/config.txt is all vanilla. | |
| # The desktop part is disabled in raspi-config (boot to console) and probably fiddled with some audio/video settings too. | |
| # These configs have nothing to do with the desktop, but you can try to wing it if you really want to use xorg or wayland. | |
| # I can't recommend running in wayland or xorg, it will have worse performance than eglfs on console. | |
| # Just figure out how to create systemd services and configure this crap over ssh. | |
| # And follow the official installation guide: | |
| # https://github.com/moonlight-stream/moonlight-docs/wiki/Installing-Moonlight-Qt-on-Raspberry-Pi-4 | |
| [Unit] | |
| Description=Moonlight QT stream | |
| After=network.target | |
| StartLimitIntervalSec=0 | |
| StartLimitBurst=0 | |
| [Service] | |
| Environment=QT_SCALE_FACTOR=0.9 | |
| Environment=QT_QPA_EGLFS_ALWAYS_SET_MODE=0 | |
| Environment=QT_QPA_PLATFORM=eglfs | |
| # Might need to explicitly switch to pipewire in raspi-config too | |
| Environment=SDL_AUDIODRIVER=pipewire | |
| # Force sound to specific device | |
| # Get node name with: pw-dump|grep node.name | |
| # pulsemixer is a good program to change and debug audio outputs over ssh | |
| #Environment=PIPEWIRE_NODE=bluez_output.BC_A0_42_70_07_24.1 | |
| Environment=PIPEWIRE_NODE=alsa_output.platform-fef00700.hdmi.hdmi-stereo | |
| # This fixed all my audio crackling issues on Pi4, adds tiny bit of audio latency | |
| # pw-top might be useful for checking errors and current latency | |
| Environment=PIPEWIRE_QUANTUM=960/48000 | |
| # You should probably pair and configure other stuff first by running moonlight-qt interactively | |
| ExecStart=moonlight-qt stream --platform minimal --1080 --bitrate 20000 10.0.0.10 Desktop | |
| Restart=always | |
| RestartSec=1 | |
| User=pi | |
| StandardOutput=truncate:/tmp/moonlight.log | |
| StandardError=inherit | |
| # You should systemctl disable getty@tty1 when using these two: | |
| PAMName=login | |
| TTYPath=/dev/tty1 | |
| [Install] | |
| WantedBy=multi-user.target |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This unit is disabled, but is started on boot by the other one if the TV is on, | |
| # or toggled by the GPIO cable when the tv turns on or off | |
| # Embedded version on RPi 3B+ | |
| # Requires vc4-fkms-v3d in config.txt to work | |
| # This my old bare minimum setup I sometimes used to stream youtube to the kitchen, never paid much attention how well audio etc works | |
| # Latency is probably better but smoothness is worse than the qt version on 4B, but qt really doesn't run on 3B. | |
| [Unit] | |
| Description=Moonlight stream | |
| After=network.target | |
| StartLimitIntervalSec=0 | |
| StartLimitBurst=0 | |
| [Service] | |
| ExecStart=moonlight stream -1080 -bitrate 20000 -app Desktop 10.0.0.10 | |
| Restart=always | |
| RestartSec=1 | |
| User=pi | |
| StandardOutput=truncate:/tmp/moonlight.log | |
| StandardError=inherit | |
| # You should systemctl disable getty@tty1 when using these two: | |
| PAMName=login | |
| TTYPath=/dev/tty1 | |
| [Install] | |
| WantedBy=multi-user.target |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # /etc/udev/hwdb.d/99-remote.hwdb | |
| # remap tv remote "ok" to spacebar | |
| # ymmv, copy scancode from: evtest /dev/input/event0 | |
| # reload: systemd-hwdb update; udevadm trigger | |
| evdev:name:vc4-hdmi-0:* | |
| KEYBOARD_KEY_0=space | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
