Browse Source

Welcome Home!

Brendan Abolivier 5 years ago
parent
commit
4920207a46
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
6 changed files with 307 additions and 0 deletions
  1. 69
    0
      README.md
  2. 114
    0
      mopidy/mopidy-pine64.conf
  3. 10
    0
      mopidy/mopidy.service
  4. 95
    0
      welcomehome
  5. 10
    0
      welcomehome.example.conf
  6. 9
    0
      welcomehome.service

+ 69
- 0
README.md View File

@@ -0,0 +1,69 @@
1
+# Welcome Home
2
+
3
+## Introduction
4
+
5
+When I got myself a [Pine64](https://www.pine64.com/), I wondered for a bit about what to do with it. [@RaitoBezarius](https://github.com/RaitoBezarius) suggested that I set up a music server on it, which got my attention as I don't have a lot of hardware to buy for that (none, actually, except if you want to plug in a cheap sound system).
6
+
7
+I set up a [Mopidy](https://www.mopidy.com/) server on the card, installed with Pip (since the Pine64 runs an ARM64 architecture and Mopidy's Debian/Ubuntu packages don't support it).
8
+
9
+When I got that done, I wanted to enhance it. Since I didn't want just some central sound system I could control from my phone (really, my apartment is not that big, I don't need that), I thought of some system which would detect when I get home after work and automatically start my music.
10
+
11
+## Welcome Home
12
+
13
+Welcome Home is a bash script which detects when a selected device is connected to your local network. If it detects the device, it will communicate with Mopidy and start playing a selected stream (set in the configuration file) on the Pine64's jack output.
14
+
15
+In my example, when I get home and my phone connects to my apartment's wifi, it gets a static IP address (as I configured it in my DHCP server). Welcome Home detects it, which means that I'm home or almost there, and start some nice Trance music for me.
16
+
17
+When Welcome Home will stop detecting the device, it will automatically tell Mopidy to stop playing the stream. Which means that if I want to stop the music and am too lazy to turn off my sound system, I can also stop the wifi on my phone.
18
+
19
+Detection of both connection and disconnection takes up to 6s maximum with the default settings.
20
+
21
+## Files
22
+
23
+There are three files in this repository (except for those in the `mopidy` directory which are Pine64-related):
24
+
25
+* `welcomehome` contains the bash script and is to move to `/usr/local/bin`.
26
+* `welcomehome.example.conf` is a configuration example, and is to edit as described below and to move as `/etc/welcomehome/welcomehome.conf` (you'll need to create the `/etc/welcomehome` directory).
27
+* `welcomehome.service` is the systemd service for Welcome Home, and is to move to `/etc/systemd/system`
28
+
29
+## Configuration
30
+
31
+Right now, there's only three configurable values:
32
+
33
+* `device_address` is the static IP address bound to the device you wish to detect.
34
+* `device_threshold` is the detection threshold. It defaults to 3. A greater threshold will mean a connection or a disconnection will take more time to detect, but you'll be able to detect them with more accuracy if your device's wifi is working weakly.
35
+* `stream` is the URL of the audio stream you want Welcome Home to play when you get home. For now, it has to be an HTTP(S) stream.
36
+
37
+For more info on the configuration syntax, have a look at the `welcomehome.example.conf` file.
38
+
39
+## Starting Welcome Home
40
+
41
+Once you moved all the needed files, and filled your configuration file, you need to start Welcome Home as a daemon. I use [systemd](https://freedesktop.org/wiki/Software/systemd/) for that (with the `welcomehome.service` file), but you're free to use whatever you want (and even to send my your configuration file in a [pull request](https://github.com/babolivier/welcome-home/pulls) so I can add it to this repi :wink:).
42
+
43
+To start Welcome Home, and launch it at the machine's startup, with systemd, just run:
44
+
45
+```shell
46
+systemctl enable welcomehome
47
+systemctl start welcomehome
48
+```
49
+
50
+## Bonus: Running Mopidy on Pine64
51
+
52
+I had some troubles setting up Mopidy on my Pine64, so here's how I did it.
53
+
54
+### No sound playing
55
+
56
+I had some troubles setting up the Pine's soundcard, but finally managed it by installing the latest Ubuntu image, and following [these instructions](http://forum.pine64.org/showthread.php?tid=1832&pid=16434#pid16434).
57
+
58
+### Binding with alsa
59
+
60
+After following the above instructions, I also installed the `gstreamer1.0-alsa` package, which got me some nice ALSA plugins for Gstreamer. I edited the `/usr/share/alsa/alsa.conf` file and set both the `defaults.ctl.card` and the `defaults.pcm.card` properties from 0 to 1, so ALSA doesn't have to wonder too much about what soundcard to use (0 is the HDMI output and 1 is the jack output). I then set Mopidy's configuration file to what you can find in `mopidy/mopidy-pine64.conf`. It's basically the default configuration file, but with the right bindings.
61
+
62
+### Launch Mopidy at startup
63
+
64
+Since you installed Mopidy from sources, there's no provided way to automatically launch Mopidy when your Pine64 starts. I did it with the systemd service file you can find in `mopidy/mopidy.service`, which I moved to `/etc/systemd/system`. Then, the usual (as root):
65
+
66
+```shell
67
+systemctl enable mopidy
68
+systemctl start mopidy
69
+```

+ 114
- 0
mopidy/mopidy-pine64.conf View File

@@ -0,0 +1,114 @@
1
+# For further information about options in this file see:
2
+#   http://docs.mopidy.com/
3
+#
4
+# The initial commented out values reflect the defaults as of:
5
+#   Mopidy 2.0.0
6
+#   Mopidy-File 2.0.0
7
+#   Mopidy-HTTP 2.0.0
8
+#   Mopidy-Local 2.0.0
9
+#   Mopidy-M3U 2.0.0
10
+#   Mopidy-MPD 2.0.0
11
+#   Mopidy-SoftwareMixer 2.0.0
12
+#   Mopidy-Stream 2.0.0
13
+#
14
+# Available options and defaults might have changed since then,
15
+# run `mopidy config` to see the current effective config and
16
+# `mopidy --version` to check the current version.
17
+
18
+[core]
19
+#cache_dir = $XDG_CACHE_DIR/mopidy
20
+#config_dir = $XDG_CONFIG_DIR/mopidy
21
+#data_dir = $XDG_DATA_DIR/mopidy
22
+#max_tracklist_length = 10000
23
+
24
+[logging]
25
+#color = true
26
+#console_format = %(levelname)-8s %(message)s
27
+#debug_format = %(levelname)-8s %(asctime)s [%(process)d:%(threadName)s] %(name)s\n  %(message)s
28
+#debug_file = mopidy.log
29
+#config_file =
30
+
31
+[audio]
32
+mixer = alsamixer
33
+mixer_volume = 100
34
+output = alsasink
35
+#buffer_time = 
36
+
37
+[proxy]
38
+#scheme = 
39
+#hostname = 
40
+#port = 
41
+#username = 
42
+#password = 
43
+
44
+[mpd]
45
+#enabled = true
46
+#hostname = 127.0.0.1
47
+#port = 6600
48
+#password = 
49
+#max_connections = 20
50
+#connection_timeout = 60
51
+#zeroconf = Mopidy MPD server on $hostname
52
+#command_blacklist = 
53
+#  listall
54
+#  listallinfo
55
+#default_playlist_scheme = m3u
56
+
57
+[http]
58
+#enabled = true
59
+#hostname = 127.0.0.1
60
+#port = 6680
61
+#static_dir =
62
+#zeroconf = Mopidy HTTP server on $hostname
63
+
64
+[stream]
65
+#enabled = true
66
+#protocols = 
67
+#  http
68
+#  https
69
+#  mms
70
+#  rtmp
71
+#  rtmps
72
+#  rtsp
73
+#metadata_blacklist = 
74
+#timeout = 5000
75
+
76
+[m3u]
77
+#enabled = true
78
+#base_dir =
79
+#default_encoding = latin-1
80
+#default_extension = .m3u8
81
+#playlists_dir =
82
+
83
+[softwaremixer]
84
+enabled = false
85
+
86
+[file]
87
+#enabled = true
88
+#media_dirs = 
89
+#  $XDG_MUSIC_DIR|Music
90
+#  ~/|Home
91
+#show_dotfiles = false
92
+#follow_symlinks = false
93
+#metadata_timeout = 1000
94
+
95
+[local]
96
+enabled = true
97
+library = json
98
+media_dir = /home/mopidy/media
99
+#scan_timeout = 1000
100
+#scan_flush_threshold = 100
101
+#scan_follow_symlinks = false
102
+#excluded_file_extensions = 
103
+#  .directory
104
+#  .html
105
+#  .jpeg
106
+#  .jpg
107
+#  .log
108
+#  .nfo
109
+#  .png
110
+#  .txt
111
+
112
+[alsamixer]
113
+card = 1
114
+control = headphone volume

+ 10
- 0
mopidy/mopidy.service View File

@@ -0,0 +1,10 @@
1
+[Unit]
2
+Description=Mopidy
3
+After=network.target
4
+[Service]
5
+Type=simple
6
+ExecStart=/usr/local/bin/mopidy
7
+Restart=on-abort
8
+User=mopidy
9
+[Install]
10
+WantedBy=multi-user.target

+ 95
- 0
welcomehome View File

@@ -0,0 +1,95 @@
1
+#!/bin/bash
2
+
3
+
4
+##### FUNCTIONS #####
5
+
6
+log() {
7
+    # Beautiful logs
8
+    date=`date +"%d/%m/%Y - %H:%M:%S"`
9
+    echo "[$date]" $1
10
+}
11
+
12
+play_stream() {
13
+    # Reset playback state
14
+    curl -s -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.stop"}' http://localhost:6680/mopidy/rpc > /dev/null
15
+    curl -s -d '{"jsonrpc": "2.0", "id": 1, "method": "core.tracklist.clear"}' http://localhost:6680/mopidy/rpc > /dev/null
16
+
17
+    # Add track infos
18
+    stream=$1
19
+    log "Playing stream $stream"
20
+
21
+    data='{"jsonrpc": "2.0", "id": 1, "method": "core.tracklist.add", "params": [ [ { "__model__": "Track", "uri": "STREAM" } ] ] }'
22
+    # Using sed cuz simple quotes
23
+    data=`echo $data | sed "s,STREAM,${stream},"`
24
+
25
+    # Play dat sound
26
+    curl -s -d "$data" http://localhost:6680/mopidy/rpc > /dev/null
27
+    curl -s -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.play"}' http://localhost:6680/mopidy/rpc > /dev/null
28
+}
29
+
30
+stop_stream() {
31
+    # Stop dat sound
32
+    curl -s -d '{"jsonrpc": "2.0", "id": 1, "method": "core.playback.stop"}' http://localhost:6680/mopidy/rpc > /dev/null
33
+    log "Stream stopped"
34
+}
35
+
36
+parse_arg() {
37
+    # Echoes the value of a given parameter in a given configuration file
38
+    value=$(cat $1 | grep -E "^$2" | cut -d"=" -f2)
39
+    value=$(echo $value | sed "s,^\s+,,")
40
+    echo $value
41
+}
42
+
43
+check_url() {
44
+    # Check if URL is starting with "http(s)://"
45
+    # Returns the same code as grep, which is 1 if incorrect,
46
+    # 0 if correct
47
+    echo $1 | grep -E "https?\://"
48
+    return $?
49
+}
50
+
51
+
52
+##### PROGRAM #####
53
+
54
+if [[ $# -eq 0 || ! -f $1 ]]; then
55
+    echo "I need a config file as first argument"
56
+    exit 1
57
+fi
58
+
59
+config_file=$1
60
+
61
+log "Parsing the configuration file"
62
+
63
+# Get parameters from the given configuration file
64
+stream=$(parse_arg $config_file "stream")
65
+threshold=$(parse_arg $config_file "device_threshold")
66
+address=$(parse_arg $config_file "device_address")
67
+
68
+if [ ! $(check_url $stream) ]; then
69
+    echo "Incorrect stream address"
70
+    exit 1
71
+fi
72
+
73
+log "Initialisation complete"
74
+
75
+while true; do
76
+    # Lock file
77
+    lock_file="/etc/welcomehome/home.lck"
78
+
79
+    count=$(ping -W 1 -c $threshold $address | grep ttl | wc -l)
80
+
81
+    if [ $count -gt 0 ]; then
82
+        # mopidy.lck present = music already playing
83
+        if [ ! -f $lock_file ]; then
84
+            log "Device detected"
85
+            play_stream $stream
86
+            touch $lock_file
87
+        fi
88
+    else
89
+        if [ -f $lock_file ]; then
90
+            log "Device lost"
91
+            stop_stream
92
+            rm $lock_file
93
+        fi
94
+    fi
95
+done

+ 10
- 0
welcomehome.example.conf View File

@@ -0,0 +1,10 @@
1
+# Static address pointing to the device you wish to check for
2
+# Can be a manually attributed static DHCP lease bound to the device
3
+device_address = 127.0.0.1
4
+
5
+# Greater threshold = Longer time to detect status change
6
+# but also greater accuracy if the device's wifi is crap
7
+device_threshold = 3
8
+
9
+# URL of the stream to be played when the device is detected
10
+stream = http://fr5.ah.fm:443

+ 9
- 0
welcomehome.service View File

@@ -0,0 +1,9 @@
1
+[Unit]
2
+Description=Welcome Home
3
+After=mopidy.service
4
+[Service]
5
+Type=simple
6
+ExecStart=/usr/local/bin/welcomehome /etc/welcomehome/welcomehome.conf
7
+Restart=on-abort
8
+[Install]
9
+WantedBy=multi-user.target