Sunday, November 25, 2012

RPi Changes


As promised here are the changes that I made to the RPi to accommodate the physical button that allows changing of the station being played.

1) add another user that will monitor for the button (adduser radiochange)
2) make changes to the sudoers... it should now have the following lines.
radio ALL=(radio)NOPASSWD:/usr/bin/doradio,/usr/bin/mpg123 *,/usr/bin/killall mpg123, /usr/bin/amixer *

www-data ALL=(radio)NOPASSWD:/usr/bin/doradio,/usr/bin/mpg123 *,/usr/bin/killall mpg123, /usr/bin/amixer *

radiochange ALL=(radiochange)NOPASSWD:/usr/bin/radioSwitch,/usr/bin/doradio,/usr/bin/mpg123 *,/usr/bin/killall mpg123, /usr/bin/amixer *


3) Create the switch monitor program (vi /usr/bin/radioSwitch) and put the following code in it:

# python3
# Monitor button on RPI
import RPi.GPIO as GPIO
import time
import subprocess
change_channel = 1
# use BCM GPIO
GPIO.setmode(GPIO.BCM)
# set up GPIO input pins
# (pull_up_down be PUD_OFF, PUD_UP or PUD_DOWN, default PUD_OFF)
GPIO.setup(1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# GPIO 0 & 1 have hardware pull ups fitted in the Pi so don't enable them
GPIO.setup(0, GPIO.IN, pull_up_down=GPIO.PUD_OFF)
GPIO.setup(change_channel, GPIO.IN, pull_up_down=GPIO.PUD_OFF)
print("RadioPI Monitoring for button press")
delay = range(0,5000)
dum = 0
while True:
 while GPIO.input(change_channel) != 0:
  time.sleep(0.07)
 print("Button Pressed")
 output = subprocess.Popen("/usr/bin/radioChange", shell=True)
 output.wait()
#finished a run so start again

4) Make the new program executable: chmod 755 /usr/bin/radioSwitch

5) Setup inittab to fire off the radioSwitch program, find the following line and comment it out.

#2:23:respawn:/sbin/getty 38400 tty2

Add the following line

2:2345:respawn:/bin/login -f radiochange tty2 /dev/tty2 2>&1


6) Change the doRadioScript.php...   replace the line

 $inName = trim($_GET["inName"]);

with

    if ($_GET["inName"] > "")
    {
      $inName = trim($_GET["inName"]);
    } else {
      $inName = trim($radioList[$nextStation][0]);
    }

7) Change the stationCommon.php script to the following:

<?php
        {
           $radioList[] = $tempArray;
           if (trim($tempArray[0]) == $currentStation)
           {
              $currentURL = trim($tempArray[1]);
              $stationPosition = $stationCount;
           }
           $stationCount++;
        }
    }
    fclose($file);
    $nextStation = $stationPosition + 1;
    if ($nextStation >= $stationCount) $nextStation = 0;
?>

8) There is also a bug in the current sound drivers so you eed to change the doRadio script... and make it look like the following (you could probably change the 2s to something less but I didn't try it)... and the amixer line just bumps up the volume.

sudo -uradio killall  mpg123
sleep 2s
sudo -uradio /usr/bin/amixer set PCM 9
sleep 2s
sudo -uradio /usr/bin/mpg123 -@ `cat /var/www/stationURL` &

I think that's all the changes.  If I think of anymore I'll post them.

Tuesday, September 11, 2012

BRPi - Completed

I've got the Bathroom Radio Pi completed now. I decided to add a button onto the case that allows me to change channels without having to go to the webpage. I accomplished this with some modifications to the PHP scripts, creating a python script to monitor for a button press through the GPIO and one more shell script to call the PHP when the button was pressed. It works pretty slick... it just cycles through the radio stations in the StationList file. Maybe sometime I'll post the updated and additional scripts :)

Sunday, August 26, 2012

Bathroom RPi Radio

So the RPi has USB issues and I can't get my wireless USB stick to work on it for the bathroom RPi radio, so I guess I'll just have to install a network jack in the bathroom :) Not 100% sure how I'm going to get the wire up that wall but we'll see what I can do. Wish the power supplies I ordered would hurry up and show up...

Tuesday, July 17, 2012

Radio PI Followup

I ended up tweaking the scripts a little to display 3 columns of stations but other than that the radio is working great. One other thing I found was that some of the radio stations start having issues with an irregular clicking sound that is not part of the stream. I found that changing the station and then changing it right back fixes the issue for awhile. So I put in a cron job to run the doradio script every 2 hours (0 */2 * * * /usr/bin/doradio). We'll see how that works out.

Sunday, July 8, 2012

Radio PI

I decided to go ahead and release my Radio PI setup. Steps 2 & 3 are completely optional and not needed for playing internet radio at all, I just like having Webmin on all of my Linux Boxes.
This assumes that you are running the Wheezy Beta RPI distro or later (audio is fixed in Wheezy)

1) log into console: /etc/init.d/ssh start so you can login from the pc :)
2) Install webmin  (wget http://prdownloads.sourceforge.net/webadmin/webmin-1.590.tar.gz)  tar xvzf filename   cd webmin_folder   ./setup.sh
3) Set SSH to auto start at bootup in Webmin
4) install PHP  (apt-get install php5)  this will also install apache
5) install mpg123 (apt-get install mpg123)
6) Create a user that will auto login (adduser radio)
7) set the hostname (hostname RadioPI)
8) Create the web pages that will be used to change radio stations

/var/www/stationCommon.php
*********************************
<?php
// Created 7/7/2012 by Gregg Buntin - http://pentiumprime.realmlabs.com/
    $file = fopen("stationList","r");
    while(!feof($file))
    {
        $string = fgets($file);
        $tempArray = explode("~",$string);
        $radioList[] = $tempArray;
    }
    fclose($file);
    $currentStation = trim(file_get_contents("station"));
?>
*********************************

/var/www/index.php
*********************************
<?php
// Created 7/7/2012 by Gregg Buntin - http://pentiumprime.realmlabs.com/
    include("stationCommon.php");
?>
<html>
<head>
   <link href="station.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#669">
    <center>
    <h2 style="color: #0f0;"><?=file_get_contents("/etc/hostname");?></h2>
    <table>
       <tr>
<?php
    $colCount = 0;
    foreach ($radioList as $key => $stationData)
    {
        if (trim($stationData[0]) > "")
        {
           $stationName = trim($stationData[0]);
           $stationUrl = trim($stationData[1]);
           if ($stationUrl > "")
           {
               $buttonClass = "button";
               if ($currentStation == $stationUrl)
               {
                   $buttonClass .= " save";
                   $currentName = $stationName;
               } // if currentStation
               else
               {
                   $buttonClass .= " play";
               }
               $colCount++;
               if ($colCount == 3) print "</tr>\r\n<tr>\r\n";
               print "<td><button id='$stationName' onclick=\"doRadio('$stationUrl','$stationName');\" class=\"$buttonClass\">$stationName</button></td>\r\n";
           } // if stationUrl
        } // if stationData[0]
    } // foreach
?>
       </tr>
    </table>
    </center>
    <iframe id="radioFrame" style="height: 0px; width: 0px; visibility: hidden;"></iframe>
    <script>
        var lastStation = '<?=$currentName;?>';
        var theFrame = document.getElementById('radioFrame');
        function doRadio(inUrl, inName)
        {
               if (lastStation > '')
               {
                  lastButton = document.getElementById(lastStation);
                  lastButton.innerText = lastStation;
                  lastButton.className = "button play";
               }
               if (inName > '')
               {
                  newButton = document.getElementById(inName);
                  newButton.innerText = inName;
                  newButton.className = "button save";
                  theFrame.src = "doRadioScript.php?inUrl="+inUrl;
                  lastStation = inName;
               }
        }
    </script>
</body>
</html>
*********************************

/var/www/station.css
*********************************
# CSS from this site: http://www.red-team-design.com/just-another-awesome-css3-buttons
.button
{
        display: inline-block;
        white-space: nowrap;
        background-color: #9cf;
        background-image: -webkit-gradient(linear, left top, left bottom, from(#eee), to(#9cf));
        background-image: -webkit-linear-gradient(top, #eee, #9cf);
        background-image: -moz-linear-gradient(top, #eee, #9cf);
        background-image: -ms-linear-gradient(top, #eee, #9cf);
        background-image: -o-linear-gradient(top, #eee, #9cf);
        background-image: linear-gradient(top, #eee, #9cf);
        filter: progid:DXImageTransform.Microsoft.gradient(startColorStr='#eeeeee', EndColorStr='#99ccff');
        border: 1px solid #777;
        padding: 0 1.5em;
        margin: 0.5em;
        font: bold 2em/3em Arial, Helvetica;
        text-decoration: none;
        color: #333;
        text-shadow: 0 1px 0 rgba(255,255,255,.8);
        -moz-border-radius: .2em;
        -webkit-border-radius: .2em;
        border-radius: .2em;
        -moz-box-shadow: 0 0 1px 1px rgba(255,255,255,.8) inset, 0 1px 0 rgba(0,0,0,.3);
        -webkit-box-shadow: 0 0 1px 1px rgba(255,255,255,.8) inset, 0 1px 0 rgba(0,0,0,.3);
        box-shadow: 0 0 1px 1px rgba(255,255,255,.8) inset, 0 1px 0 rgba(0,0,0,.3);
}

.button:hover
{
        background-color: #cfc;
        background-image: -webkit-gradient(linear, left top, left bottom, from(#fafafa), to(#cfc));
        background-image: -webkit-linear-gradient(top, #fafafa, #cfc);
        background-image: -moz-linear-gradient(top, #fafafa, #cfc);
        background-image: -ms-linear-gradient(top, #fafafa, #cfc);
        background-image: -o-linear-gradient(top, #fafafa, #cfc);
        background-image: linear-gradient(top, #fafafa, #cfc);
        filter: progid:DXImageTransform.Microsoft.gradient(startColorStr='#fafafa', EndColorStr='#cfc');
}

.button:active
{
        -moz-box-shadow: 0 0 4px 2px rgba(0,0,0,.3) inset;
        -webkit-box-shadow: 0 0 4px 2px rgba(0,0,0,.3) inset;
        box-shadow: 0 0 4px 2px rgba(0,0,0,.3) inset;
        position: relative;
        top: 1px;
}

.button:focus
{
        outline: 0;
        background: #fafafa;
}

.button:before
{
        background: #9cf;
        background: rgba(0,0,0,.1);
        float: left;
        width: 1em;
        text-align: center;
        font-size: 1.5em;
        margin: 0 1em 0 -1em;
        padding: 0 .2em;
        -moz-box-shadow: 1px 0 0 rgba(0,0,0,.5), 2px 0 0 rgba(255,255,255,.5);
        -webkit-box-shadow: 1px 0 0 rgba(0,0,0,.5), 2px 0 0 rgba(255,255,255,.5);
        box-shadow: 1px 0 0 rgba(0,0,0,.5), 2px 0 0 rgba(255,255,255,.5);
        -moz-border-radius: .15em 0 0 .15em;
        -webkit-border-radius: .15em 0 0 .15em;
        border-radius: .15em 0 0 .15em;
        pointer-events: none;
}

/* Hexadecimal entities for the icons */

.save:before
{
        content: "\2714";
}

.play:before
{
        content: "\25B6";
}
*********************************

/var/www/doRadioScript.php
*********************************
<?php
// Created 7/7/2012 by Gregg Buntin - http://pentiumprime.realmlabs.com/
    include "stationCommon.php";

    $inUrl = trim($_GET["inUrl"]);
    
    // Check to make sure the passed in URL is in our list of valid radio stations
    $DoIt = false;
    foreach ($radioList as $key => $stationData)
    {
        if (trim($stationData[0]) > "")
        {
           $stationUrl = trim($stationData[1]);
           if ($stationUrl > "")
           {
               if ($inUrl == $stationUrl)
               {
                   $DoIt = true;
               } // if inUrl
           } // if stationUrl
        } // if stationData[0]
    } // foreach

    // Passed in Url is in our List so write it out and restart mpg123
    if ($DoIt)
    {
       $file = fopen("/var/www/station","w");
       fwrite($file,$inUrl);
       fclose($file);
       echo shell_exec("/usr/bin/doradio > /dev/null 2>/dev/null &");
    }
?>
*********************************

/var/www/stationlist
*********************************
Antioch ~ http://lin2.ash.fast-serv.com:9022/listen.pls
WTMJ ~ http://1942.live.streamtheworld.com:3690/WTMJAM_SC
*********************************

/var/www/station (be sure to chmod 777 this file)
*********************************
http://lin2.ash.fast-serv.com:9022/listen.pls
*********************************

9) create the script to change the radio station based on their selections:

/usr/bin/doradio (be sure to chmod 755 this file)
*********************************
# Created 7/7/2012 by Gregg Buntin - http://pentiumprime.realmlabs.com/
sudo -uradio killall  mpg123
sudo -uradio /usr/bin/mpg123 -@ `cat /var/www/station` &
*********************************

10) Setup for the Auto Login (vi /etc/inittab)
    find this line and comment it out: 1:2345:respawn:/sbin/getty 115200 tty1
    below it add this line:  1:2345:respawn:/bin/login -f radio tty1 </dev/tty1 >/dev/tty1 2>&1
    save it
    
11) set the user to start the radio on login
    vi /home/radio/.bash_profile  and add the following line
    /usr/bin/doradio
    save it
    
12) setup www-data so it can sudo to the radio user
    vi /etc/sudoers and add the following lines:
        radio ALL=(radio)NOPASSWD:/usr/bin/doradio,/usr/bin/mpg123 *,/usr/bin/killall mpg123
        www-data ALL=(radio)NOPASSWD:/usr/bin/doradio,/usr/bin/mpg123 *,/usr/bin/killall mpg123
    save it
    
13) REBOOT and you are done!

Monday, July 2, 2012

Stuff

Was a pretty productive week last week.  Got my RPi running RBMC working well in the basement and I use the XBMC Android Remote Control published by the XBMC team.

Got 2 more RPis that I have to decide what to do with yet.

Fixed my Wii setup.

Fixed 2 monitors.

And ghosted a 500gb HD to a 1tb HD.

All in all... pretty good.

Thursday, May 31, 2012

Joooooy

Got the RPi working with XBMC nicely.  Even works with my wireless keyboard & mouse.

Took me a bit to get it to talk to my file share, I had to reset the password in Samba rather than on the system itself (doh, I knew that).

Working GREAT... very happy with it.  Now to put it in its case, paper for now :)

Mmmmm... Pi....

My first RaspberryPI arrived.  If you don't know what that is, what cave have you been living in? ;)

Total cost was about $42 shipped.  I'm loading Raspbmc on it as I type.

Forgot to connect the Lan cable at first so it was pissing me off until I noticed that.  Lesson 1, connect all cables :)

Friday, March 16, 2012

Da Cube

I started on the other board and as I was putting the pieces on, I found that I forgot to order a few parts :(

I will continue to work on the board as much as I can until the new parts arrive (2 - 3 weeks).  Ah well...  at least I found a new supplier that had less expensive parts :)  (http://www.taydaelectronics.com/)

Saturday, March 10, 2012

Cube Progress

I've been ignoring the cube for quite awhile now because I hit a part that just kept confusing me every time I looked at it.  So I brought it to work and had one of my coworkers glance at it and he helped me get past my confusion.

I've now got the first board completed (I think).  I ran through a bunch of the connections for continuity and the ones that should work do.  I did have to fix a couple of broken wires but overall everything went well.

I will be starting on the second board shortly so hopefully I'll be able to get it up and running before April.
Copyright Realm Laboratories, LLC