Skip to content

Instantly share code, notes, and snippets.

@Dregu
Last active April 8, 2025 20:55
Show Gist options
  • Save Dregu/1163dddcd879d0ed390707a2b3dead30 to your computer and use it in GitHub Desktop.
Save Dregu/1163dddcd879d0ed390707a2b3dead30 to your computer and use it in GitHub Desktop.
Scripts to start and stop moonlight or moonlight-embedded 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.
#!/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 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 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
# QT version on RPi 4B
# Using vc4-fkms-v3d in /boot/firmware/config.txt, it seems to report lower rendering times and might affect audio options too
# I also have some slight gpu_freq etc overclocks in there, might be important
# I'm using RaspiOS bookworm 64bit desktop 2024-11-19, still running kernel 6.6
# The desktop part is disabled in raspi-config (boot to console)
# 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 desktop, it will have worse performance than eglfs on console.
# Just figure out how to create systemd services and configure this crap over ssh.
# Official installation guide: https://github.com/moonlight-stream/moonlight-docs/wiki/Installing-Moonlight-Qt-on-Raspberry-Pi-4
# Some additional notes related to breakage on kernel 6.12, getting overlay stats to work etc: https://gist.github.com/Dregu/20b124b59bb281c0307dfedc553359e1
[Unit]
Description=Moonlight QT stream
After=network.target
StartLimitIntervalSec=infinity
[Service]
Environment=QT_SCALE_FACTOR=0.9
Environment=QT_QPA_EGLFS_ALWAYS_SET_MODE=0
Environment=QT_QPA_PLATFORM=eglfs
# Remember to switch to pipewire in raspi-config
Environment=SDL_AUDIODRIVER=pipewire
# Force sound to specific device
# Get node name with: pactl list sinks|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
# This fixed all my audio crackling issues on Pi4, adds tiny bit of audio latency
# pw-top might be useful for checking errors and 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
# The official build is going to need this until it's fixed, see https://github.com/moonlight-stream/moonlight-qt/issues/1496
KillSignal=SIGKILL
# You should systemctl disable getty@tty1 when using these two:
PAMName=login
TTYPath=/dev/tty1
[Install]
WantedBy=multi-user.target
# 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 use 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=infinity
[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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment