REMOTE MONITORING OF HYDROPONIC SOLUTION LEVEL USING THE COMPUTER'S PARALLEL PORT AND CGI
John T. Rine
======Objective======
The object of this project is to create a CGI application that will be used to monitor sensors via the computer's parallel port. The status of the sensors will then be accessible on the Web. The sensors will be used to monitor a hydroponics garden.
======Materials/Reading/Prerequisites======
Parts List\\
^Quantity^Description^Cost^URL^
|1|float switch|$6.99|http://www.aquahub.com/store/ifloatfloatswitch.html|
|1|tubing adapter|$1.00|http://www.aquahub.com/store/product21.html|
|1|mold-a-holder|$3.95|http://www.aquahub.com/store/moldaholder9.html|
|1|crimp caps|$1.99|http://www.aquahub.com/store/product34.html|
|1|Computer w/Linux Debian and Apache 2 Web Server|priceless|see Linux install and Apache 2 install projects|
======Background======
Web servers use the client/server model. That is, the client makes a request, and the server responds by fullfilling that request.When a URL to a Web page is entered into the Web browser, the browser segments it into protocol, server, and resource (page). The browser then sends a request for a particular page (resource to the server, and the server responds by sending the requested page; in the case of a Web page, it sends it encoded as HTML. When the browser gets the page, it interprets it and then displays it.
There are many protocols used for different purposes across the Internet for example, HTTP-Hypertext Transfer Protocol, FTP-File Transfer Protocol, and HTTPS-Secure HTTP. The communication protocol used to request and serve unsecure Web pages is HTTP. As we learned in our networking fundamentals course, the well known port for HTTP is port 80. So a Web server listens at port 80 (usually) for a request from a Web client.\\
\\
CGI is a specification for transferring information between a Web server and a CGI program. A CGI program is any program designed to accept and return data that follows the CGI specification. A CGI program could be written in any one of a number of programming and scripting languages.
In order for the scripts or programs to be executed, a Web server must be installed. Next, the file or files to be executed must be in a specific directory on the Web server. The CGI program can then be executed by including the bin-cgi directory and filename in the URL, for example: http://10.80.2.179/cgi-bin/example.\\
\\
The parallel port is no longer available as a standard interface on contemporary computers. It is a parallel communications port meaning that multiple bits are sent or received at one time. Typically there are three ports associated with the parallel port, a data port (base address), a status port (base address + 1), and a control port (base address + 2). There are different varieties of parallel port. There are uni-directional and bi-directional (IEEE 1284) parallel ports.\\
The data port of a uni-directional port is uni-directional, that is the data port bits can only be written to. Some of the status bits can, however, be read. This project uses status port bits for input, and data port bits for output. The port various port bits are mapped to the physical interface (connector).\\
======Procedure======
First, if an Apache Web server isn't installed, install it using instructions at http://library.linode.com/web-servers/apache/installation/debian-6-squeeze :\\
root@dhcp-179:/home/bob# apt-get install apache2 apache2-doc apache2-utils
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
apache2-mpm-worker apache2.2-common
Suggested packages:
apache2-suexec apache2-suexec-custom
The following NEW packages will be installed:
apache2 apache2-doc apache2-mpm-worker apache2-utils apache2.2-common
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 2,779 kB of archives.
After this operation, 15.1 MB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://security.debian.org/ squeeze/updates/main apache2-utils i386 2.2.16-6+squeeze1 [165 kB]
Get:2 http://security.debian.org/ squeeze/updates/main apache2.2-common i386 2.2.16-6+squeeze1 [307 kB]
Get:3 http://security.debian.org/ squeeze/updates/main apache2-mpm-worker i386 2.2.16-6+squeeze1 [2,230 B]
Get:4 http://security.debian.org/ squeeze/updates/main apache2 i386 2.2.16-6+squeeze1 [1,386 B]
Get:5 http://security.debian.org/ squeeze/updates/main apache2-doc all 2.2.16-6+squeeze1 [2,304 kB]
Fetched 2,779 kB in 7s (387 kB/s)
Selecting previously deselected package apache2-utils.
(Reading database ... 124913 files and directories currently installed.)
Unpacking apache2-utils (from .../apache2-utils_2.2.16-6+squeeze1_i386.deb) ...
Selecting previously deselected package apache2.2-common.
Unpacking apache2.2-common (from .../apache2.2-common_2.2.16-6+squeeze1_i386.deb) ...
Selecting previously deselected package apache2-mpm-worker.
Unpacking apache2-mpm-worker (from .../apache2-mpm-worker_2.2.16-6+squeeze1_i386.deb) ...
Selecting previously deselected package apache2.
Unpacking apache2 (from .../apache2_2.2.16-6+squeeze1_i386.deb) ...
Selecting previously deselected package apache2-doc.
Unpacking apache2-doc (from .../apache2-doc_2.2.16-6+squeeze1_all.deb) ...
Processing triggers for man-db ...
Setting up apache2-utils (2.2.16-6+squeeze1) ...
Setting up apache2.2-common (2.2.16-6+squeeze1) ...
Enabling site default.
Enabling module alias.
Enabling module autoindex.
Enabling module dir.
Enabling module env.
Enabling module mime.
Enabling module negotiation.
Enabling module setenvif.
Enabling module status.
Enabling module auth_basic.
Enabling module deflate.
Enabling module authz_default.
Enabling module authz_user.
Enabling module authz_groupfile.
Enabling module authn_file.
Enabling module authz_host.
Enabling module reqtimeout.
Setting up apache2-mpm-worker (2.2.16-6+squeeze1) ...
Starting web server: apache2.
Setting up apache2 (2.2.16-6+squeeze1) ...
Setting up apache2-doc (2.2.16-6+squeeze1) ...
Reloading web server config: apache2.
root@dhcp-179:/home/bob#
root@dhcp-179:/home/bob#
root@dhcp-179:/home/bob# apt-get install libapache2-mod-ruby
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
libapache-ruby1.8 libreadline5 libruby1.8
Suggested packages:
liberuby
The following NEW packages will be installed:
libapache-ruby1.8 libapache2-mod-ruby libreadline5 libruby1.8
0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded.
Need to get 2,180 kB of archives.
After this operation, 8,147 kB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://debian.uchicago.edu/debian/ squeeze/main libreadline5 i386 5.2-7 [135 kB]
Get:2 http://debian.uchicago.edu/debian/ squeeze/main libruby1.8 i386 1.8.7.302-2 [1,965 kB]
Get:3 http://debian.uchicago.edu/debian/ squeeze/main libapache-ruby1.8 all 1.2.6-2 [20.5 kB]
Get:4 http://debian.uchicago.edu/debian/ squeeze/main libapache2-mod-ruby i386 1.2.6-2 [59.7 kB]
Fetched 2,180 kB in 5s (407 kB/s)
Selecting previously deselected package libreadline5.
(Reading database ... 126955 files and directories currently installed.)
Unpacking libreadline5 (from .../libreadline5_5.2-7_i386.deb) ...
Selecting previously deselected package libruby1.8.
Unpacking libruby1.8 (from .../libruby1.8_1.8.7.302-2_i386.deb) ...
Selecting previously deselected package libapache-ruby1.8.
Unpacking libapache-ruby1.8 (from .../libapache-ruby1.8_1.2.6-2_all.deb) ...
Selecting previously deselected package libapache2-mod-ruby.
Unpacking libapache2-mod-ruby (from .../libapache2-mod-ruby_1.2.6-2_i386.deb) ...
Setting up libreadline5 (5.2-7) ...
Setting up libruby1.8 (1.8.7.302-2) ...
Setting up libapache-ruby1.8 (1.2.6-2) ...
Setting up libapache2-mod-ruby (1.2.6-2) ...
Enabling module ruby.
Run '/etc/init.d/apache2 restart' to activate new configuration!
root@dhcp-179:/home/bob# apt-get install libapache2-mod-perl2
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
libapache2-reload-perl libbsd-resource-perl libdevel-symdump-perl
The following NEW packages will be installed:
libapache2-mod-perl2 libapache2-reload-perl libbsd-resource-perl libdevel-symdump-perl
0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,146 kB of archives.
After this operation, 4,063 kB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://debian.uchicago.edu/debian/ squeeze/main libdevel-symdump-perl all 2.08-3 [16.6 kB]
Get:2 http://debian.uchicago.edu/debian/ squeeze/main libapache2-mod-perl2 i386 2.0.4-7 [1,077 kB]
Get:3 http://debian.uchicago.edu/debian/ squeeze/main libapache2-reload-perl all 0.10-2 [19.9 kB]
Get:4 http://debian.uchicago.edu/debian/ squeeze/main libbsd-resource-perl i386 1.2904-1 [32.5 kB]
Fetched 1,146 kB in 2s (418 kB/s)
Selecting previously deselected package libdevel-symdump-perl.
(Reading database ... 127688 files and directories currently installed.)
Unpacking libdevel-symdump-perl (from .../libdevel-symdump-perl_2.08-3_all.deb) ...
Selecting previously deselected package libapache2-mod-perl2.
Unpacking libapache2-mod-perl2 (from .../libapache2-mod-perl2_2.0.4-7_i386.deb) ...
Selecting previously deselected package libapache2-reload-perl.
Unpacking libapache2-reload-perl (from .../libapache2-reload-perl_0.10-2_all.deb) ...
Selecting previously deselected package libbsd-resource-perl.
Unpacking libbsd-resource-perl (from .../libbsd-resource-perl_1.2904-1_i386.deb) ...
Processing triggers for man-db ...
Setting up libdevel-symdump-perl (2.08-3) ...
Setting up libapache2-mod-perl2 (2.0.4-7) ...
Enabling module perl.
Run '/etc/init.d/apache2 restart' to activate new configuration!
Setting up libapache2-reload-perl (0.10-2) ...
Setting up libbsd-resource-perl (1.2904-1) ...
root@dhcp-179:/home/bob#
root@dhcp-179:/home/bob#
root@dhcp-179:/home/bob# apt-get install libapache2-mod-python
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
libapache2-mod-python-doc
The following NEW packages will be installed:
libapache2-mod-python
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 122 kB of archives.
After this operation, 537 kB of additional disk space will be used.
Get:1 http://debian.uchicago.edu/debian/ squeeze/main libapache2-mod-python i386 3.3.1-9+b1 [122 kB]
Fetched 122 kB in 0s (240 kB/s)
Selecting previously deselected package libapache2-mod-python.
(Reading database ... 128129 files and directories currently installed.)
Unpacking libapache2-mod-python (from .../libapache2-mod-python_3.3.1-9+b1_i386.deb) ...
Setting up libapache2-mod-python (3.3.1-9+b1) ...
Reloading web server config: apache2.
Processing triggers for python-central ...
root@dhcp-179:/home/bob# apt-get install python-mysqldb
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
libmysqlclient16 mysql-common
Suggested packages:
python-egenix-mxdatetime mysql-server-5.1 mysql-server python-mysqldb-dbg
The following NEW packages will be installed:
libmysqlclient16 mysql-common python-mysqldb
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 2,101 kB of archives.
After this operation, 4,788 kB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://debian.uchicago.edu/debian/ squeeze/main mysql-common all 5.1.49-3 [70.9 kB]
Get:2 http://debian.uchicago.edu/debian/ squeeze/main libmysqlclient16 i386 5.1.49-3 [1,935 kB]
Get:3 http://debian.uchicago.edu/debian/ squeeze/main python-mysqldb i386 1.2.2-10+b1 [95.8 kB]
Fetched 2,101 kB in 5s (374 kB/s)
Selecting previously deselected package mysql-common.
(Reading database ... 128154 files and directories currently installed.)
Unpacking mysql-common (from .../mysql-common_5.1.49-3_all.deb) ...
Selecting previously deselected package libmysqlclient16.
Unpacking libmysqlclient16 (from .../libmysqlclient16_5.1.49-3_i386.deb) ...
Selecting previously deselected package python-mysqldb.
Unpacking python-mysqldb (from .../python-mysqldb_1.2.2-10+b1_i386.deb) ...
Setting up mysql-common (5.1.49-3) ...
Setting up libmysqlclient16 (5.1.49-3) ...
Setting up python-mysqldb (1.2.2-10+b1) ...
Processing triggers for python-support ...
root@dhcp-179:/home/bob# apt-get install libapache2-mod-php5 php5 php-pear php5-xcache
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
apache2-mpm-prefork libonig2 libqdbm14 php5-cli php5-common php5-suhosin
Suggested packages:
php5-dev
The following packages will be REMOVED:
apache2-mpm-worker
The following NEW packages will be installed:
apache2-mpm-prefork libapache2-mod-php5 libonig2 libqdbm14 php-pear php5 php5-cli
php5-common php5-suhosin php5-xcache
0 upgraded, 10 newly installed, 1 to remove and 0 not upgraded.
Need to get 7,101 kB of archives.
After this operation, 20.3 MB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://debian.uchicago.edu/debian/ squeeze/main libonig2 i386 5.9.1-1 [134 kB]
Get:2 http://security.debian.org/ squeeze/updates/main apache2-mpm-prefork i386 2.2.16-6+squeeze1 [2,286 B]
Get:3 http://security.debian.org/ squeeze/updates/main php5-common i386 5.3.3-7+squeeze1 [541 kB]
Get:4 http://debian.uchicago.edu/debian/ squeeze/main libqdbm14 i386 1.8.77-4 [147 kB]
Get:5 http://debian.uchicago.edu/debian/ squeeze/main php5-suhosin i386 0.9.32.1-1 [80.8 kB]
Get:6 http://debian.uchicago.edu/debian/ squeeze/main php5-xcache i386 1.3.0-7 [86.4 kB]
Get:7 http://security.debian.org/ squeeze/updates/main libapache2-mod-php5 i386 5.3.3-7+squeeze1 [2,888 kB]
Get:8 http://security.debian.org/ squeeze/updates/main php5-cli i386 5.3.3-7+squeeze1 [2,857 kB]
Get:9 http://security.debian.org/ squeeze/updates/main php-pear all 5.3.3-7+squeeze1 [364 kB]
Get:10 http://security.debian.org/ squeeze/updates/main php5 all 5.3.3-7+squeeze1 [1,068 B]
Fetched 7,101 kB in 15s (445 kB/s)
dpkg: apache2-mpm-worker: dependency problems, but removing anyway as you requested:
apache2 depends on apache2-mpm-worker (= 2.2.16-6+squeeze1) | apache2-mpm-prefork (= 2.2.16-6+squeeze1) | apache2-mpm-event (= 2.2.16-6+squeeze1) | apache2-mpm-itk (= 2.2.16-6+squeeze1); however:
Package apache2-mpm-worker is to be removed.
Package apache2-mpm-prefork is not installed.
Package apache2-mpm-event is not installed.
Package apache2-mpm-itk is not installed.
(Reading database ... 128207 files and directories currently installed.)
Removing apache2-mpm-worker ...
Stopping web server: apache2 ... waiting .
Selecting previously deselected package apache2-mpm-prefork.
(Reading database ... 128203 files and directories currently installed.)
Unpacking apache2-mpm-prefork (from .../apache2-mpm-prefork_2.2.16-6+squeeze1_i386.deb) ...
Setting up apache2-mpm-prefork (2.2.16-6+squeeze1) ...
Starting web server: apache2.
Selecting previously deselected package libonig2.
(Reading database ... 128208 files and directories currently installed.)
Unpacking libonig2 (from .../libonig2_5.9.1-1_i386.deb) ...
Selecting previously deselected package libqdbm14.
Unpacking libqdbm14 (from .../libqdbm14_1.8.77-4_i386.deb) ...
Selecting previously deselected package php5-common.
Unpacking php5-common (from .../php5-common_5.3.3-7+squeeze1_i386.deb) ...
Selecting previously deselected package libapache2-mod-php5.
Unpacking libapache2-mod-php5 (from .../libapache2-mod-php5_5.3.3-7+squeeze1_i386.deb) ...
Selecting previously deselected package php5-cli.
Unpacking php5-cli (from .../php5-cli_5.3.3-7+squeeze1_i386.deb) ...
Selecting previously deselected package php-pear.
Unpacking php-pear (from .../php-pear_5.3.3-7+squeeze1_all.deb) ...
Selecting previously deselected package php5.
Unpacking php5 (from .../php5_5.3.3-7+squeeze1_all.deb) ...
Selecting previously deselected package php5-suhosin.
Unpacking php5-suhosin (from .../php5-suhosin_0.9.32.1-1_i386.deb) ...
Selecting previously deselected package php5-xcache.
Unpacking php5-xcache (from .../php5-xcache_1.3.0-7_i386.deb) ...
Processing triggers for man-db ...
Setting up libonig2 (5.9.1-1) ...
Setting up libqdbm14 (1.8.77-4) ...
Setting up php5-common (5.3.3-7+squeeze1) ...
Setting up libapache2-mod-php5 (5.3.3-7+squeeze1) ...
Creating config file /etc/php5/apache2/php.ini with new version
Reloading web server config: apache2.
Setting up php5-cli (5.3.3-7+squeeze1) ...
Creating config file /etc/php5/cli/php.ini with new version
update-alternatives: using /usr/bin/php5 to provide /usr/bin/php (php) in auto mode.
Setting up php-pear (5.3.3-7+squeeze1) ...
Setting up php5 (5.3.3-7+squeeze1) ...
Setting up php5-suhosin (0.9.32.1-1) ...
Setting up php5-xcache (1.3.0-7) ...
root@dhcp-179:/home/bob# apt-get install php5-suhosin
Reading package lists... Done
Building dependency tree
Reading state information... Done
php5-suhosin is already the newest version.
php5-suhosin set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
root@dhcp-179:/home/bob# apt-get install php5-mysql
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
php5-mysql
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 66.8 kB of archives.
After this operation, 250 kB of additional disk space will be used.
Get:1 http://security.debian.org/ squeeze/updates/main php5-mysql i386 5.3.3-7+squeeze1 [66.8 kB]
Fetched 66.8 kB in 2s (27.0 kB/s)
Selecting previously deselected package php5-mysql.
(Reading database ... 128521 files and directories currently installed.)
Unpacking php5-mysql (from .../php5-mysql_5.3.3-7+squeeze1_i386.deb) ...
Processing triggers for libapache2-mod-php5 ...
Reloading web server config: apache2.
Setting up php5-mysql (5.3.3-7+squeeze1) ...
root@dhcp-179:/home/bob# cd ..
root@dhcp-179:/home# cd /
root@dhcp-179:/# cd /etc/apache2
root@dhcp-179:/etc/apache2# ls
apache2.conf envvars magic mods-enabled sites-available
conf.d httpd.conf mods-available ports.conf sites-enabled
root@dhcp-179:/etc/apache2# cd /var/www
root@dhcp-179:/var/www# ls
index.html
\\
The following program checks the state of the parallel port pin 15 (float switch in the hydroponics solution)and reports it via the Web.
//example.c
//Author: John T. Rine
#include
#include
#include
#include
#define BASEPORT 0x378
int main()
{
if (ioperm(BASEPORT, 3, 1))
{
perror("ioperm");
exit(1);
}
outb(0x00, BASEPORT);
usleep(100000);
char* remoteAddress = getenv("REMOTE_ADDR");
printf("Content-type: text/html\n\n");
printf("");
printf(" Float Switch Status \n");
char dummy;
dummy = 0x08 & inb(BASEPORT + 1);
if (dummy)
{
printf("\n");
printf(" Hello %s \n", remoteAddress);
printf(" The hydroponic solution level is too low!\n ");
}
else
{
printf("\n");
printf(" Hello %s \n", remoteAddress);
printf(" The hydroponic solution level is okay\n ");
}
printf(" \n");
printf(" \n");
if (ioperm(BASEPORT, 3, 0))
{
perror("ioperm");
exit(1);
}
exit(0);
}
Compile the above file.\\
jr018429@dhcp-179:~/Desktop$ gcc example.c -o example
Move the compiled file to the /usr/lib/cgi-bin directory.\\
jr018429@dhcp-179:~/Desktop$ su
Password:
root@dhcp-179:/home/jr018429/Desktop# mv example /usr/lib/cgi-bin
The graphic file floatswitch.jpeg,\\
{{:user:jr018429:floatswitch.jpeg|}}\\
must reside in the /var/www directory.\\
jr018429@dhcp-179:~/Desktop$ su
Password:
root@dhcp-179:/home/jr018429/Desktop# mv floatswitch.jpeg /var/www
In order for the example executable to be executed via an http request, the following commands must be performed:
* chown root.root example
* chmod +s example
root@dhcp-179:/usr/lib/cgi-bin# chown root.root example
root@dhcp-179:/usr/lib/cgi-bin# chmod +s example
The parallel port pinout is as follows:\\
{{:user:jr018429:parallel_port.gif|}}\\
Unidirectional Parallel Port Signals are as follows:\\
^Pin (DB-25)^Signal^Direction^Register/Bit^
|1|STROBE|IN/OUT|CONTROL/0|
|2|DATA0|OUT|DATA/0|
|3|DATA1|OUT|DATA/1|
|4|DATA2|OUT|DATA/2|
|5|DATA3|OUT|DATA/3|
|6|DATA4|OUT|DATA/4|
|7|DATA5|OUT|DATA/5|
|8|DATA6|OUT|DATA/6|
|9|DATA7|OUT|DATA/7|
|10|ACKNOWLEDGE|IN|STATUS/6|
|11|~BUSY|IN|STATUS/7|
|12|PAPER OUT|IN|STATUS/5|
|13|SELECT|IN|STATUS/4|
|14|LINEFEED|IN/OUT|CONTROL/1|
|15|ERROR|IN|STATUS/3|
|16|INIT|IN/OUT|CONTROL/2|
|17|SELECT IN|IN/OUT|CONTROL/3|
|18-25|SIGNAL GROUND|-|-|
\\
Connect the float switch leads from parallel port pin 15 to pin 18.\\
\\
The state of the floatswitch can be accessed by entering the following URL in the URL field of a Web browser: “10.80.2.179/cgi-bin/example”.\\
With parallel port pin 15 open, the hydroponics solution level is too low.\\
{{:user:jr018429:float_switch_status1.png|}}\\
\\
With parallel port pin 15 connected to the parallel port ground, the hydroponics solution level is okay.\\
{{:user:jr018429:float_switch_status3.png|}}\\
======References======
* CGI information: http://hi.baidu.com/someprojects/blog/item/01097e6d0b4df4ff421694c6.html
* CGI Scripts: http://wiki.linuxquestions.org/wiki/Debian:CGI
* Parallel Port information: http://www.labbookpages.co.uk/electronics/parallelPort.html
* More parallel port information: http://www.epanorama.net/circuits/parallel_output.html