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.

-1

You can do it using rc.local file. Write this command in terminal:

sudo nano /etc/rc.local

Now at the end of the file, write this:

python3 path/to/file.py

Now press ctrl+x followed by y and hit enter. Then reboot your pi. Now your code will run automatically after every start-up.

  • Please take note that using /etc/rc.local has limitations due to Compatibility with SysV. We have seen many problems here on this site using it. Following the recommendation of the developers from systemd you should avoid using it. – Ingo Dec 17 '19 at 17:27
  • Okay. I'll take care in future. But I think everyone here is expected to provide a better solution to the main question instead of finding problem in existing answers and down-vote them. – theashwanisingla Dec 17 '19 at 17:32
  • It's really time to forget this old deprecated error prone stuff and learn how to use systemd Units. An answer with a Unit file could be a good exercise. – Ingo Dec 17 '19 at 17:40

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.