Get Room Temperature - Perl Script



#!c:/perl/bin/perl.exe

# the very first line tells Apache where Perl is, the line must start with "#!"

#the serial port driver is a perl module called "Win32-SerialPort", the module is installed from the command line in the perl/bin directory: type "ppm"<return>, then type "install http://www.bribes.org/perl/ppm/Win32-SerialPort.ppd"<return>, the program will find and install the  module.  Also, "Win32-API" must be installed if it is not already installed: type "install http://www.bribes.org/perl/ppm/Win32-API.ppd"


#open serial port

use Win32::SerialPort;

$Sport = new Win32::SerialPort('COM1');  #this creates an "object", "class" or something, named $Sport

$Sport->baudrate(2400);		#configures serial port
$Sport->parity('none');
$Sport->databits(8);
$Sport->stopbits(1);
$Sport->handshake('none');
$Sport->write_settings;

print "Content-type: text/html\n\n";   #tells browser HTML to follow
#print "foo";


$stoptime = time + 2; #gets computer time (seconds), adds 2 sec (this is the length of time the program is allowed to run)



sub get_temp($)  {		#subroutine "get_temp                      "

	$Sport->write("c=t");	#sends "c=t" to picaxe

	$_='';			#clears $_  (sets to 'nothing')

	do {

sub no_resp  {			#No response subroutine



die;				

}

	if (time >= $stoptime)  {		#if current time greater than stoptime,

	no_resp;				#calls no_resp subroutine above

	}


	$_.= $Sport->read(1);	#reads serial port character
	
	}


	while (!/\r/);    	#until carriage return (\r) received


	return $_;		#returns $_ value

	}

$_= get_temp ("");		#calls the get_temp subroutine above

$_ = ($_ * 5.08);	#This is the voltage scale factor; converts 0-255 to tens of millivolts.
			#The scale factor is determined by multiplying the power supply
			#voltage (as measured with a digital multimeter) on the PICAXE chip by
			#1000/1024.  The power supply voltage in this case measured 5.2 volts. 
			#Thus, 5.2 x 1000/1024 = 5.08. If you don't have a digital multimeter,
			#use 5.0 in the calculation; the scale factor in that case is 4.88

$_ = int($_ + .5);		#rounds to nearest whole number

$_ = ($_ / 10);		#converts tens of millivolts to degrees

$_ = ($_ - 2);		#correction (if necessary)



print "$_";