#!/usr/bin/perl use Device::SerialPort; use DBI; # MySQL CONFIG VARIABLES $host = "hostname"; $database = "database"; $tablename = "pic"; $user = "username"; $pw = "password"; #Filename for when MySQL server is offline $file="pcontroller_cache.dat"; # Set up the serial port # 19200, 81N on the USB ftdi driver my $port = Device::SerialPort->new("/dev/ttyUSB0"); $port->databits(8); $port->baudrate(19200); $port->parity("none"); $port->stopbits(1); $port->read(255); $port->write("\$S1,Q#"); my $notFndCnt = 0; $data=""; while($data !~ /\$C0.*\#/) { $data.=$port->read(255); } $port->close(); @values=split(/,/,$data); $hex=$values[3]; $Temperature=hex($hex)/128; #if(!$hex) { #die(); #} print "$Temperature\n"; my ($s,$m,$h,$d,$mon,$yr,$wday,$yday,$isdst) = localtime time; if($dbh = DBI->connect('DBI:mysql:'.$database.":$host",$user,$pw)) { $sql = "INSERT INTO `1wire`.`$tablename` (`dt`, `hex`, `temp_c`, `misc`) VALUES (CURRENT_TIMESTAMP, \'$hex\', \'$Temperature\', NULL);"; $c = $dbh->do($sql); if(!$quiet) {print "Affected $c rows for table $tablename\n";} $dbh->disconnect(); } else { if(!$quiet) {print "Could not connect to database: $DBI::errstr";} $c=0; } if(!$c) { if(!$quiet) {print "Failure, writing to $file\n";} $stamp = sprintf("%4d-%02d-%02d %02d:%02d:%02d", $yr+1900, $mon+1, $d, $h, $m, $s); open(FH,">>$file") || die("Cannot open $file."); print FH "$tablename,$stamp,$hex,$Temperature,0\n"; close(FH); }