0

I have a Raspberry Pi Zero W and want it to use as an Internet Radio. So I wrote a Python script that connects to my bluetooth speaker and starts a stream. Everything works fine, but i always need to login and start the script.

Now I would love to play that stream automatically when the bluetooth speaker is turned on but have no clue how to do this.
Maybe someone can help me, so that the Pi permanently scans for the speaker and starts to play the stream?

Here is my script so far:

import os
import sys
import subprocess
import time

def blue_it():
    status = subprocess.call('ls /dev/input/event0 2>/dev/null', shell=True)
    while status == 0:
        print("Bluetooth UP")
        print(status)
        time.sleep(5)
    subprocess.call('mplayer http://addressToSomeStream.com', shell=True)
        status = subprocess.call('ls /dev/input/event0 2>/dev/null', shell=True)
    else:
        waiting()

def waiting():
    subprocess.call('killall -9 pulseaudio', shell=True)
    time.sleep(2)
    subprocess.call('pulseaudio --start', shell=True)
    time.sleep(2)
    status = subprocess.call('ls /dev/input/event0 2>/dev/null', shell=True)  
    while status == 2:
        print("Bluetooth DOWN")
        print(status)
        subprocess.call('~/scripts/autopair', shell=True)
        time.sleep(15)
        status = subprocess.call('ls /dev/input/event0 2>/dev/null', shell=True)
    else:
        blue_it() 

blue_it()
0

I think your question is how do I auto-boot this script on the pizero. If it is, then the recognised way is via command line to sudo nano /etc/profile

and after last 'fi'

type sudo python3 /.....your file path and full file name

Then save this via control X ...Y Pi zero will now autoboot this script every time it restarts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.