paulgorman.org/technical

Raspberry Pi Radio Config Notes

!!!!!! These notes assumes your Raspberry Pi resides on a secure, trusted network (e.g. behind a firewall in your own home). No consideration has been given to security. !!!!!!

Run raspi-config to turn on ssh, set boot to console, and expand filesystem

sudo apt-get update sudo apt-get dist-upgrade sudo apt-get install nginx fcgiwrap spawn-fcgi vim pulseaudio mplayer tmux

Edit /etc/network/interfaces to set static IP:

auto eth0
iface eth0 inet static
        address 10.0.1.10
        netmask 255.255.255.0
        gateway 10.0.1.1

Add the www-data user to the audio, pulse groups. Edit /etc/group:

audio:x:29:pi,pulse,www-data
pulse:x:110:pi,www-data
pulse-access:x:111:pi,www-data

Edit /etc/pulse/daemon.conf

daemonize = yes
system-instance = yes
exit-idle-time = -1

Edit /etc/default/pulseaudio

PULSEAUDIO_SYSTEM_START=1

Find the name of your prefered audio output with pacmd list-sinks.

Edit /etc/pulse/client.conf

default-sink = alsa_output.usb-C-Media_INC._USB_Sound_Device-00-MicroII.analog-stereo

/usr/bin/amixer set Master 26%

cat /dev/urandom | pacat ^C

tmux new-session -s michiganradio -d ‘pulseaudio -D ; mplayer http://ummedia12.miserver.it.umich.edu:8004/miradio’

Web Server Setup

sudo mkdir /var/www sudo mkdir /var/www/cgi-bin sudo chown -R www-data:www-data /var/www/

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/dist-default

Edit /etc/nginx/sites-enabled/default:

server {
    root /var/www/;
    index index.html;
    server_name localhost;
    location / {
        try_files $uri/ /index.html;
    }
    location /cgi-bin {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/fcgiwrap.socket;
    }
}

Restart nginx: sudo service nginx restart.

Minimal Web Radio Control

Create /var/www/index.html:

<!DOCTYPE html>
<html lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Raspberry Pi</title>
<script>
function dumbHttpRequest(url) {
    var request = new XMLHttpRequest();
    request.open("get", url, true);
    request.send();
}
</script>
</head>
<body>
<h1>Raspberry Pi</h1>
<p><button onclick="dumbHttpRequest('/cgi-bin/play-michiganradio.cgi');">Play Michigan Radio</button></p>
<p><button onclick="dumbHttpRequest('/cgi-bin/stop-michiganradio.cgi');">Stop Michigan Radio</button></p>
<p><button onclick="dumbHttpRequest('/cgi-bin/increase-volume.cgi');">Increase Volume</button></p>
<p><button onclick="dumbHttpRequest('/cgi-bin/decrease-volume.cgi');">Decrease Volume</button></p>
</body>
</html>

Create /var/www/cgi-bin/play-michiganradio.cgi:

#!/bin/dash
/bin/echo -e "Content-type: text/html\r\n\r\n"
/usr/bin/tmux new-session -s michiganradio -d 'mplayer -cache 1024 http://ummedia12.miserver.it.umich.edu:8004/miradio'

Create /var/www/cgi-bin/stop-michiganradio.cgi:

#!/bin/dash
/bin/echo -e "Content-type: text/html\r\n\r\n"
/usr/bin/tmux kill-session -t michiganradio

Create /var/www/cgi-bin/increase-volume.cgi:

#!/bin/dash
/bin/echo -e "Content-type: text/html\r\n\r\n"
/usr/bin/amixer set Master 1%+

Create /var/www/cgi-bin/decrease-volume.cgi:

#!/bin/dash
/bin/echo -e "Content-type: text/html\r\n\r\n"
/usr/bin/amixer set Master 1%-

Finally:

cd /var/www/cgi-bin sudo chown www-data:www-data ./* sudo chmod u+x ./*

Speech Synthesis

sudo apt-get install espeak

The espeak package works, but it outputs at 22050Hz, which my external USB speaker doesn’t like.

This fails: espeak “Test Test Test test test test test test test”

This works: espeak “Test Test Test test test test test test test” –stdout | mplayer -cache 1024 -af resample=44100:0:0 -