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
#define base 0x378 /* printer port base address */
int main(int argc, char **argv)
{
if (ioperm(base,1,1))
fprintf(stderr, "Couldn't get the port at %xn", base), exit(1);
outb(0, base);
return 0;
}
lpton.c:#include
#include
#include
#include
#include
#define base 0x378 /* printer port base address */
int main(int argc, char **argv)
{
if (ioperm(base,1,1))
fprintf(stderr, "Couldn't get the port at %xn", base), exit(1);
outb(0b00000001, base);
return 0;
}
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.
BlueHost is the best hosting provider for any hosting plans you require.