SpeakerSaver is a Python-based solution designed to automatically manage and control your powered speakers connected via a smart plug. The primary goal is to ensure your speakers are turned off when not in use, specifically when no music has been played for a certain period or when the connected TV is off. This project also provides health monitoring, allowing you to check the status of the system.
- Automatically turns off speakers if no music is playing on Spotify or the TV is off.
- Integrates with Kasa smart plugs to control the power state of the speakers.
- Includes a health check endpoint to monitor the status of the running service.
- Logs system activities, with automatic log rotation to prevent log file overflow.
First, clone the repository to your local machine:
git clone https://github.com/yourusername/speakersaver.git
cd speakersaver
Create a virtual environment and install the required dependencies:
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
Run the setup script to configure the environment variables. This script will prompt you to input the necessary details, such as your Spotify CLIENT_ID
, CLIENT_SECRET
, and the IP addresses of your speakers and TV:
python setup_env.py
This script will generate a .env
file with your configurations.
The first time you run the api.py
script, you'll need to authorize your Spotify app. Start the Flask server:
python -m src.api
Navigate to the /authorize
endpoint in your browser to complete the Spotify authorization process. For example, if you're running the Flask server locally, visit http://localhost:8888/authorize
.
To run the main monitoring script:
python -m src.main
To start the Flask server for handling Spotify authorization and exposing health endpoints:
python -m src.api
Use scp
to copy the entire project directory to your Raspberry Pi:
scp -r /path/to/speakersaver pi@<raspberry_pi_ip>:/home/pi/speakersaver
SSH into your Raspberry Pi and navigate to the project directory:
ssh pi@<raspberry_pi_ip>
cd /home/pi/speakersaver
Create and activate a virtual environment, then install dependencies:
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
Run the setup script:
python3 setup_env.py
- Create script file:
sudo nano ~/start_services.sh
- Add the following script:
#!/bin/bash
echo "Starting speaker saver service"
sleep 10
echo "Starting virtual environment"
cd /home/plisker/speaker-saver
source venv/bin/activate
echo "Virtual environment is ready"
env > /home/plisker/speaker-saver/script_env.txt
# Run api and main scripts
python -m src.api &
API_PID=$!
echo "API server script started with PID: $API_PID"
python -m src.main &
MAIN_PID=$!
echo "Main script started with PID: $MAIN_PID"
wait $API_PID
wait $MAIN_PID
- Create a service file:
sudo nano /etc/systemd/system/speaker-saver.service
- Add the following configuration, making sure to change your paths as necessary:
[Unit]
Description=Speaker Saver Service
After=network.target
[Service]
Type=simple
ExecStart=/home/plisker/start_services.sh
WorkingDirectory=/home/plisker/speaker-saver
StandardOutput=inherit
StandardError=inherit
User=plisker
[Install]
WantedBy=multi-user.target
- Enable and start the service:
sudo systemctl enable speaker-saver.service
sudo systemctl start speaker-saver.service
If, on the Raspberry Pi, you get an error about the add_edge_detection
, you may need to run this after activating the venv:
sudo apt remove python3-rpi.gpio
SpeakerSaver logs its health status to health.log
and makes this information available through an HTTP endpoint exposed by the Flask server. You can monitor the system’s health by visiting the /health
endpoint provided by the Flask server and a simple log in the /logs
endpoint.
If you need to access the endpoints on your Raspberry Pi from your local computer, you can set up port forwarding. Add the following configuration to your ~/.ssh/config
file:
Host raspberrypi
HostName <raspberrypi-host>
User <raspberrypi-username>
LocalForward 8888 127.0.0.1:8888
After running ssh raspberrypi
, you will be able to access the endpoints on your computer by navigating to http://localhost:8888
. For example, the /health
endpoing will be http://localhost:8888/health
.
Please note that the HostName
may differ depending on your Raspberry Pi's configuration.
PIP Distribution: In the future, the project will be packaged for easy installation via PIP, eliminating the need for manual SCP transfers.