#!/usr/bin/perl # use Device::SerialPort 0.12; use Switch; $LOGDIR = "/home/alex/furnace"; # path to data file $LOGFILE = "furnace"; # file name to output to $PORT = "/dev/ttyS0"; # port to watch # Serial Settings my $ob = Device::SerialPort->new ($PORT) || die "Can't Open $PORT: $!"; $ob->baudrate(2400) || die "failed setting baudrate"; $ob->parity("none") || die "failed setting parity"; $ob->databits(8) || die "failed setting databits"; $ob->handshake("none") || die "failed setting handshake"; $ob->write_settings || die "no settings"; #test line #open log file $s_logdate = "000000"; &CreateLogFile; # $f_tmr = 0; $f_state = 0; $f_start = time; $ac_state = 0; $ac_start = time; while(1) { # send 'A', wait for status # print "Send A\n"; $ob->write("A"); # print "Wait for character\n"; my $char = $ob->read(2); # print $char; # print "\n"; if ($char) { if ($char == "3") { if ($f_state == 0) { $f_state = 1; $f_start = &PrintTimeStamp; print logfile ", Heating turned on., 0\n"; } } if ($char == "5") { if ($ac_state == 0) { $ac_state = 1; $ac_start = &PrintTimeStamp; print logfile ", Cooling turned on. , 0\n"; } } if ($char == "1") { if ($f_state == 1) { $f_state = 0; $f_end = &PrintTimeStamp - $f_start; print logfile ", Heating turned off., " . $f_end . "\n"; } if ($ac_state == 1) { $ac_state = 0; $ac_end = &PrintTimeStamp - $ac_start; print logfile ", Cooling turned off., " . $ac_end . "\n"; } } } else { &PrintTimeStamp; #print ", char was null, 0\n"; print logfile ", [".$char."] char was null, 0\n"; } sleep(2); # sleep for 2 second (1 second is not enough for stamp) &CreateLogFile; } # we never get here, but for completeness close the file handle close (logfile); #subroutine to obtain, print and return a timestamp. sub PrintTimeStamp { $l_time = time; printf logfile $l_time . ", "; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($l_time); printf logfile "%4d-%02d-%02d %02d:%02d:%02d", $year+1900,$mon+1,$mday,$hour,$min,$sec; return $l_time; } #create new log file sub CreateLogFile { ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); $s_newdate = sprintf "%02d%02d%02d", $mon+1, $mday, $year-100; # at midnight close current file and open new file. # before closing check to see if furnace / ac is running at midnight # if so, 'turn it off', close log file, create new, 'turn it on' # probably not ideal, but should work. if ($s_logdate != $s_newdate) { if ($f_state == 1) { $clf_ts = &PrintTimeStamp; $f_end = $clf_ts - $f_start; print logfile ", Heating turned off(EOF)., " . $f_end . "\n"; $f_start = $clf_ts; } if ($ac_state == 1) { $clf_ts = &PrintTimeStamp; $ac_end = $clf_ts - $ac_start; print logfile ", Cooling turned off(EOF)., " . $ac_end . "\n"; $ac_start = $clf_ts; } $s_logdate = $s_newdate; close (logfile); open (logfile, '>>'.$LOGDIR."/".$LOGFILE.$s_logdate.".log"); $devnulltime = &PrintTimeStamp; print logfile ", FurnaceLog Version 2.1 fs=[$f_state] ac=[$ac_state], 0\n"; if ($f_state == 1) { $f_start = &PrintTimeStamp; print logfile ", Heating turned on (SOF)., 0\n"; } if ($ac_state == 1) { $ac_start = &PrintTimeStamp; print logfile ", Cooling turned on (SOF)., 0\n"; } #make file handle HOT to prevent buffering select((select(logfile), $|=1)[0]); } }