Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5 another question about booting #6

Merged
merged 11 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
# <img src="logo.png" alt="turntable image" width="60" /> balenaPhono
## A turntable phono/AUX/USB network streamer

balenaPhono is a project for Raspberry Pi that takes the audio output from a turntable or any other audio device and creates a shoutcast/icecast network stream. This project is great for anyone looking for a cheap and simple way to play vinyl on Sonos speakers.
balenaPhono is a project for Raspberry Pi that takes the audio output from a turntable or any other audio device and creates a shoutcast/icecast network stream. This project is great for anyone looking for a cheap and simple way to play vinyl on [Sonos](https://www.sonos.com/en-us/home) or [Ikea Symfonisk](https://www.ikea.com/us/en/cat/wi-fi-speakers-46194/) speakers.

### _*UPDATE!*_:
* I've updated the booter container to use the cron instead of a bash script `sleep` method. Now you use the `REBOOT_TIME` variable to specify the hour of the day you would like balenaPhono to reboot. This keeps the darkice stream from becoming unstable over time. If you have a better way to do this please make a pull request or open an issue.

---
### Equipment needed:
* Raspberry Pi (Tested with Pi3, running daily on Pi Zero W)
* Audio device:
1. Turntable with USB output -or-
2. Turntable with RCA output and [USB Phono Preamp](https://smile.amazon.com/s?k=usb+phono+preamp)
3. You can use a [USB audio capture card](https://smile.amazon.com/gp/product/B00WPVQXS0) if you want to connect your old Walkman or Diskman.
* A cheaper [USB phono preamp](https://smile.amazon.com/gp/product/B002GHBYZ0)
2. Turntable with RCA output and [USB Phono Preamp](https://www.amazon.com/s?k=usb+phono+preamp)
3. You can use a [USB audio capture card](https://www.amazon.com/s?k=usb+audio+capture+card) if you want to connect your old Walkman or Diskman.
* A cheaper [USB phono preamp](https://www.amazon.com/gp/product/B002GHBYZ0)
* The turntable I use: audio-technica [AT-LP60XUSB](https://www.audio-technica.com/en-us/turntables/best-for/new-to-vinyl/at-lp60xusb)

---
Expand Down Expand Up @@ -39,7 +42,8 @@ Running this project is as simple as deploying it to a balenaCloud application.
| PORTAL_PASSPHRASE | balenaPhono | Wifi-connect captive portal Passphrase |
| PORTAL_LISTENING_PORT | 8000 | *Changed from the default port 80 due to conflict with Icecast server |
| CHECK_CONN_FREQ | 120 | This is the wifi-connect polling wait time in seconds. The default is 120 seconds, I usually set mine to 3000. |
| REBOOT_SLEEP_TIME | 1d | Default is 1d (one day). Will work with d m s (days, minutes, seconds). It is a sleep time for the balenaBooter. Darkice audio streams have a tendency to get corrupted after a few days of up time. balenaBooter reboots the host once a day to keep things clean. Looking for a better way... if you have ideas. |
| REBOOT_TIME | 4 | Default is 4 (reboot every 24 hours at [4:00am](https://github.com/SamEureka/balenaPhono/issues/5) in the `America/Los_Angeles` timezone). Acceptable values are 0 through 23 representing the hour you want the device to reboot every day. Darkice audio streams have a tendency to get corrupted after a few days of up time. A python script triggered by cron reboots the host once a day to keep things clean. Looking for a better way... if you have ideas. |
| TZ | `America/Los_Angeles` | Sets the timezone. Look up your timezone [here](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List). Default is `America/Los_Angeles` |
| ICECAST_LOCATION | Interwebs | Where your stream is hosted from or where you are located. |
| ICECAST_ADMIN_EMAIL | balenaAdmin@localhost | Used for the Icecast status page, doesn't need to be real |
| ICECAST_CLIENTS | 10 | How many clients can connect to your stream. Keep it low if you are using a Pi Zero |
Expand Down
5 changes: 3 additions & 2 deletions balena.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ data:
- PORTAL_PASSPHRASE: balenaPhono
- PORTAL_LISTENING_PORT: 8000
- CHECK_CONN_FREQ: 120
- REBOOT_SLEEP_TIME: 1d
- REBOOT_TIME: 4
- TZ: America/Los_Angeles
- ICECAST_LOCATION: Interwebs
- ICECAST_ADMIN_EMAIL: balenaAdmin@localhost
- ICECAST_CLIENTS: 10
Expand Down Expand Up @@ -54,4 +55,4 @@ data:
- raspberry-pi2
- raspberrypi3
- raspberrypi4-64
version: 1.0.4
version: 2.0.1
2 changes: 0 additions & 2 deletions balenaBooter/Dockerfile.template

This file was deleted.

12 changes: 6 additions & 6 deletions balenaPhono/Dockerfile.template
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
## balenaPhono/Dockerfile.template ##
## Sam Dennon//2021 ##
## Sam Dennon//2023 ##

# Use balena distros, they have extra Charlie Unicorn magic built in!
FROM balenalib/%%BALENA_MACHINE_NAME%%-debian-python:3.9-buster
# Use balena base images, they have extra Charlie Unicorn magic built in!
FROM balenalib/%%BALENA_MACHINE_NAME%%-debian-python:3.10.10-bullseye

# Set the working directory
WORKDIR /balenaPhono

# Install required packages
RUN install_packages alsa-utils darkice icecast2 mime-support
RUN install_packages alsa-utils darkice icecast2 mime-support curl tzdata dbus

# Copy all the things
COPY . ./

# Check for balena device variables, apply or set defaults... build configs
RUN python3 phonoConfig.py
RUN python3 /balenaPhono/phonoConfig.py

# Makes stuff dynamically available in the container
ENV UDEV=1

# Start the services
CMD ["bash", "start.sh"]
CMD ["bash", "/balenaPhono/start.sh"]
5 changes: 3 additions & 2 deletions balenaPhono/start.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#/bin/bash

## balenaPhono/start.sh ##
## Sam Dennon//2021 ##
## Sam Dennon//2023 ##

# Build the config files
python3 phonoConfig.py
echo "Config files complete"

echo "Copying icecast2 service and config"
cp icecast2 /etc/default/
Expand All @@ -22,4 +23,4 @@ do
done

echo "USB device detected, starting Darkice stream"
darkice -c ./darkice.cfg
darkice -c ./darkice.cfg
19 changes: 19 additions & 0 deletions booter/Dockerfile.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## booter/Dockerfile.template ##
## Sam Dennon//2023 ##

# Use balena base images, they have extra Charlie Unicorn magic built in!
FROM balenalib/%%BALENA_MACHINE_NAME%%-debian-python:3.10.10-bullseye

# Set the working directory
WORKDIR /booter

# Install required packages
RUN install_packages tzdata cron dbus

RUN pip install requests python-dotenv pathlib

# Copy all the things
COPY . ./

# Start the booter
CMD ["bash", "/booter/booter.sh"]
24 changes: 24 additions & 0 deletions booter/booter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env python

## balenaPhono/booter.py ##
## Sam Dennon // 2023 ##

import os
import requests
from dotenv import load_dotenv
from pathlib import Path


dotenv_path = Path('/booter/.env')
load_dotenv(dotenv_path=dotenv_path)

# Get some stuff from env
BALENA_SUPERVISOR_ADDRESS = os.environ['BALENA_SUPERVISOR_ADDRESS']
BALENA_SUPERVISOR_API_KEY = os.environ['BALENA_SUPERVISOR_API_KEY']

# POST variables
URL = f"{BALENA_SUPERVISOR_ADDRESS}/v1/reboot?apikey={BALENA_SUPERVISOR_API_KEY}"
HEADERS = {"Content-Type": "application/json"}

# Send the reboot signal to the BALENA SUPERVISOR
requests.post(URL, headers=HEADERS)
15 changes: 15 additions & 0 deletions booter/booter.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

# Need to copy the env to a .env file so the booter.py script can access
# the environment variables. I could not figure out a better way to do this.
env >> /booter/.env
echo "Variables copied to .env"

# Let's update the crond to reboot the system every 24 hours
# at the time specified in the REBOOT_TIME env variable.
# (defaults to 4am in the timezone specified in the TZ variable.)
REBOOT_TIME="${REBOOT_TIME:=4}"
INCREMENT="${INCREMENT:=0}"
(echo "${INCREMENT} ${REBOOT_TIME} * * * /usr/local/bin/python /booter/booter.py > /proc/1/fd/1 2>&1") | crontab -
echo "cron updated"
exec cron -f
20 changes: 8 additions & 12 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,19 @@ services:
ports:
- "80:80/tcp"
- "80:80/udp"
balenaBooter:
build: ./balenaBooter
environment:
- 'REBOOT_SLEEP_TIME=1d'
- 'DBUS_SYSTEM_BUS_ADDRESS=unix:path=/host/run/dbus/system_bus_socket'
booter:
build: ./booter
restart: always
privileged: true
labels:
io.balena.features.dbus: 1
command:
- "/bin/sh"
- "-c"
- "sleep $REBOOT_SLEEP_TIME && dbus-send --system --print-reply --dest=org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager.Reboot"
- 'echo "Reboot delay set to $REBOOT_SLEEP_TIME"'
io.balena.features.supervisor-api: 1
io.balena.features.balena-api: 1
wifi-connect:
build: ./wifi-connect
restart: always
network_mode: host
privileged: true
labels:
io.balena.features.dbus: '1'
io.balena.features.firmware: '1'
io.balena.features.dbus: 1
io.balena.features.firmware: 1