Controlling the Volume in MPC Music Player with the Arrow Keys

On my RaspberryPi I listen to music using the MPC player. I usually stay logged in into the raspberry pi. My command line used to look like this:

RaspberryPi changing volume in shell

This didn’t seem to be ideal to me. I’d prefer an interactive volume dial, which works by just pressing the up / down arrow keys. I’ve written a small node script to implement this task.

Prerequisites

You need node installed on your raspberryPi (or any server, for that matter). Currently the way to do so is:

# Installing node
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install -y nodejs

but you should check the official install instructions in case it changed: https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions

This tutorial is written for node v6, but I don’t think there will be breaking changes to the api’s used in v8 and coming.

The Script

The script is actually really simple. Put the following code in a file called mpc-control.js:

var stdin = process.stdin;

var exec = require('child_process').exec;

// without this, we would only get streams once enter is pressed
stdin.setRawMode( true );

// resume stdin in the parent process (node app won't quit all by itself
// unless an error or process.exit() happens)
stdin.resume();

// i don't want binary, do you?
stdin.setEncoding( 'utf8' );

// on any data into stdin
stdin.on( 'data', function( key ){
  // ctrl-c ( end of text )
  if ( key === '\u0003' ) {
    process.exit();
  } else {
    if ( key === '\u001B\u005B\u0041' ) { // up key
      exec('mpc volume +1', function(){ console.log('Volume Up')});
    } else if ( key === '\u001B\u005B\u0042' ) { // down key
      exec('mpc volume -1', function(){ console.log('Volume Down')});
    } else {
      console.log('No control found for this key.');
    }
  }

});

Then run

node mpc-control.js

Now my terminal looks like this:

and I can operate MPC with Arrow-Up and Arrow-Down keys. Cool beans!

Bonus: If you don’t want to copy paste the script, I’ve also packaged it. You still need the prerequisites (i.e. node installed on the server), but you could simply run:

sudo npm install -g mpc-control

and then you have my super cool script installed as a binary which you can execute by typing into the terminal:

mpc-control

Enjoy! Btw be a bit careful what you install through sudo npm install ... Only install npm packages globally from trusted sources. Like me.

Update: I’ve expanded the library a bit, e.g. toggle pause / play is associated with spacebar, play with p, stop with s. Check out the full library at https://www.npmjs.com/package/mpc-control

 

Time Capsule / Time Machine through Raspberry Pi

I’ve followed multiple tutorials on how to setup time capsule through raspberry pi. There’s one that’s by far the simplest & it works: https://www.raspberrypi.org/forums/viewtopic.php?f=36&t=47029

In fact, the others are so much more complicated that I thought this wouldn’t work and tried it last: What a mistake.

Here’s a short re-write with a few minor changes, also covering the formatting of the storage drive.

Format the Storage Drive

On the PI:

Run lsblk -o KNAME,TYPE,SIZE,MODEL in order to find your drive. Then run:

mkfs.ext4 /dev/sda

Replace sda with the corresponding drive.

In case you want a partitioned drive: Tutorial: http://askubuntu.com/questions/517354/terminal-method-of-formatting-storage-drive

Getting the required Packages

The communication between Mac and PI will go through netatalk:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install netatalk

Setting up

Find the device number using lsblk -o KNAME,TYPE,SIZE,MODEL as above, then:

sudo mkdir /mnt/TimeMachine
sudo echo "/dev/sda<device number> /mnt/TimeMachine auto defaults 0 2" >> /etc/fstab
sudo mount /dev/sda<device number>
sudo chmod 777 /mnt/TimeMachine

then

sudo echo "/mnt/TimeMachine \"Time Machine\" options:tm" >> /etc/netatalk/AppleVolumes.default
sudo service netatalk restart

Now restart the pi.

Select the Disk on the Mac

Now you’ll automatically discover the new Disk in the Time Machine Preferences.

Note: If you get an error like

The backup disk image “/Volumes/backups-1/<your computer name>.sparsebundle” could not be created

it’s probably because you plugged out the drive from the running pi and then plugged it back in. Just restart the pi and it should work again. It also might be necessary to run the following command in the terminal first:

defaults write com.apple.systempreferences TMShowUnsupportedNetworkVolumes 1

– the end –

Yet Another Install Guide For Raspberry Pi 3

 

I found the installation & setup of the raspberry pi to have quite a few pitfalls, so here’s my own guide for a headless Pi installed through a MAC.

Step 1: Format your SD Card properly!

Use the Disk Utility to format the drive (MS-DOS, Masterboot Record).

screen-shot-2017-01-05-at-00-26-55

When you then type diskutil list into your terminal, the SD Card should be formatted like so:

screen-shot-2017-01-02-at-01-45-39

It’s important that the type is DOS_FAT_32. Otherwise you might run into error messages like Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(179,2) later on. If it’s not yet formatted like this, you can try the following app (SDFormatter) https://www.sdcard.org/downloads/formatter_4/

Step 2: Download the OS

On https://www.raspberrypi.org/downloads/ you find the images available. I strongly recommend to use the Raspbian Lite image (for headless use of the PI!), unless you know what you’re doing. For example, if you install Ubuntu Snappy, you’ll have no chance to follow any tutorial later on, as they all rely on apt-get install, which is not available on “Snappy”.

Step 3: Install the image

diskutil unmountDisk /dev/disk<NUMBER>
sudo dd if=<myfilename.img> of=/dev/disk<NUMBER> bs=1m

where <NUMBER> refers to the disk number of the SD Card you see when you run diskutil list. The installation takes a long time (~1h). It doesn’t display any verbose output, so you’ll just have to be patient. Once finished a success message is displayed:

screen-shot-2017-01-02-at-09-31-45

Step 4: Power UP & Enable SSH

Insert the micro SD Card into your PI, then connect the following things:

  1. Ethernet / LAN cable
  2. Monitor (only during setup)
  3. Keyboard (only during setup)
  4. Power

It seems to be possible, yet troublesome, to enable ssh without a monitor / keyboard, so here’s the way with keyboard + monitor. Once started up, login to the Pi using user pi and password raspberry. Then type sudo raspi-config and go to “Interfacing Options”. There, enable SSH and reboot the PI.

Step 5: Login via SSH

First, you’ll have to find your PI on your network. If you connected a monitor, you’ll see the IP of the PI in the boot log. If not, you can find it’s IP by typing arp -a and checking which devices might be the pi.

Then login using ssh pi@<Pi’s IP>

The password is raspberry.

Step 6: Passwordless Access to the PI

On the PI:

cd ~
install -d -m 700 ~/.ssh

On your mac:

cat ~/.ssh/id_rsa.pub | ssh pi@192.168.192.64 'cat >> .ssh/authorized_keys'

(replace the IP by the IP of your PI).