Get Room Temperature - Perl Script


# 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"<return>, the program will find and install the  module.  Also, "Win32-API" must be installed if it is not already installed: type "install"

#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

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



	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 "$_";