OpenWrt/MightyOhm Wifi Radio

From Wiki
< OpenWrt
Revision as of 12:52, 27 September 2012 by Marcluer (talk | contribs) (→‎OpenWrt)

OpenWrt

interface.sh

#! /bin/sh -
# interface.sh - Wifi Radio User Interface Script
# 01/29/09	Jeff Keyzer	http://mightyohm.com
#
# This shell script sets up a playlist in mpd and changes playlist entries
# based on the position of a tuner knob connected to an AVR on the router's
# serial port.
#
# The script expects the AVR to send data at 9600 baud (8N1) to the router,
# in the format "tuner: value", one line at a time.
#
# This script also launches display.sh, the LCD display script.
#
# For more information, visit
# http://mightyohm.com/blog/tag/wifiradio/
#
# This work is protected by the
# Creative Commons Attribution-Share Alike 3.0 United States License.
# http://creativecommons.org/licenses/by-sa/3.0/us/ 

# Some configuration settings
VOLUME=60
STATIONS=10
MAXADC=1024

ADCBIN=$(expr "(" $MAXADC / $STATIONS ")" + 1)

trap 'kill $! ; exit 1' SIGINT	# exit on ctrl-c, useful for debugging
				# kills the display.sh process before exiting

stty 9600 -echo < /dev/tts/0	# set serial port to 9600 baud
				# so we can talk to the AVR
				# turn off local echo to make TX/RX directions
				# completely separate from each other

# mpd setup
mpc volume $VOLUME	# adjust this to suit your speakers/amplifier
mpc clear	# clear current playlist

# build a playlist, substitute your favorite radio stations here
# the first line becomes station #1, and so on.
mpc add http://relay3.slayradio.org:8000/			# Slay Radio
mpc add http://scfire-dtc-aa01.stream.aol.com:80/stream/1046	# KCRW Simulcast
mpc add http://208.101.28.234:8004				# Bassdrive
mpc add http://205.188.215.232:8016				# di.fm Soulful House
mpc add http://scfire-ntc-aa03.stream.aol.com:80/stream/1009	# di.fm Lounge
mpc add http://205.188.215.225:8002				# di.fm Breaks
mpc add http://scfire-ntc-aa03.stream.aol.com:80/stream/1025	# di.fm Electro House
mpc add http://di-fm-01.quintex.com:8888			# di.fm NYC Exposure
mpc add http://208.122.59.30:7234				# di.fm Future Synthpop
mpc add http://scfire-mtc-aa03.stream.aol.com:80/stream/1026	# di.fm Progressive

mpc playlist	# show the resulting playlist

oldstation=0	# var to keep track of what station we're playing

# Tell the AVR we're ready to start doing stuff
echo "AVR Start!\n" > /dev/tts/0

# launch LCD display routines in the background
/root/display2.sh &

while true	# loop forever
do
   inputline="" # clear input
  
   # Loop until we get a valid tuner position from the AVR 
   until inputline=$(echo $inputline | grep -e "^tuner: ")
   do
      inputline=$(head -n 1 < /dev/tts/0)
   done
   value=$(echo $inputline | sed 's/tuner: //')	# strip out the tuner: part
   #echo "$value"
  
   # compute playlist positon based on tuner value
   # the tuner range is 0 to MAXADC
   # the ADC range is separated into equally sized bins, each bin
   # is one station
   # station ends up being an integer from 1 to STATIONS
   # that determines what station to play 
   station=$(expr $value / $ADCBIN + 1)

   # if station has changed, we need to change position in the playlist 
   if [ "$station" -ne "$oldstation" ]
   then
   	echo "Tuner Position: " $value
   	echo "New station..."
   	mpc play $station
    fi
    
   oldstation=$station	# remember what station we're on so we know
   			# if we need to change stations next time around
done


display2.sh

#! /bin/sh -
# display2.sh - Wifi Radio LCD display routines, revision 2
# 01/29/08	Jeff Keyzer	http://mightyohm.com
# This shell script queries mpd for current song information and sends
# relevant bits of it to the serial port, where an AVR-based LCD display
# is waiting.
#
# For more information, visit
# http://mightyohm.com/blog/tag/wifiradio/
#
# This work is protected by the
# Creative Commons Attribution-Share Alike 3.0 United States License.
# http://creativecommons.org/licenses/by-sa/3.0/us/
#

trap 'exit 1' SIGINT	# exit on ctrl-c, useful for debugging

#stty 9600 -echo < /dev/tts/0	# set serial port to 9600 baud
				# so we can talk to the AVR

while true		# loop forever
do
   # get name of current song from the mpd server
   name=$(echo "currentsong" | nc localhost 6600 | grep -e "^Name: ")
   #echo $name
   echo $name > /dev/tts/0	# send it to the AVR for display

   # get the artist/title of the current song, if it exists
   title=$(echo "currentsong" | nc localhost 6600 | grep -e "^Title: ")
   if [ -z "$title" ]	# check if a title was returned
   then
      title="Title: NULL" # no, let the AVR know not to display a title
   fi
   #echo $title
   echo $title > /dev/tts/0	# send it to the AVR for display
   
   sleep 1	# wait a while, this determines the base rate at which
   		# the AVR display loops
   
done


mpd autostart

ln -s /etc/init.d/mpd /etc/rc.d/S93mpd


/etc/init.d/AVR

#!/bin/sh /etc/rc.common
# Copyright (C) 2008 OpenWrt.org

START=99
start() {
sleep 5    # make sure boot process is done, no more console messages
/root/interface.sh
}
ln -s /etc/init.d/AVR /etc/rc.d/S99AVR

/etc/inittab

::sysinit:/etc/init.d/rcS S boot
::shutdown:/etc/init.d/rcS K stop
#tts/0::askfirst:/bin/ash --login
#ttyS0::askfirst:/bin/ash --login
tty1::askfirst:/bin/ash --login