#!/usr/bin/perl
#
# util to fermenting beer at fixed temp
# > ./beer.pl /dev/ttyUSB0
#
# may need to do "sudo apt-get install libdevice-serialport-perl"
use Device::SerialPort;

# get device dir to use
($device, $other) = @ARGV;
if($device eq "") {
  print "usage: beer.pl device ( /dev/tty??? for dac )\n";
  exit(0);
}

# setup facts...
# temp1 is v0 and probe sits at/near bottom of water
# temp2 is v1 and probe is attached to cold heat sink
# temp3 is v2 and probe is attached to hot heat sink
# temp4 is v3 and probe is sitting by the area - room tmep
#
# relay1 is o0 and controls cold/hot polarity - feeds relay2
# relay2 is o1 and controls power to thermal device off/on
# relay3 is o2 and controls heat sink fan off/on
#
# variables - global
# tmp36 sensor, 25c is 0750 or 10mv/1c - offset of 500
# set points of returned values we test for
# adjust values for each sensors calibration....
#
# to calibrate - get all probes at same temp - ideally 20c
# and then put probe values as stemp? and adjust all other
# values up or down from the stemp? values...
#
# set the desired value, target values to reach
# for ale 20c
$stemp1="0691";
$stemp2="0692";
$stemp3="0693";
$stemp4="0687";
# hi value to not exceed...
# 2c above stemp
$htemp1="0693";
$htemp2="0694";
$htemp3="0695";
$htemp4="0689";
# low value not to go below...
# 2c below stemp
$ltemp1="0689";
$ltemp2="0690";
$ltemp3="0691";
$ltemp4="0685";
# values returned from sensors
$vtemp1="";
$vtemp2="";
$vtemp3="";
$vtemp4="";

####### start processing run ###########
# get start time of run
$date = `date +'%Y-%m-%d|%H:%M:%S'`;
chomp $date;
print "$date|";

my $port = Device::SerialPort->new("$device");
$port->user_msg(ON);
$port->databits(8);
$port->baudrate(19200);
$port->parity("none");
$port->stopbits(1);
$port->handshake("none");
$port->write_settings;

# needed to clear buffer
$port->lookclear; 

# do send command - get  digitial in 
$port->write("v\n");
# get results and print it
get_val () ;

print "$vtemp1|$vtemp2|$vtemp3|$vtemp4|";

# check for turning cooling fan on
# is temp3 not same as room temp?
# being on almost always is not a bad thing...
if ( $vtemp3 ne $vtemp4 ) {
       $port->write("o2+\n");
       drop_echo () ;                    
       $relay3 = "fan on "
       } else {
       $port->write("o2-\n");
       drop_echo () ;                    
       $relay3 = "fan off"
       }
print "$relay3|" ;

# check for turning thermal to cold or hot
# is temp higher than setpoint
# relay off is cooling
if ( $vtemp1 ge $stemp1 ) {
       $port->write("o0-\n");
       drop_echo () ;                    
       $relay1 = "cool"
       } else {
       $port->write("o0+\n");
       drop_echo () ;                    
       $relay1 = "heat"
       }
print "$relay1|" ;

# check for turning device on
# is relay 1 heating or cooling?
if ( $relay1 eq "heat"  &&  $vtemp2 ge $htemp2 ) {
       $port->write("o2+\n");
       drop_echo () ;                    
       $relay2 = "TC on "
       } else { 
       $port->write("o2-\n"); 
       drop_echo () ;                    
       $relay2 = "TC off"
       }
if ( $relay1 eq "cool"  &&  $vtemp2 lt $ltemp2 ) {
       $port->write("o2+\n");
       drop_echo () ;                    
       $relay2 = "TC on "
       } else { 
       $port->write("o2-\n"); 
       drop_echo () ;                    
       $relay2 = "TC off"
       }

print "$relay2|" ;

print "\n";
$port->close || die "failed to close";
undef $port;
exit;

###### subs ########
sub get_val {
# should be echo char space value
  while(1) {
    my $byte=$port->read(1);
    if ( $byte eq ">") {
       last;
    }
    if ( $byte eq "\r") {
       $byte="";
    }
    if ( $byte eq "\n") {
       $byte="";
    }
    if ( $byte eq ".") {
       next;
    }
    #print "$byte";
    $value="$value$byte";
  }
  ($echo, $vtemp1, $vtemp2, $vtemp3, $vtemp4, $junk) = split(/ /, $value);
}

sub drop_echo {
# read till > and toss it all...
# should be echo char space value
  while(1) {
    my $byte=$port->read(1);
    if ( $byte eq ">") {
       last;
    }
  }
}



