Did you ever want to have your return to your office or home announced with a fanfare?
Probably not, but let’s do this anyway 😉
This script will use Bluetooth (built into the Raspberry Pi 3) to detect when your phone is nearby, and if detected will play the MP3 of your choice!
See it in action:
Setting up
You will need some prerequisites:
- Bluetooth developer libraries
sudo apt-get install libbluetooth-dev
Bluez Bluetooth tools
sudo apt-get install bluez
Python library
sudo python3 -m pip install pybluez
If you want to convert your audio to mp3
sudo apt-get install ffmpeg
Nice tool for grabbing YouTube and converting to MP3
sudo python3 -m pip install youtube-dl
Eg.
youtube-dl -x --audio-format mp3 https://www.youtube.com/watch?v=-bzWSJG93P8
- Media player for the Pi command line
sudo apt-get install mplayer
Code
In this simple code we will keep looping and looking for a specific device name, in my case “Chrisg”. When it is found we launch Mplayer with the specific audio.
import os import bluetooth while 1: print("Searching ...") devices = bluetooth.discover_devices(duration=10, lookup_names=1, flush_cache=1) for device_address, device_name in devices: print("Found: {}".format(device_name)) if(device_name.lower() == "chrisg"): os.system("mplayer /home/pi/imperial.mp3")