In my youth, my favorite wrestler was definitely Shawn Michaels, the “Heartbreak Kid”. A great athlete and performer, everything he did was ludicrously entertaining.
That began the moment he entered the building and this music played:
That had me thinking.. if Shawn Michaels can arrive like this, why can’t I?
Well, with a Raspberry Pi and some Python chops, a fella like me (or you!) just might be able to hook that up.
Well, there are several options.
We could detect when your phone attaches to your wireless network. This is useful because the Raspberry Pi knows that it’s definitely you and not your housemate or family member stealing your sweet moves.
The downside is that it’s much less precise about how close you are. I mean, it’s entrance music, right? It should start right when you’re through the door.
You could instead use a PIR motion sensor. This can precisely match the music to the entrance, although it’s not at all fussy about who triggers it.
So, let’s combine both. The Raspberry Pi will wait until your phone connects to your wireless network to know you’ve arrived home and will then wait for the sensor to detect your exact moment of entrance.
If you would prefer to only use one of these things, you can easily cut the other bit out.
This project also introduces Python’s subprocess module. This is in the standard library, which means you don’t need to install it.
This lets us run shell commands from inside Python. This is super convenient; if you already know how to do something from the command line, you can just run that command.
The two commands we’ll use are mpg123 and ping.
mpg123 is a command line utility to play mp3 files. We’ll need to install mpg123.
You don’t need to install ping; it’s there already. This is a simple tool to check whether a remote server or device is alive and responding. We’ll be using this to check whether your phone is connected to your wireless network.
For this project, you’ll need the following:
A Raspberry Pi with power supply, wireless connectivity, and an SD card | ![]() |
A PIR motion sensor
(I’m using an XC-4444) |
![]() |
A high-quality MP3 of Sexy Boy by Shawn Michaels (or a different song if you absolutely insist) | |
Some way to play music from your Raspberry Pi; a home entertainment system connected to the HDMI port is ideal |
You’ll also need a smartphone, a wireless home network, and 3 male to female jumper cables.
Make sure your phone is set up to connect to your home’s wireless network automatically, and that you aren’t using any power saving settings to disable wireless networking when it’s not in use.
We’re also assuming you’re already familiar with the PIR motion sensors and the GPIO Zero library. If not, please check out this introduction first. You’ll learn how the device works, and we will be wiring it up in much the same way for this project.
This has been tested using the Raspberry Pi OS Buster, but older versions of Raspbian should be fine too. The Python modules and Linux commands we’re using have been around for a very long time.
Let’s start by creating a directory for this project and switching to it. Open a terminal and type:
mkdir ~/entrancemusic
cd ~/entrancemusic
Copy your mp3 into this directory by whatever method you prefer.
Then update your system and install mpg123 with these commands:
sudo apt update && sudo apt upgrade -y
sudo apt install mpg123
Let’s avoid the breadboard this time. For what we’re doing, it’s in the way.
Grab your PIR motion sensor and your jumper wires. Connect the sensor’s power pin to the 5 volt power pin on the Raspberry Pi. Then connect the sensor’s ground pin to the ground pin on the Raspberry Pi.
Then attach the digital output to a GPIO pin. I’m using pin 24 this time, just because it’s conveniently placed.
That’s the motion sensor all wired up. Point it so that it faces the doorway you’ll enter through.
Almost all home wireless networks uses DHCP to automatically assign an available IP address to new devices as they connect. This is convenient because it’s reliable and it frees you up from having to administer it yourself.
This also means that a particular device’s IP address can change from time to time. For this project, we want your phone to reliably appear on the same IP address at all times, and for nothing else to use that IP address.
Most routers will assign an IP address to a device for at least a day or so. So if you are only going to do this once, have a laugh and move on with your life, don’t even worry about this.
If you want to run this script again, you should reserve an IP address for your device. You can do this by logging in to the admin panel of your wireless router. Different routers handle this a little bit differently, though it is generally quite easy to do. If you get stuck, use a search engine to find the manual for your device.
I have my phone on a reserved IP address of 192.168.0.5. Yours will likely be on a different IP address; just substitute it in as you follow along.
And while you’re there – are you at all in the habit of using SSH to access your Raspberry Pi over the local network? Reserve an IP address for that too. It’s that little bit more convenient if it never switches IP address.
To use mpg123 from the command line, just type it with the name of the mp3 you want to play, like this:
mpg123 ‘Sexy Boy (Shawn Michaels).mp3’
Running this command from Python is not much more complicated.
First, import the run function from the subprocess module. Then pass this function the command as the first argument, and “shell=True” as the second, like this:
from subprocess import run
run(“mpg123 ~/entrancemusic/’Sexy Boy (Shawn Michaels).mp3′”, shell=True)
From the command line, you can ping your phone like this:
ping -c 192.168.0.5
Remember to substitute in your phone’s IP address. We add that “-c 1” to tell ping to only send one request. If we don’t include it, it will keep going until it’s canceled.
If the device responds, ping will return successfully. If it doesn’t respond, it will return an error code.
Ideally, we could use this error code to see whether the phone is connected to the network. Unfortunately, many smartphones just don’t respond to echo requests. That means ping can return an error code whether your phone is on the network or not.
Instead, let’s look at the output. If no device is on the specified IP address, it will include the phrase “Destination Host Unreachable”.
Python’s subprocess module includes Popen and PIPE objects which let us to process the output, like this:
from subprocess import Popen, PIPE
p1 = Popen([“ping”, “-c”, “1”, “192.168.0.5”], stdout=PIPE)
stdout_value = p1.communicate()[0]
If you want to understand all the moving parts in this code, read here. For this project, it’s enough to know that this runs the ping command and then assigns the output to stdout_value.
We can use this to write a loop that breaks when this “Destination Host Unreachable” doesn’t appear in the output.
while True:
p1 = Popen([“ping”, “-c”, “1”, “192.168.0.5”], stdout=PIPE)
stdout_value = p1.communicate()[0]
if b’Destination Host Unreachable’ not in stdout_value:
break
sleep(2)
This loop keeps running until it sees that your phone is on the network.
We now have all the building blocks for a simple entrance music program. Open a new file by typing:
nano entrancemusic.py
Then type (or paste) in the following.
from time import sleep
from signal import pause
from subprocess import run, Popen, PIPE
from gpiozero import MotionSensor
sleep(600) # This gives you 10 minutes to leave your home
pir = MotionSensor(24)
while True:
p1 = Popen([“ping”, “-c”, “1”, “192.168.0.5”], stdout=PIPE)
stdout_value = p1.communicate()[0]
if b’Destination Host Unreachable’ not in stdout_value:
break
sleep(2)
sleep(5)
pir.wait_for_motion()
run(“mpg123 ~/entrancemusic/’Sexy Boy (Shawn Michaels).mp3′”, shell=True)
Save and exit nano.
When you want to schedule your entrance music for your return, run this script just before you leave your home, by typing:
python3 entrancemusic.py
This script works as it is, but there are a few parts you might want to play with.
Timing Your Entrance
You might have noticed that this script sleeps for 5 seconds before telling the sensor to wait for motion. Why?
It’s to limit the window of time available for someone else to steal your entrance. This matters most if you have the motion sensor set up in a shared area, such as a living room or hall.
Is 5 seconds the correct length of time to wait? This depends on the range of your wireless network and the layout of your home. If your sensor is pointed right at your front door, you might not want any delay at all. Have a play with this value to find what works for you.
Image: Raspberry Pi
What if you end up going to dinner or the pub, come home late and wake everyone up with the dulcet tones of Shawn Michaels? If you are completely committed to your role, this is an excellent opportunity to goad these rivals and establish dominance.
As for me? I’m reluctant to blast loud music any later than 10pm.
We can check the time using a localtime() function in the time module. We can import it by changing the first line of our script to this:
from time import sleep, localtime
We can then check that it’s between 9am and 10pm with the following comparison:
9 < localtime().tm_hour < 22
From here it’s just a matter of wrapping our mpg123 command in an if-statement.
if 9 < localtime().tm_hour < 22:
run(“mpg123 ~/entrancemusic/’Sexy Boy (Shawn Michaels).mp3′”, shell=True)
Tweak these hours of operation as you please to suit your situation.
Exceedingly few people on the planet will ever be able to make it big as professional wrestlers. Thanks to Python and Raspberry Pi, you can still enjoy the big entrance.
Are there any further tweaks you would make to this? Leave your ideas in the comments!