Saturday, June 23, 2012

Desk Lamp Web Control

Once I got the web server working from the last post, it was time to get the parallel port to do something useful. As its first task, I decided to hook it up to my desk lamp so that anyone in the world could turn the light in my apartment on and off. If you would like to try it yourself (and my computer is on), go to http://108.233.132.201/.

THIS IS IMPORTANT: I can't tell who is turning my lamp on and off yet. So! If you're going to play around with it, please let me know who you are. Phone/text/email/facebook are all acceptable, or just leave a comment below.

I used an AC adaptor I had laying around (from my kitchen light switch mod from two and a half years ago) to get 12V DC for the relay. When I actually hook this up in the kitchen, I plan on splicing in to the old Gateway's power supply to get the 12V. I tied the output ground from the power supply to the 0V pins of the parallel port and put an LED in to let me know when I had the device powered up. Next I wired up a TIP31 NPN bipolar junction transistor. The base was attached to one of the parallel pins, the collector was attached to the relay coil (I also put an LED here just for peace of mind), and the emitter was attached to ground. And that's it! Once I get this in the kitchen (and wired up properly, i.e. not with live 120AC wires taped to terminals) I'll post a video of it in action. 

An overview of the setup. Parallel port on the left, electronics in the middle, AC adaptor, relay, and the lamp's plug on the upper right.


A close-up of the electronics. The IC on the right is a 555 timer that's not being used. 

Everything together including the lamp. 

My not-quite-up-to-code wiring, which shows how I fed the 120V AC from the wall, through the adaptor, through the relay, and to the lamp. Not super safe but I won't leave it plugged in until I build a more permanent enclosure for it.


Also I promise I had this idea before this episode of the Big Bang Theory aired. WHAT A COINCIDENCE! This is what I felt like though. Then it was creepy when people started turning my lamp on and off.

Tuesday, June 19, 2012

Progress on the "Turing" Slow Cooker

I have successfully managed to control the pins on a parallel (printer) port over the internets. I now document my efforts.

I already have this working on my personal desktop (which is apparently old enough to still have a parallel port in it?). For the purposes of writing this without flaws, I will be re-creating my work on the old Gateway in the kitchen, mostly using SSH. As of this writing, the computer is running Ubuntu 12.04 with LXDE.

First, I installed the apache2 webserver.

sudo apt-get install apache2

Next, I need to modify apache to run CGI scripts. This is much easier than everyone else on the internet seems to think. First, open /etc/apache2/apache2.conf with your favorite text editor and add the following line above  
ErrorLog ${APACHE_LOG_DIR}/error.log:

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ 

Now it is time to get dirty. I used www.epanorama.net/circuits/parallel_output.html as a primer. It is full of spelling and grammar mistakes (and a few coding mistakes) but it's legible and helpful. Any way, the default web page that apache serves is /var/www/index.html. Mine looks like this:

<html>
<head>
<title>Parallel port controlling test page</title>
</head>
<body>
<h1>Parallel port controlling test page</h1>
<p>
<form action="/cgi-bin/lpton.cgi" method="get" name="on">
<input type=submit value="Set all data pins high">
</form>
<p>
<form action="/cgi-bin/lptoff.cgi" method="get" name="off">
<input type=submit value="Set all data pins low">
</form>
<p><img src="pic.png">
<p>
</body>
</html>


The picture was to double-check to make sure apache was serving more than just index.html because at first I had problems serving the CGI scripts. Any way! Now we need to get those CGI scripts written. I use two, in this example, one to set all the data pins on the parallel port high (turn things on) and one to set them all low (turn them off). I started off in ~/programs with two C programs.


lptoff.c:
include
#include
#include
#include
#include


lpton.c:
#include
#include
#include
#include
#include


I compiled the programs:

gcc -O lptoff.c -o lptoff
gcc -O lpton.c -o lpton 


Then I moved the programs to the /usr/sbin/ directory and gave them root access. This means that every time these programs are called they run as root automatically, even if the user who executes the programs does not have root privileges. This may seem like bad practice, but the reason for this is that writing to the parallel port requires root. This seems unavoidable. Just make sure the program won't get stuck in an infinite loop or it will be difficult to stop it. Any way...

sudo cp lptoff /usr/sbin/
sudo cp lpton /usr/sbin/
cd /usr/sbin
sudo chmod +s lptoff
sudo chmod +s lpton


The next step is to write CGI scrips that tell apache to run these two programs. They will be placed in the /usr/lib/cgi-bin/ directory. There are two buttons in this example, so there are two CGI scripts:

lptoff.cgi:
#!/bin/sh
# Parallel port CGI script
#
# Send HTTP headers
echo Content-type: text/html;charset=ISO-8859
echo
# Do the controlling
/usr/sbin/lptoff
# Output web page data
echo ""
echo "Parallel port controlled
"
echo "Go back to controlling page"
echo ""
#


lpton.cgi:
#!/bin/sh
# Parallel port CGI script
#
# Send HTTP headers
echo Content-type: text/html;charset=ISO-8859
echo
# Do the controlling
/usr/sbin/lpton
# Output web page data
echo ""
echo "Parallel port controlled
"
echo "Go back to controlling page"
echo ""
#


That's all! Again, credit where credit is due, most of the pages came from http://www.epanorama.net/circuits/parallel_output.html#webcontrol, and I modified the rest to suit my needs. This blog is primarily a way that I can keep track of what I do, and that is what I have done. Hooray science!

A video of it in action:




UPDATE: Actually a lot of this code is messed up because it's almost impossible to post code with this blog publisher's web software. So if anyone REALLY WANTS the files, maybe I'll make them available for download from my server. Also. You'll need to make the .cgi files executable! This is important otherwise you'll get a "INTERNAL SERVER ERROR!!!!!11" error when you try to run this. Do sudo chmod +x on the files and you'll be fine.

Sunday, June 10, 2012

Slow Cooker + Fridge Speakers

The iPod finally gave up all the way. The screen still works but it doesn't play audio even if it's cold. As a stop-gap in the awesomeness that is fridge speakers, I had been using the "brypod" to play the fridge music, but I wanted that back for listening to music in other places. So I took the old Gateway computer I have, put a fun version of Linux on it, and made it play the music in the fridge. As a bonus: This computer also has a parallel port (a consequence of it being about 12 years old) which I will eventually use to control other things in the kitchen over the internet, such as the slow cooker. This is how I did it!

First! I downloaded the Ubuntu 12.04 x86 (32-bit) alternate desktop installer and burned it to a CD-RW and used that to install Ubuntu to the Gateway. This led to a problem that is becoming worse and worse with Ubuntu: despite having an "alternate installer" for old computers that don't have enough system resources to run the GUI, it's either impossible or very difficult to boot it to a command-line interface once it is installed. To me, this seems to defeat the purpose of having an alternate installer, but I also don't claim to be any sort of Linux expert. But any way! I spent about two hours coaxing the resource hog of a GUI into a terminal window so I could do something useful.

sudo apt-get install lxde

This installed a new GUI called LXDE which is extremely efficient and light-weight (unlike GNOME/Unity), without being downright unusable and terrible (unlike Fluxbox). After logging out and logging in to my LXDE session, I was able to actually use the computer.

sudo apt-get install ssh vlc x11vnc build-essential ubuntu-restricted-extras
sudo apt-get update
sudo apt-get upgrade


This installed all the system updates as well as the tools I will be using on this computer:
  1. VLC, a versatile media player (easier to manipulate than my personal preference, Rhythmbox)
  2. SSH, which allows me to login to the computer remotely via command-line
  3. X11VNC, which gives me remote desktop control (so I don't have to have a monitor/keyboard hooked up to the computer).
I needed to configure the remote desktop software to start automatically at boot.

x11vnc -storepasswd

It is necessary to have a password. Next: I went to ~/.config/autostart and created a shortcut named X11VNC.desktop. (It is necessary to create the autostart directory if it does not exist already.) The file should look like this:

[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=X1VNC
Comment=
Exec=x11vnc -forever -usepw -httpport 5900
StartupNotify=false
Terminal=false
Hidden=false


The next step is to configure the login options:
sudo nano /etc/lightdm/lightdm.conf

Now I need to configure the computer to automatically log in to an LXDE session when it starts up, so I don't have to type in a user name and password. I changed the lightdm.conf file from this:

[SeatDefaults]
user-session=ubuntu
greeter-session=unity-greeter


to this:

[SeatDefaults]
user-session=lxde
greeter-session=unity-greeter
autologin-user=bryan
autologin-user-timeout=0


The next step is to install folder sharing software (samba) so I can easily drag and drop music to the computer for my refrigerator listening pleasure. I decided to share the Music folder (~/Music) over the network. (I could have used SSH to do this, but it's a lot easier to drag-and-drop).

Once all the music I wanted was on the computer, I moved all the .mp3 files to one folder and created an .m3u playlist:

ls -d * | grep .mp3 > "${PWD##*/}.m3u"

This creates a file called ~/Music/Music.m3u. The final step is to tell the computer to start VLC when it boots, AND play this playlist file when it starts the program, AND turn on repeat and shuffle. This was accomplished by making a file called vlc.desktop and placing it in the autostart folder.


[Desktop Entry]
Encoding=UTF-8
Version=0.9.4
Type=Application
Name=vlc
Comment=
Exec=vlc /home/bryan/Music/Music.m3u --random --loop
StartupNotify=false
Terminal=false
Hidden=false


That's it! Now the computer plays fridge music, is accessible remotely by VNC, SSH, and Samba, and is ready to start controlling things in the kitchen over the internets!