update script, daemon, services, added sound, small improvements

This commit is contained in:
Lurkars 2022-02-08 20:09:26 +01:00
parent 5fdc7d636b
commit c769724146
14 changed files with 671 additions and 433 deletions

410
README.md
View File

@ -1,29 +1,30 @@
# luniebox # luniebox
lunibox is a RFID jukebox based on a Raspberry Pi. It is similar to the [Phoniebox](https://www.phoniebox.de) \[[https://github.com/MiczFlor/RPi-Jukebox-RFID](GitHub)\] and an upgrade of the [TonUINO (de)](https://www.voss.earth/tonuino/) which both are DIY versions of the popular Toniebox©. The main focus for now is to play Spotify© content and playing local files (also supports Spotify© download for offline use). Other sources will be integrated later. lunibox is a RFID jukebox based on a Raspberry Pi. It is similar to the [Phoniebox](https://www.phoniebox.de) \[[GitHub](https://github.com/MiczFlor/RPi-Jukebox-RFID)\] and an upgrade of the [TonUINO (de)](https://www.voss.earth/tonuino/) which both are DIY versions of the popular Toniebox©. The main focus for now is to play Spotify© content and playing local files (also supports Spotify© download for offline use). Other sources will be integrated later.
## Prerequisites ## Prerequisites
You need to be familiar with ssh (Putty on Windows). For the usage of Spotify© a premium account is required. You need to be familiar with ssh (Putty on Windows). For the usage of Spotify© a premium account is required.
### Hardware ### Hardware
- Raspberry Pi (tested with Raspberry Pi 3 Model B, Zero W, Zero 2 W) \[starting at ~14€\] - Raspberry Pi (tested with Raspberry Pi 3 Model B, Zero 2 W) \[starting at ~14€\]
- power supply (5V ~2.5A) \[starting at ~6€\] - power supply (5V ~2.5A) \[starting at ~6€\]
- Micro SD card (at least 4GB) \[starting at ~4€\] - Micro SD card (at least 4GB) \[starting at ~4€\]
- RC522 RFID Reader connected to [TODO] \[starting at ~1.50€\] - RC522 RFID Reader \[starting at ~1.50€\]
- RFID (MiFare) cards or chips \[starting at ~2€\] - RFID (MiFare) cards or chips \[starting at ~2€\]
- a Audio Card: - a Audio Card:
- Pimoroni Audio Amp SHIM (3W Mono Amp) \[starting at ~11€\] with passive speaker \[starting at ~5€\] - Pimoroni Audio Amp SHIM (3W Mono Amp) \[starting at ~11€\] with passive speaker \[starting at ~5€\]
- Pimoroni Audio DAC SHIM \[starting at ~14€\] with active speaker - Pimoroni Audio DAC SHIM \[starting at ~14€\] with active speaker
- Adafruit Speaker Bonnet for Raspberry Pi - Adafruit Speaker Bonnet for Raspberry Pi
- ...something else (use custom setup!) - ...something else (use custom setup!)
- (optional) MPU9250 9-axis sensor [TODO] \[starting at ~1.50€\] - (optional) 2x Push Buttons for volume control \[starting at ~1€\]
- (optional) MPU9250 9-axis sensor \[starting at ~1.50€\]
- (optional) Waveshare UPS HAT + 2x 18650 18650 Li battery \[starting at ~30€\] - (optional) Waveshare UPS HAT + 2x 18650 18650 Li battery \[starting at ~30€\]
- some wires or dupont connectors \[starting at ~2€\] - some wires or dupont connectors \[starting at ~2€\]
- case for all above - case for all above
- depending on hardware: soldering equipment - depending on hardware: soldering equipment
A minimal setup (Raspberry Zero 2 W, power supply, Micro SD, RC522, Cards, Audio Amp) should be about ~40€ plus case materials. A minimal setup (Raspberry Zero 2 W, power supply, Micro SD, RC522, Cards, Audio Amp, Speaker, Buttons) should be about ~45€ plus case materials.
### Software ### Software
- latest Raspberry Pi OS blank installation on Micro SD card (Instructions: [raspberrypi.com/software/](https://www.raspberrypi.com/software/)) - latest Raspberry Pi OS blank installation on Micro SD card (Instructions: [raspberrypi.com/software/](https://www.raspberrypi.com/software/))
@ -36,209 +37,278 @@ If you're using a Raspberry Pi Zero or have missing peripherals to setup WiFi an
- insert Micro SD card with Raspberry Pi OS into a computer - insert Micro SD card with Raspberry Pi OS into a computer
- place an empty file called `ssh` into `/boot` folder/partioon - place an empty file called `ssh` into `/boot` folder/partioon
- place a filed called `wpa_supplicant.conf` into `/boot` folder/partition with following content - place a filed called `wpa_supplicant.conf` into `/boot` folder/partition with following content
``` > ```
country=$COUNTRY_CODE > country=$COUNTRY_CODE
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev > ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1 > update_config=1
>
network={ > network={
ssid="$WIFI_SSID" > ssid="$WIFI_SSID"
psk="$WIFI_PASSWORD" > psk="$WIFI_PASSWORD"
} > }
``` > ```
- replace `$COUNTRY_CODE` with upper-case country code (eg. GB or DE) and `$WIFI_SSID` and `$WIFI_PASSWORD` with your WiFi credentials - replace `$COUNTRY_CODE` with upper-case country code (eg. GB or DE) and `$WIFI_SSID` and `$WIFI_PASSWORD` with your WiFi credentials
- plug card back into your Pi and connect power supply - plug card back into your Pi and connect power supply
## Setup ## Setup
Establish a ssh connection to your Pi: Open terminal on your pi or establish a ssh connection to your Pi:
- the Pi should be reachable under `raspberrypi` or `raspberrypi.local` in your network, if not try to find out it's IP address from your router. - the Pi should be reachable under `raspberrypi` or `raspberrypi.local` in your network, if not try to find out it's IP address from your router.
- default username is `pi` and default password is `raspberry` - default username is `pi` and default password is `raspberry`
- Example: `ssh pi@raspberrypi.local` or `ssh pi@192.168.2.100` - Example: `ssh pi@raspberrypi.local` or `ssh pi@192.168.2.100`
> ⚠️ Warning: you should change the default password by executing `passwd` after login
> **⚠️ Warning: you should change the default password by executing `passwd` after login**
### Automatic setup ### Automatic setup
> ⚠️ Warning: executing scripts from the internet without checking is bad. This is only done to get things done fast. In any doubts you can perform the [Manual setup](#manual-setup) and execute all commands step-by-step to understand what's going on. > **⚠️ Warning: executing scripts from the internet without checking is bad. This is only done to get things done fast. In any doubts you can perform the [Manual setup](#manual-setup) and execute all commands step-by-step to understand what's going on.**
- download and excecute setup script (you will be prompted to confirm certain steps beforehand anyway) - download and excecute setup script (you will be prompted to confirm certain steps beforehand anyway)
> `curl https://git.bstly.de/Lurkars/luniebox/raw/branch/main/luniebox.sh -o luniebox.sh`
> ```
> `chmod +x luniebox.sh` bash <(curl -s https://git.bstly.de/Lurkars/luniebox/raw/branch/main/luniebox.sh)
> ```
> `./luniebox.sh`
- Alternative first download script, take a look and then execute
```
curl https://git.bstly.de/Lurkars/luniebox/raw/branch/main/luniebox.sh -o luniebox.sh
chmod +x luniebox.sh
./luniebox.sh
```
### Manual setup ### Manual setup
#### Software setup #### Software setup
go to home directory - go to home directory
> `cd /home/pi`
```
cd /home/pi
```
install `git`, `python3-venv` and `python3-pip` - install `git`, `python3-venv` and `python3-pip`
> `sudo apt install -y git python3-venv python3-pip`
```
sudo apt install -y git python3-venv python3-pip
```
clone repository `https://git.bstly.de/Lurkars/luniebox.git` with sources and config: - clone repository `https://git.bstly.de/Lurkars/luniebox.git` with sources and config:
> `git clone https://git.bstly.de/Lurkars/luniebox.git luniebox`
```
git clone https://git.bstly.de/Lurkars/luniebox.git luniebox
```
setup application ##### Setup luniebox application and daemon
> `cd /home/pi/luniebox/application` - setup application by building python venv and setup systemd service
>
> `python -m venv venv` ```
> cd /home/pi/luniebox/application
> `source venv/bin/activate`
> python -m venv venv
> `export CFLAGS=-fcommon`
> source venv/bin/activate
> `pip install -r requirements.txt`
> export CFLAGS=-fcommon
> `deactivate`
> pip install -r requirements.txt
> `mkdir /home/pi/luniebox/config`
> deactivate
> `cp /home/pi/luniebox/contrib/config/luniebox.cfg /home/pi/luniebox/config/luniebox.cfg`
> mkdir /home/pi/luniebox/config
> `sudo cp /home/pi/luniebox/contrib/service/luniebox-app.service /etc/systemd/system/`
> cp /home/pi/luniebox/contrib/config/luniebox.cfg /home/pi/luniebox/config/luniebox.cfg
> `sudo cp /home/pi/luniebox/contrib/service/luniebox-daemon.service /etc/systemd/system/`
> sudo cp /home/pi/luniebox/contrib/service/luniebox-app.service /etc/systemd/system/
> `sudo systemctl daemon-reload`
> sudo cp /home/pi/luniebox/contrib/service/luniebox-daemon.service /etc/systemd/system/
> `sudo systemctl enable luniebox-app luniebox-daemon`
sudo systemctl daemon-reload
sudo systemctl enable luniebox-app luniebox-daemon
```
##### Setup spotify
setup spotifyd For Spotify support, spofiyd is required.
> `mkdir /home/pi/luniebox/bin`
>
> `wget -c https://github.com/Spotifyd/spotifyd/releases/download/v0.3.3/spotifyd-linux-armv6-slim.tar.gz -O - | tar -xz -C /home/pi/luniebox/bin`
>
> `cp /home/pi/luniebox/contrib/config/spotifyd.cfg /home/pi/luniebox/config/spotifyd.cfg` (if you use other audio hardware, you may need to adjust the `backend` and `device` properties to your needs!)
>
> `sudo cp /home/pi/luniebox/contrib/service/spotifyd.service /etc/systemd/system/`
>
> `sudo systemctl daemon-reload`
>
> `sudo systemctl enable spotifyd`
setup mpd - get spotifyd binary and setup systemd service
> `mkdir /home/pi/luniebox/library`
> ```
> `sudo apt install -y mpd` mkdir /home/pi/luniebox/bin`
>
> `sudo cp /home/pi/luniebox/contrib/config/mpd.conf /etc/mpd.conf` (if you use other audio hardware, you may need to adjust the `audio_output` section to your needs!) wget -c https://github.com/Spotifyd/spotifyd/releases/download/v0.3.3/spotifyd-linux-armv6-slim.tar.gz -O - | tar -xz -C /home/pi/luniebox/bin
>
cp /home/pi/luniebox/contrib/config/spotifyd.cfg /home/pi/luniebox/config/spotifyd.cfg
sudo cp /home/pi/luniebox/contrib/service/spotifyd.service /etc/systemd/system/`
sudo systemctl daemon-reload`
sudo systemctl enable spotifyd
```
setup ClSpotify > If you use other audio hardware, you may need to adjust the `backend` and `device` property in `/home/pi/luniebox/config/spotifyd.cfg` to your needs!
> `git clone https://github.com/agent255/clspotify.git /home/pi/clspotify`
>
> `cd /home/pi/clspotify`
>
> `python -m venv venv`
>
> `source venv/bin/activate`
>
> `pip install -r requirements.txt`
>
> `deactivate`
>
> `sed -i "s/^zspotify_path =.*$/zspotify_path = \/home\/pi\/clspotify\//" /home/pi/luniebox/config/luniebox.cfg`
start/restart all services ##### Setup Music Player Daemon
> `sudo systemctl restart mpd spotifyd luniebox-daemon luniebox-app`
for offline file support setup [Music Player Daemon](https://www.musicpd.org/) (mpd)
- create library folder and install `mpd`
```
mkdir /home/pi/luniebox/library
sudo apt install -y mpd
sudo cp /home/pi/luniebox/contrib/config/mpd.conf /etc/mpd.conf
```
> If you use other audio hardware, you may need to adjust the `audio_output` in `/etc/mpc.conf` section to your needs!
##### Setup ClSpotify
for Spotify offline support setup [ClSpotify](https://github.com/agent255/clspotify.git) (requires [spotify](#setup-spotify) and [mpd](#setup-music-player-daemon)!).
- get code, install pythen venv and update config
```
git clone https://github.com/agent255/clspotify.git /home/pi/clspotify
cd /home/pi/clspotify
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
deactivate
sed -i "s/^zspotify_path =.*$/zspotify_path = \/home\/pi\/clspotify\//" /home/pi/luniebox/config/luniebox.cfg
```
#### Hardware Setup #### Hardware Setup
##### enable SPI for RFID Reader For a detailed Raspberry Pi pinout take a look at [https://pinout.xyz](https://pinout.xyz).
uncomment `dtparam=spi=on` in `/boot/config.txt` ##### Setup RC522 RFID Reader
> `sudo sed -i "/dtparam=spi=on/s/^#//g" /boot/config.txt`
##### enable I2C for MPU9250 9-axis sensor - uncomment `dtparam=spi=on` in `/boot/config.txt` tp enable SPI for RC522
```
sudo sed -i "/dtparam=spi=on/s/^#//g" /boot/config.txt
```
install `i2c-tools` and `python3-smbus` - connect RC522
> `sudo apt install -y i2c-tools python3-smbus` - SDA to Pin 24 \[GPIO 8 / SPI0 CE0\]
- SCK to Pin 23 \[GPIO 11 / SPI0 SCLK / CLK\]
- MOSI to Pin 19 \[GPIO 10 / SPI0 MOSI\]
- MISO to Pin 21 \[GPIO 9 / SPI0 MISO\]
- GND to Pin 20 \[Ground\]
- RST to Pin 22 \[GPIO 25\]
- 3.3v to Pin 1 \[3v3 Power\]
uncomment `dtparam=i2c_arm=on` in `/boot/config.txt` ##### Setup Pimoroni Amp or DAC
> `sudo sed -i "/dtparam=i2c_arm=on/s/^#//g" /boot/config.txt`
- disable onboard audio comment out `dtparam=audio=on` in `/boot/config.txt`
```
sudo sed -i "/dtparam=audio=on/s/^/#/g" /boot/config.txt
```
- setup hifiberry-dac by adding following content to `/boot/config.txt`
> ```
> # setup hifiberry for Pimoroni Shim
> dtoverlay=hifiberry-dac
> gpio=25=op,dh
> ```
```
printf "\n# setup hifiberry for Pimoroni Shim\ndtoverlay=hifiberry-dac\ngpio=25=op,dh\n" | sudo tee -a /boot/config.txt
```
##### Setup Adafruit Speaker Bonnet for Raspberry Pi
- disable onboard audio comment out `dtparam=audio=on` in `/boot/config.txt`
```
sudo sed -i "/dtparam=audio=on/s/^/#/g" /boot/config.txt
```
- setup hifiberry-dac and i2s by adding following content to `/boot/config.txt`
> ```
> # setup hifiberry and i2s for Adafruit Speaker Bonnet
> dtoverlay=hifiberry-dac
> dtoverlay=i2s-mmap
> ```
```
printf "\n# setup hifiberry and i2s for Adafruit Speaker Bonnet\ndtoverlay=hifiberry-dac\ndtoverlay=i2s-mmap\n" | sudo tee -a /boot/config.txt
```
- copy [contrib/config/asound.conf](contrib/config/asound.conf) to `/etc/asound.conf`.
```
cat /home/pi/luniebox/contrib/config/asound.conf | sudo tee /etc/asound.conf
```
- copy [contrib/config/.asoundrc](contrib/config/.asoundrc) to `/home/pi/.asoundrc`.
```
cat /home/pi/luniebox/contrib/config/.asoundrc | tee /home/pi/.asoundrc
```
##### Setup Push Buttons
- connect first push button (volume down)
1. Pin 8 \[GPIO 14\]
2. Pin 6 \[Ground\]
- connect second push button (volume up)
1. Pin 7 \[GPIO 4\]
2. Pin 9 \[Ground\]
##### Setup MPU9250 9-axis sensor
- install `i2c-tools` and `python3-smbus`
```
sudo apt install -y i2c-tools python3-smbus
```
- uncomment `dtparam=i2c_arm=on` in `/boot/config.txt` to enable I2C for MPU9250
```
sudo sed -i "/dtparam=i2c_arm=on/s/^#//g" /boot/config.txt
```
- setup i2c bus 4 for MPU9250 by adding following content to `/boot/config.txt`
> ```
> # setup ic2 bus 4 for mpu
> dtoverlay=i2c-gpio,bus=4,i2c_gpio_delay_us=1,i2c_gpio_sda=23,i2c_gpio_scl=24
> ```
```
printf "\n# setup i2c bus 4 for mpu\ndtoverlay=i2c-gpio,bus=4,i2c_gpio_delay_us=1,i2c_gpio_sda=23,i2c_gpio_scl=24\n" | sudo tee -a /boot/config.txt
```
- connect MPU9250
- SDA to Pin 16 \[GPIO 23\]
- SCL to Pin 18 \[GPIO 24\]
- GND to Pin 14 \[Ground\]
- 3V3 to Pin 17 \[3v3 Power\]
add `dtoverlay=i2c-gpio,bus=4,i2c_gpio_delay_us=1,i2c_gpio_sda=23,i2c_gpio_scl=24` to `/boot/config.txt` #### Finish Setup
> `printf "dtoverlay=i2c-gpio,bus=4,i2c_gpio_delay_us=1,i2c_gpio_sda=23,i2c_gpio_scl=24" | sudo tee -a /boot/config.txt` - After setup, reboot system.
#### Setup Audio ```
sudo reboot
##### for Pimoroni Amp or DAC ```
disable onboard audio comment out `dtparam=audio=on` in `/boot/config.txt`
> `sudo sed -i "/dtparam=audio=on/s/^/#/g" /boot/config.txt`
setup hifiberry-dac by adding
```
dtoverlay=hifiberry-dac
gpio=25=op,dh
```
to `/boot/config.txt`
> `printf "dtoverlay=hifiberry-dac\ngpio=25=op,dh" | sudo tee -a /boot/config.txt`
##### for Adafruit Speaker Bonnet for Raspberry Pi
disable onboard audio comment out `dtparam=audio=on` in `/boot/config.txt`
> `sudo sed -i "/dtparam=audio=on/s/^/#/g" /boot/config.txt`
setup hifiberry-dac and i2s by adding
```
dtoverlay=hifiberry-dac
dtoverlay=i2s-mmap
```
to `/boot/config.txt`
> `printf "dtoverlay=hifiberry-dac\ndtoverlay=i2s-mmap" | sudo tee -a /boot/config.txt`
create `/etc/asound.conf` file with following content:
```
pcm.speakerbonnet {
type hw card 0
}
pcm.dmixer {
type dmix
ipc_key 1024
ipc_perm 0666
slave {
pcm "speakerbonnet"
period_time 0
period_size 1024
buffer_size 8192
rate 44100
channels 2
}
}
ctl.dmixer {
type hw card 0
}
pcm.softvol {
type softvol
slave.pcm "dmixer"
control.name "PCM"
control.card 0
}
ctl.softvol {
type hw card 0
}
pcm.!default {
type plug
slave.pcm "softvol"
}
```
After setup, reboot system.
> `sudo reboot`
## Planned features ## Planned features
- status LEDs - status LEDs
- indicators for UPS HAT - indicators for UPS HAT
- WiFi Hotspot (https://www.raspberryconnect.com/projects/65-raspberrypi-hotspot-accesspoints/158-raspberry-pi-auto-wifi-hotspot-switch-direct-connection) - WiFi Hotspot (https://www.raspberryconnect.com/projects/65-raspberrypi-hotspot-accesspoints/158-raspberry-pi-auto-wifi-hotspot-switch-direct-connection)

View File

@ -10,18 +10,6 @@ app = Flask(__name__)
app.register_blueprint(api, url_prefix='/api') app.register_blueprint(api, url_prefix='/api')
app.register_blueprint(pages) app.register_blueprint(pages)
loglevel = 'INFO'
if luniebox.get_setting('logging', 'level'):
loglevel = luniebox.get_setting('logging', 'level')
logger = logging.getLogger('luniebox')
logger.setLevel(logging._nameToLevel[loglevel])
logFormatter = logging.Formatter(
style='{', datefmt='%Y-%m-%d %H:%M:%S', fmt='{asctime} {levelname}: {message}')
logstdoutHandler = logging.StreamHandler(sys.stdout)
logstdoutHandler.setFormatter(logFormatter)
logger.addHandler(logstdoutHandler)
if __name__ != '__main__': if __name__ != '__main__':
gunicorn_logger = logging.getLogger('gunicorn.error') gunicorn_logger = logging.getLogger('gunicorn.error')
app.logger.handlers = gunicorn_logger.handlers app.logger.handlers = gunicorn_logger.handlers

View File

@ -3,6 +3,7 @@
import signal import signal
import time import time
import sys import sys
import subprocess
import logging import logging
from luniebox import luniebox from luniebox import luniebox
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
@ -26,23 +27,29 @@ class LunieboxDaemon(object):
self.tolerance = self.luniebox.get_setting( self.tolerance = self.luniebox.get_setting(
'rfid', 'pause_tolerance', 4) 'rfid', 'pause_tolerance', 4)
self.reads = 0 self.reads = 0
self.error_text = False
if self.luniebox.get_setting('hardware', 'mpu') == 'True': if self.luniebox.get_setting('hardware', 'mpu') == 'True':
self.mpu = MPU9250( try:
address_ak=AK8963_ADDRESS, self.mpu = MPU9250(
address_mpu_master=MPU9050_ADDRESS_68, address_ak=AK8963_ADDRESS,
address_mpu_slave=None, address_mpu_master=MPU9050_ADDRESS_68,
bus=4, address_mpu_slave=None,
gfs=GFS_1000, bus=4,
afs=AFS_8G, gfs=GFS_1000,
mfs=AK8963_BIT_16, afs=AFS_8G,
mode=AK8963_MODE_C100HZ) mfs=AK8963_BIT_16,
self.mpu.configure() mode=AK8963_MODE_C100HZ)
self.mpu.configure()
except Exception as exception:
logging.getLogger('luniebox').warning(
"Could not setup MPU9250: " + str(exception))
self.mpu = False
else: else:
self.mpu = False self.mpu = False
def run(self): def run(self):
logging.getLogger('luniebox').info("run luniebox") logging.getLogger('luniebox').info("run luniebox daemon")
while True: while True:
# mpu # mpu
@ -86,10 +93,14 @@ class LunieboxDaemon(object):
if self.reads >= self.tolerance: if self.reads >= self.tolerance:
self.reads = 0 self.reads = 0
self.luniebox.pause() self.luniebox.pause()
self.error_text = False
time.sleep(0.1) time.sleep(0.1)
else: else:
self.reads = 0 self.reads = 0
self.luniebox.play(text) if text != self.error_text and not self.luniebox.play(text):
if self.luniebox.get_setting('luniebox', 'error_sound') != 'False':
self.error_text = text
self.error_sound()
time.sleep(0.1) time.sleep(0.1)
def signal_handler(self, signal, frame): def signal_handler(self, signal, frame):
@ -98,6 +109,21 @@ class LunieboxDaemon(object):
luniebox.stop() luniebox.stop()
sys.exit(0) sys.exit(0)
def beep_sound(self):
self.play_sound("/home/pi/luniebox/audio/beep.wav",
str(self.luniebox.volume))
def error_sound(self):
self.play_sound("/home/pi/luniebox/audio/error.wav", "100")
def play_sound(self, path, volume):
volume_p = subprocess.Popen(
["amixer", "set", "PCM", volume + "%"], stdin=None,
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, close_fds=True, shell=False, group="pi", user="pi")
volume_p.wait()
subprocess.Popen(["aplay", path], stdin=None,
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, close_fds=True, shell=False, group="pi", user="pi")
if __name__ == "__main__": if __name__ == "__main__":
daemon = LunieboxDaemon(luniebox=luniebox) daemon = LunieboxDaemon(luniebox=luniebox)

View File

@ -168,9 +168,9 @@ class Luniebox(object):
self.zspotify_path, username, password, root) self.zspotify_path, username, password, root)
logging.getLogger('luniebox').info("spotifydl enabled!") logging.getLogger('luniebox').info("spotifydl enabled!")
return True return True
except Exception as ex: except Exception as exception:
logging.getLogger('luniebox').warning( logging.getLogger('luniebox').warning(
"error on setup spotifydl: " + str(ex)) "error on setup spotifydl: " + str(exception))
return False return False
def read_config(self): def read_config(self):
@ -315,14 +315,20 @@ class Luniebox(object):
else: else:
logging.getLogger('luniebox').warn( logging.getLogger('luniebox').warn(
"cannot resume spotify: " + self.current) "cannot resume spotify: " + self.current)
return True
elif self.service == PlayerService.MPD: elif self.service == PlayerService.MPD:
if text != self.current: if text != self.current:
if self.mpd_connect(): if self.mpd_connect():
self.mpd.setvol(self.volume) try:
self.mpd.clear() self.mpd.setvol(self.volume)
text = text.replace('mpd:', '') self.mpd.clear()
self.mpd.add(text) text = text.replace('mpd:', '')
self.mpd.play() self.mpd.add(text)
self.mpd.play()
except Exception as exception:
logging.getLogger('luniebox').warning(
"cannot not play mpd '" + text + "': " + str(exception))
return False
self.current = text self.current = text
self.set_setting('luniebox', 'current', self.current) self.set_setting('luniebox', 'current', self.current)
self.resume = False self.resume = False
@ -334,8 +340,13 @@ class Luniebox(object):
"play mpd: " + self.current) "play mpd: " + self.current)
elif self.resume and text == self.current: elif self.resume and text == self.current:
if self.mpd_connect(): if self.mpd_connect():
self.mpd.setvol(self.volume) try:
self.mpd.play() self.mpd.setvol(self.volume)
self.mpd.play()
except Exception as exception:
logging.getLogger('luniebox').warning(
"cannot not resume mpd '" + self.current + "': " + str(exception))
return False
self.resume = False self.resume = False
if text.startswith('spotify:'): if text.startswith('spotify:'):
logging.getLogger('luniebox').debug( logging.getLogger('luniebox').debug(
@ -343,10 +354,12 @@ class Luniebox(object):
else: else:
logging.getLogger('luniebox').debug( logging.getLogger('luniebox').debug(
"resume mpd: " + self.current) "resume mpd: " + self.current)
return True
elif text != None: elif text != None:
logging.getLogger('luniebox').info( logging.getLogger('luniebox').info(
"invalid value(?): " + str(text)) "invalid value(?): " + str(text))
return False
return False
def stop(self): def stop(self):
self.pause() self.pause()

View File

@ -6,4 +6,5 @@ mpu9250-jmdev==1.0.12
python-dateutil==2.8.2 python-dateutil==2.8.2
python-mpd2==3.0.4 python-mpd2==3.0.4
requests==2.26.0 requests==2.26.0
smbus==1.1.post2
smbus2==0.4.1 smbus2==0.4.1

BIN
audio/beep.wav Normal file

Binary file not shown.

BIN
audio/error.wav Normal file

Binary file not shown.

37
contrib/config/.asoundrc Normal file
View File

@ -0,0 +1,37 @@
pcm.speakerbonnet {
type hw card 0
}
pcm.dmixer {
type dmix
ipc_key 1024
ipc_perm 0666
slave {
pcm "speakerbonnet"
period_time 0
period_size 1024
buffer_size 8192
rate 44100
channels 2
}
}
ctl.dmixer {
type hw card 0
}
pcm.softvol {
type softvol
slave.pcm "dmixer"
control.name "PCM"
control.card 0
}
ctl.softvol {
type hw card 0
}
pcm.!default {
type plug
slave.pcm "softvol"
}

View File

@ -1,38 +1,26 @@
pcm.speakerbonnet { pcm.speakerbonnet {
type hw card 0 type hw card 0
} }
pcm.dmixer { pcm.!default {
type dmix type plug
ipc_key 1024 slave.pcm "dmixer"
ipc_perm 0666
slave {
pcm "speakerbonnet"
period_time 0
period_size 1024
buffer_size 8192
rate 44100
channels 2
}
} }
ctl.dmixer { pcm.dmixer {
type hw card 0 type dmix
ipc_key 1024
ipc_perm 0666
slave {
pcm "speakerbonnet"
period_time 0
period_size 1024
buffer_size 8192
rate 44100
channels 2
}
} }
pcm.softvol { ctl.dmixer {
type softvol type hw card 0
slave.pcm "dmixer" }
control.name "PCM"
control.card 0
}
ctl.softvol {
type hw card 0
}
pcm.!default {
type plug
slave.pcm "softvol"
}

View File

@ -13,5 +13,5 @@ level = DEBUG
library_path = /home/pi/luniebox/library library_path = /home/pi/luniebox/library
[spotify] [spotify]
auto_download = False auto_download = True
zspotify_path = zspotify_path =

View File

@ -2,11 +2,10 @@
Description=Luniebox Application Description=Luniebox Application
[Service] [Service]
Type=simple
WorkingDirectory=/home/pi/luniebox/application WorkingDirectory=/home/pi/luniebox/application
ExecStart=/home/pi/luniebox/application/venv/bin/gunicorn -b 0.0.0.0:80 app:app ExecStart=/home/pi/luniebox/application/venv/bin/gunicorn -b 0.0.0.0:80 app:app
Restart=always Restart=on-failure
RestartSec=12 RestartSec=15
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target

View File

@ -2,11 +2,13 @@
Description=Luniebox Daemon Description=Luniebox Daemon
[Service] [Service]
Type=simple User=pi
Group=pi
WorkingDirectory=/home/pi/luniebox/application WorkingDirectory=/home/pi/luniebox/application
ExecStart=/home/pi/luniebox/application/venv/bin/python daemon.py ExecStart=/home/pi/luniebox/application/venv/bin/python daemon.py
Restart=always Restart=on-failure
RestartSec=12 RestartSec=15
#FailureAction=/home/pi/luniebox/application/venv/bin/python daemon-failure.py
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target

View File

@ -9,8 +9,8 @@ After=network-online.target
User=pi User=pi
Group=pi Group=pi
ExecStart=/home/pi/luniebox/bin/spotifyd --config-path /home/pi/luniebox/config/spotifyd.cfg --no-daemon ExecStart=/home/pi/luniebox/bin/spotifyd --config-path /home/pi/luniebox/config/spotifyd.cfg --no-daemon
Restart=always Restart=on-failure
RestartSec=12 RestartSec=15
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target

View File

@ -1,19 +1,22 @@
#!/usr/bin/env bash #!/usr/bin/env bash
INSTALL_CONFIG_FILE="/home/pi/luniebox/config/install.cfg"
REPO_URL="https://git.bstly.de/Lurkars/luniebox.git"
AUTOMATIC=false AUTOMATIC=false
NO_UPDATES=false NO_UPDATES=false
NO_SPOTIFYDL=false NO_SPOTIFYDL=false
NO_SPOTIFY=false NO_SPOTIFY=false
NO_MPD=false NO_MPD=false
NO_AUDIO=false NO_AUDIO=false
MPU=false NO_MPU=true
AUDIO_DEVICE=false AUDIO_DEVICE=false
AUDIO_DEVICES=("pimoroni" "adafruit") AUDIO_DEVICES=("pimoroni" "adafruit")
DEVICE_NAME=false DEVICE_NAME=false
REBOOT=false
help() { help() {
local IFS="|" local IFS="|"
printf "Usage: ${SCRIPTNAME} [-h] [-a] [--audio-device AUDIO_DEVICE] [--device-name DEVICE_NAME] [--no-updates] [--no-spotifydl] [--no-spotify] [--no-mpd] [--mpu] \n" printf "Usage: ${SCRIPTNAME} [-h] [-a] [--audio-device AUDIO_DEVICE] [--device-name DEVICE_NAME] [--no-updates] [--no-spotifydl] [--no-spotify] [--no-mpd] [--mpu] [--repo-url] \n"
printf " -h\t\t\t\tshow this help\n" printf " -h\t\t\t\tshow this help\n"
printf " -a\t\t\t\tautomatic/non-interactive mode (without --audio-device, no Audio device is set up)\n" printf " -a\t\t\t\tautomatic/non-interactive mode (without --audio-device, no Audio device is set up)\n"
printf " --audio-device [AUDIO_DEVICE]\tset Audio device (one of [${AUDIO_DEVICES[*]}])\n" printf " --audio-device [AUDIO_DEVICE]\tset Audio device (one of [${AUDIO_DEVICES[*]}])\n"
@ -23,6 +26,7 @@ help() {
printf " --no-mpd\t\t\tskip installation of Music Player Daemon (no offline support, also skips zspotify)\n" printf " --no-mpd\t\t\tskip installation of Music Player Daemon (no offline support, also skips zspotify)\n"
printf " --no-spotify\t\t\tskip installation of spotifyd (no Spotify support, also skips zspotify)\n" printf " --no-spotify\t\t\tskip installation of spotifyd (no Spotify support, also skips zspotify)\n"
printf " --mpu\t\t\t\tsetup MPU9250 on I2C port (default is no MPU9250)\n" printf " --mpu\t\t\t\tsetup MPU9250 on I2C port (default is no MPU9250)\n"
printf " --repo-url [REPOSITORY_URL]\tset luniebox repository url (default https://git.bstly.de/Lurkars/luniebox.git)\n"
exit 0 exit 0
} }
@ -55,7 +59,11 @@ while [ -n "$1" ]; do
shift shift
;; ;;
"--mpu") "--mpu")
MPU=true NO_MPU=false
;;
"--repo-url")
REPO_URL="$2"
shift
;; ;;
*) *)
printf "\nargument '$1' not supported!\n\n" printf "\nargument '$1' not supported!\n\n"
@ -106,248 +114,354 @@ Run the script with -h to get more information about possible options to be appl
setup() { setup() {
cd /home/pi cd /home/pi
if [[ ${NO_UPDATES} == "false" ]]; then initialize
update update
fi setup_device_name
setup_software setup_software
set_device_name
setup_rfid setup_rfid
setup_audio
setup_mpu setup_mpu
if [[ ${NO_AUDIO} == "false" ]]; then
setup_audio
fi
finalize finalize
} }
update() { initialize() {
if [[ ${AUTOMATIC} == "false" ]]; then echo "Initizalize setup..."
read -rp "Do you want to update system now? [Y/n] (recommended) " response
echo "" echo "Check for previous setup..."
case "$response" in if [[ -f "${INSTALL_CONFIG_FILE}" ]]; then
[nN][oO] | [nN]) eval $(sed -r "/[^=]+=[^=]+/!d;s/\s+=\s/=/g" "${INSTALL_CONFIG_FILE}")
return 0
;;
esac
fi fi
echo "Update system" if [[ -z ${SETUP_INITIALIZE+x} ]]; then
sudo apt update echo "Install required packages..."
sudo apt upgrade -y sudo apt install -y git
echo "Fetch luniebox repository..."
git clone "${REPO_URL}" /home/pi/luniebox
echo "Create config directory"
mkdir -p /home/pi/luniebox/config
echo "SETUP_INITIALIZE=true" | tee -a "${INSTALL_CONFIG_FILE}" 1>/dev/null
else
echo "luniebox is already setup!"
fi
if [[ -z ${SETUP_REPO_URL+x} ]]; then
echo "SETUP_REPO_URL=${REPO_URL}" | tee -a "${INSTALL_CONFIG_FILE}" 1>/dev/null
else
REPO_URL="${SETUP_REPO_URL}"
fi
}
update() {
if [[ -z ${SETUP_UPDATES+x} ]]; then
if [[ ${NO_UPDATES} == "false" ]]; then
if [[ ${AUTOMATIC} == "false" ]]; then
read -rp "Do you want to update system now? [Y/n] (recommended) " response
echo ""
case "$response" in
[nN][oO] | [nN])
NO_UPDATES=true
return 0
;;
esac
fi
echo "Update system"
sudo apt update
sudo apt upgrade -y
echo "SETUP_UPDATES=true" | tee -a "${INSTALL_CONFIG_FILE}" 1>/dev/null
fi
fi
if [[ -z ${SETUP_NO_UPDATES+x} ]]; then
echo "SETUP_NO_UPDATES=${NO_UPDATES}" | tee -a "${INSTALL_CONFIG_FILE}" 1>/dev/null
else
NO_UPDATES="${SETUP_NO_UPDATES}"
fi
} }
setup_software() { setup_software() {
setup_app setup_app
if [[ ${NO_SPOTIFY} == "false" ]]; then setup_spotify
setup_spotify setup_mpd
fi setup_zspotify
if [[ ${NO_MPD} == "false" ]]; then
setup_mpd
fi
if [[ ${NO_SPOTIFYDL} == "false" ]]; then
setup_zspotify
fi
} }
setup_app() { setup_app() {
echo "Install required packages..." if [[ -z ${SETUP_APP+x} ]]; then
sudo apt install -y git python3-venv python3-pip echo "Install required packages..."
echo "Fetch luniebox repository..." sudo apt install -y python3-venv python3-pip
git clone https://git.bstly.de/Lurkars/luniebox.git /home/pi/luniebox cd /home/pi/luniebox/application
cd /home/pi/luniebox/application echo "Setup python venv..."
echo "Setup python venv..." python -m venv venv
python -m venv venv source venv/bin/activate
source venv/bin/activate export CFLAGS=-fcommon
export CFLAGS=-fcommon echo "Install pip requirements..."
echo "Install pip requirements..." pip install -r requirements.txt
pip install -r requirements.txt deactivate
deactivate echo "Setup config and systemd services..."
mkdir /home/pi/luniebox/config cp /home/pi/luniebox/contrib/config/luniebox.cfg /home/pi/luniebox/config/luniebox.cfg
echo "Setup config and systemd services..." sudo cp /home/pi/luniebox/contrib/service/luniebox-app.service /etc/systemd/system/
cp /home/pi/luniebox/contrib/config/luniebox.cfg /home/pi/luniebox/config/luniebox.cfg sudo cp /home/pi/luniebox/contrib/service/luniebox-daemon.service /etc/systemd/system/
sudo cp /home/pi/luniebox/contrib/service/luniebox-app.service /etc/systemd/system/ sudo systemctl daemon-reload
sudo cp /home/pi/luniebox/contrib/service/luniebox-daemon.service /etc/systemd/system/ sudo systemctl enable luniebox-app luniebox-daemon
sudo systemctl daemon-reload REBOOT=true
sudo systemctl enable luniebox-app luniebox-daemon echo "SETUP_APP=true" | tee -a "${INSTALL_CONFIG_FILE}" 1>/dev/null
fi
} }
setup_spotify() { setup_spotify() {
if [[ ${AUTOMATIC} == "false" ]]; then if [[ -z ${SETUP_SPOTIFY+x} ]]; then
read -rp "Do you want to install spotifyd for Spofity support? [Y/n] " response if [[ ${NO_SPOTIFY} == "false" ]]; then
echo "" if [[ ${AUTOMATIC} == "false" ]]; then
case "$response" in read -rp "Do you want to install spotifyd for Spofity support? [Y/n] " response
[nN][oO] | [nN]) echo ""
NO_SPOTIFY=true case "$response" in
return 0 [nN][oO] | [nN])
;; NO_SPOTIFY=true
esac return 0
;;
esac
fi
mkdir -p /home/pi/luniebox/bin
echo "Download spotifyd..."
wget -c https://github.com/Spotifyd/spotifyd/releases/download/v0.3.3/spotifyd-linux-armv6-slim.tar.gz -O - | tar -xz -C /home/pi/luniebox/bin
echo "Setup config and systemd services..."
if [[ ${NO_AUDIO} == "true" ]]; then
echo "WARNING: you may need to adjust the file '/home/pi/luniebox/config/spotifyd.cfg' manually to setup your audio device!"
echo ""
fi
cp /home/pi/luniebox/contrib/config/spotifyd.cfg /home/pi/luniebox/config/spotifyd.cfg
sed -i "s/^device_name.*=.*$/device_name = \"${DEVICE_NAME}\"/" /home/pi/luniebox/config/spotifyd.cfg
sudo cp /home/pi/luniebox/contrib/service/spotifyd.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable spotifyd
REBOOT=true
echo "SETUP_SPOTIFY=true" | tee -a "${INSTALL_CONFIG_FILE}" 1>/dev/null
fi
fi fi
mkdir /home/pi/luniebox/bin if [[ -z ${SETUP_NO_SPOTIFY+x} ]]; then
echo "Download spotifyd..." echo "SETUP_NO_SPOTIFY=${NO_SPOTIFY}" | tee -a "${INSTALL_CONFIG_FILE}" 1>/dev/null
wget -c https://github.com/Spotifyd/spotifyd/releases/download/v0.3.3/spotifyd-linux-armv6-slim.tar.gz -O - | tar -xz -C /home/pi/luniebox/bin else
echo "Setup config and systemd services..." NO_SPOTIFY="${SETUP_NO_SPOTIFY}"
if [[ ${NO_AUDIO} == "true" ]]; then
echo "WARNING: you may need to adjust the file '/home/pi/luniebox/config/spotifyd.cfg' manually to setup your audio device!"
echo ""
fi fi
cp /home/pi/luniebox/contrib/config/spotifyd.cfg /home/pi/luniebox/config/spotifyd.cfg
sudo cp /home/pi/luniebox/contrib/service/spotifyd.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable spotifyd
} }
setup_mpd() { setup_mpd() {
if [[ ${AUTOMATIC} == "false" ]]; then if [[ -z ${SETUP_MPD+x} ]]; then
read -rp "Do you want to install mpd (Music Player Daemon) for offline file support? [Y/n] " response if [[ ${NO_MPD} == "false" ]]; then
echo "" if [[ ${AUTOMATIC} == "false" ]]; then
case "$response" in read -rp "Do you want to install mpd (Music Player Daemon) for offline file support? [Y/n] " response
[nN][oO] | [nN]) echo ""
NO_MPD=true case "$response" in
return 0 [nN][oO] | [nN])
;; NO_MPD=true
esac return 0
;;
esac
fi
mkdir -p /home/pi/luniebox/library
echo "Install required packages..."
sudo apt install -y mpd
echo "Setup config..."
if [[ ${NO_AUDIO} == "true" ]]; then
echo "WARNING: you may need to adjust the file '/etc/mpd.conf' manually to setup your audio device!"
echo ""
fi
sudo cp /home/pi/luniebox/contrib/config/mpd.conf /etc/mpd.conf
REBOOT=true
echo "SETUP_MPD=true" | tee -a "${INSTALL_CONFIG_FILE}" 1>/dev/null
fi
fi fi
mkdir /home/pi/luniebox/library
echo "Install required packages..." if [[ -z ${SETUP_NO_MPD+x} ]]; then
sudo apt install -y mpd echo "SETUP_NO_MPD=${NO_MPD}" | tee -a "${INSTALL_CONFIG_FILE}" 1>/dev/null
echo "Setup config..." else
if [[ ${NO_AUDIO} == "true" ]]; then NO_MPD="${SETUP_NO_MPD}"
echo "WARNING: you may need to adjust the file '/etc/mpd.conf' manually to setup your audio device!"
echo ""
fi fi
sudo cp /home/pi/luniebox/contrib/config/mpd.conf /etc/mpd.conf
} }
setup_zspotify() { setup_zspotify() {
if [[ ${NO_SPOTIFY} == "false" ]] && [[ ${NO_MPD} == "false" ]]; then if [[ -z ${SETUP_SPOTIFYDL+x} ]]; then
if [[ ${AUTOMATIC} == "false" ]]; then if [[ ${NO_SPOTIFYDL} == "false" ]] && [[ ${NO_SPOTIFY} == "false" ]] && [[ ${NO_MPD} == "false" ]]; then
read -rp "Do you want to install zspotify for downloading Spotify tracks for offline support? [Y/n] " response if [[ ${AUTOMATIC} == "false" ]]; then
read -rp "Do you want to install zspotify for downloading Spotify tracks for offline support? [Y/n] " response
echo ""
case "$response" in
[nN][oO] | [nN])
NO_MPD=true
return 0
;;
esac
fi
echo "Fetch clspotify repository..."
git clone https://github.com/agent255/clspotify.git /home/pi/clspotify
cd /home/pi/clspotify
echo "Setup python venv..."
python -m venv venv
source venv/bin/activate
echo "Install pip requirements..."
pip install -r requirements.txt
deactivate
echo "Setup config..."
sed -i "s/^zspotify_path =.*$/zspotify_path = \/home\/pi\/clspotify\//" /home/pi/luniebox/config/luniebox.cfg
REBOOT=true
echo "SETUP_SPOTIFYDL=true" | tee -a "${INSTALL_CONFIG_FILE}" 1>/dev/null
fi
fi
if [[ -z ${SETUP_NO_SPOTIFY+x} ]]; then
echo "SETUP_NO_SPOTIFYDL=${NO_SPOTIFYDL}" | tee -a "${INSTALL_CONFIG_FILE}" 1>/dev/null
else
NO_SPOTIFYDL="${SETUP_NO_SPOTIFYDL}"
fi
}
setup_device_name() {
if [[ -z ${SETUP_DEVICE_NAME+x} ]]; then
if [[ ${AUTOMATIC} == "false" ]] && [[ ${DEVICE_NAME} == "false" ]]; then
read -rp "Name for your Luniebox devices [default: luniebox]: " DEVICE_NAME
fi
if ! [[ $DEVICE_NAME ]] || [[ ${DEVICE_NAME} == "false" ]]; then
DEVICE_NAME="luniebox"
fi
echo "Set device name to '${DEVICE_NAME}'"
printf "${DEVICE_NAME}" | sudo tee /etc/hostname 1>/dev/null
sudo sed -i "/^127.0.0.1/s/$/ ${DEVICE_NAME}/g" /etc/hosts
REBOOT=true
echo "SETUP_DEVICE_NAME=${DEVICE_NAME}" | tee -a "${INSTALL_CONFIG_FILE}" 1>/dev/null
else
DEVICE_NAME="${SETUP_DEVICE_NAME}"
fi
}
setup_rfid() {
if [[ -z ${SETUP_RFID+x} ]]; then
echo "Enable SPI for RFID reader"
sudo sed -i "/dtparam=spi=on/s/^#//g" /boot/config.txt
REBOOT=true
echo "SETUP_RFID=true" | tee -a "${INSTALL_CONFIG_FILE}" 1>/dev/null
fi
}
setup_mpu() {
if [[ -z ${SETUP_MPU+x} ]]; then
if [[ ${AUTOMATIC} == "false" ]] && [[ ${NO_MPU} == "true" ]]; then
read -rp "Do you want to setup I2C for MPU9250 9-axis sensor? [y/N] " response
echo "" echo ""
case "$response" in case "$response" in
[nN][oO] | [nN]) [yY][eE][sS] | [yY])
NO_MPD=true NO_MPU=false
;;
*)
return 0 return 0
;; ;;
esac esac
fi fi
echo "Fetch clspotify repository..." if [[ ${NO_MPU} == "false" ]]; then
git clone https://github.com/agent255/clspotify.git /home/pi/clspotify echo "Install I2C software, enable i2c bus 4, enable mpu"
cd /home/pi/clspotify sudo apt install -y i2c-tools python3-smbus
echo "Setup python venv..." sudo sed -i "/dtparam=i2c_arm=on/s/^#//g" /boot/config.txt
python -m venv venv printf "\n# setup i2c bus 4 for mpu\ndtoverlay=i2c-gpio,bus=4,i2c_gpio_delay_us=1,i2c_gpio_sda=23,i2c_gpio_scl=24\n" | sudo tee -a /boot/config.txt 1>/dev/null
source venv/bin/activate sed -i "s/^mpu =.*$/mpu = True/" /home/pi/luniebox/config/luniebox.cfg
echo "Install pip requirements..." REBOOT=true
pip install -r requirements.txt echo "SETUP_MPU=true" | tee -a "${INSTALL_CONFIG_FILE}" 1>/dev/null
deactivate fi
echo "Setup config..."
sed -i "s/^zspotify_path =.*$/zspotify_path = \/home\/pi\/clspotify\//" /home/pi/luniebox/config/luniebox.cfg
fi
}
set_device_name() {
if [[ ${AUTOMATIC} == "false" ]] && [[ ${DEVICE_NAME} == "false" ]]; then
read -rp "Name for your Luniebox devices [default: luniebox]: " DEVICE_NAME
fi fi
if ! [[ $DEVICE_NAME ]] || [[ ${DEVICE_NAME} == "false" ]]; then if [[ -z ${SETUP_NO_MPU+x} ]]; then
DEVICE_NAME="luniebox" echo "SETUP_NO_MPU=${NO_MPU}" | tee -a "${INSTALL_CONFIG_FILE}" 1>/dev/null
fi else
NO_MPU="${SETUP_NO_MPU}"
echo "Set device name to '${DEVICE_NAME}'"
printf "${DEVICE_NAME}" | sudo tee /etc/hostname 1>/dev/null
sudo sed -i "/^127.0.0.1/s/$/ ${DEVICE_NAME}/g" /etc/hosts
if [[ NO_SPOTIFY == "false" ]]; then
sed -i "s/^device_name.*=.*$/device_name = \"${DEVICE_NAME}\"/" /home/pi/luniebox/config/spotifyd.cfg
fi
}
setup_rfid() {
echo "Enable SPI for RFID reader"
sudo sed -i "/dtparam=spi=on/s/^#//g" /boot/config.txt
}
setup_mpu() {
if [[ ${AUTOMATIC} == "false" ]] && [[ ${MPU} == "false" ]]; then
read -rp "Do you want to setup I2C for MPU9250 9-axis sensor? [y/N] " response
echo ""
case "$response" in
[yY][eE][sS] | [yY])
MPU=true
break
;;
*)
return 0
;;
esac
fi
if [[ ${MPU} == "true" ]]; then
echo "Install I2C software, enable i2c bus 4, enable mpu"
sudo apt install -y i2c-tools python3-smbus
sudo sed -i "/dtparam=i2c_arm=on/s/^#//g" /boot/config.txt
printf "dtoverlay=i2c-gpio,bus=4,i2c_gpio_delay_us=1,i2c_gpio_sda=23,i2c_gpio_scl=24" | sudo tee -a /boot/config.txt 1>/dev/null
sed -i "s/^mpu =.*$/mpu = True/" /home/pi/luniebox/config/luniebox.cfg
fi fi
} }
setup_audio() { setup_audio() {
if [[ ${AUTOMATIC} == "false" ]]; then if [[ -z ${SETUP_AUDIO+x} ]]; then
echo "Choose your audio hardware" if [[ ${NO_AUDIO} == "false" ]]; then
select response in "${AUDIO_DEVICES[@]}"; do if [[ ${AUTOMATIC} == "false" ]]; then
echo "" echo "Choose your audio hardware"
AUDIO_DEVICE="$response" select response in "${AUDIO_DEVICES[@]}"; do
break echo ""
done AUDIO_DEVICE="$response"
break
done
fi
case "$AUDIO_DEVICE" in
"pimoroni")
setup_pimoroni
;;
"adafruit")
setup_adafruit
;;
*)
NO_AUDIO=true
echo "WARNING: please setup your audio device manually!"
;;
esac
fi
fi fi
case "$AUDIO_DEVICE" in if [[ -z ${SETUP_AUDIO_DEVICE+x} ]]; then
"pimoroni") echo "SETUP_AUDIO_DEVICE=${AUDIO_DEVICE}" | tee -a "${INSTALL_CONFIG_FILE}" 1>/dev/null
setup_pimoroni else
;; AUDIO_DEVICE="${SETUP_AUDIO_DEVICE}"
"adafruit") fi
setup_adafruit
;; if [[ -z ${SETUP_NO_AUDIO+x} ]]; then
*) echo "SETUP_NO_AUDIO=${NO_AUDIO}" | tee -a "${INSTALL_CONFIG_FILE}" 1>/dev/null
NO_AUDIO=true else
echo "WARNING: please setup your audio device manually!" NO_AUDIO="${SETUP_NO_AUDIO}"
;; fi
esac
} }
setup_pimoroni() { setup_pimoroni() {
echo "Setup Pimoroni Shim" echo "Setup Pimoroni Shim"
sudo sed -i "/dtparam=audio=on/s/^/#/g" /boot/config.txt sudo sed -i "/dtparam=audio=on/s/^/#/g" /boot/config.txt
printf "dtoverlay=hifiberry-dac\ngpio=25=op,dh" | sudo tee -a /boot/config.txt 1>/dev/null printf "\n# setup hifiberry for Pimoroni Shim\ndtoverlay=hifiberry-dac\ngpio=25=op,dh\n" | sudo tee -a /boot/config.txt 1>/dev/null
REBOOT=true
echo "SETUP_AUDIO=true" | tee -a "${INSTALL_CONFIG_FILE}" 1>/dev/null
} }
setup_adafruit() { setup_adafruit() {
echo "Setup Adafruit Speaker Bonnet" echo "Setup Adafruit Speaker Bonnet"
sudo sed -i "/dtparam=audio=on/s/^/#/g" /boot/config.txt sudo sed -i "/dtparam=audio=on/s/^/#/g" /boot/config.txt
printf "dtoverlay=hifiberry-dac\ndtoverlay=i2s-mmap" | sudo tee -a /boot/config.txt 1>/dev/null printf "\n# setup hifiberry and i2s for Adafruit Speaker Bonnet\ndtoverlay=hifiberry-dac\ndtoverlay=i2s-mmap\n" | sudo tee -a /boot/config.txt 1>/dev/null
cat /home/pi/luniebox/contrib/config/asound.conf | sudo tee /etc/asound.conf 1>/dev/null cat /home/pi/luniebox/contrib/config/asound.conf | sudo tee /etc/asound.conf 1>/dev/null
cat /home/pi/luniebox/contrib/config/.asoundrc | tee /home/pi/.asoundrc 1>/dev/null
REBOOT=true
echo "SETUP_AUDIO=true" | tee -a "${INSTALL_CONFIG_FILE}" 1>/dev/null
} }
finalize() { finalize() {
if [[ ${AUTOMATIC} == "false" ]]; then if [[ ${REBOOT} == "true" ]]; then
read -rp "Setup finished and reboot required. Do you want to automatically reboot your luniebox now? [Y/n] " response if [[ ${AUTOMATIC} == "false" ]]; then
echo "" read -rp "Setup finished and reboot required. Do you want to automatically reboot your luniebox now? [Y/n] " response
case "$response" in echo ""
[nN][oO] | [nN]) case "$response" in
echo "Please reboot manually for luniebox working properly!" [nN][oO] | [nN])
echo "Bye...." echo "Please reboot manually for luniebox working properly!"
exit 0 echo "Bye...."
;; exit 0
esac ;;
fi esac
fi
echo "Rebooting now...." echo "Rebooting now...."
sleep 2 sleep 1
sudo reboot sudo reboot
exit exit 0
else
echo "Bye...."
exit 0
fi
} }
start start