Store Temperature Data

<?php

  class Serial
  {
    private $fd;

    public function open( $port )
    {
      exec( 'mode ' . $port . ' baud=115200 data=8 stop=1 parity=n' );
      $this->fd = dio_open( $port, O_RDWR );
      return $this->fd;
    }

    public function close()
    {
      dio_close( $this->fd );
    }

    public function readline()
    {
      if( $this->fd )
      {
        $line = "";
        while( ($ch = dio_read( $this->fd, 1 )) != "\n" )
        {
          if( $ch != "\r" )
            $line .= $ch;
        }
        return $line;
      }
    }
  }

  class Temperature_Database
  {
    public $stmt;
    public $mysqli;

    public function __construct()
    {
      global $mean;
      global $std_dev;

      $db_name = "temperature";

      //
      // This program can be called with optional arguments:
      //   --database=foo         the name of the database to use
      //   --new                  delete the old database first
      //

      $options = getopt( "", [ "database::", "new::" ] );

      $drop_it = FALSE;
      foreach( array_keys( $options ) as $key )
      {
        if( $key == "new" )
          $drop_it = TRUE;
        if( $key == "database" )
        {
          $db_name = $options[$key];
          print( "Using database $db_name\n" );
        }
      }

      //
      // Connect to the MySql server on localhost
      //
      $this->mysqli = new mysqli( 'localhost', 'root', '' )
        or die( 'Could not connect: ' . mysqli_error() );

      //
      // Optionally start a fresh database by dropping the old one
      //
      if( $drop_it )
      {
        print( "Droping database $db_name\n" );
        $this->mysqli->query( 'DROP DATABASE IF EXISTS ' . $db_name )
          or die( "Could not drop database $db_name" );
      }
        
      //
      // Create the database if it does not exist
      //
      $this->mysqli->query( 'CREATE DATABASE IF NOT EXISTS ' . $db_name )
        or die( "Could not create database $db_name" );

      //
      // Select the database
      //
      $this->mysqli->select_db( $db_name )
          or die( "Could not select database $db_name" );

      // $this->mysqli->query("DROP TABLE IF EXISTS temps");

      //
      // Create a table of data if this is a new database
      //
      $create_table = "CREATE TABLE IF NOT EXISTS temps ("
        . " id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,"
        . " mean DOUBLE NOT NULL,"
        . " std_dev DOUBLE NOT NULL,"
        . " reg_date TIMESTAMP )";

      $this->mysqli->query( $create_table )
        or die( 'Create table failed: ' . $this->mysqli->error );

      //
      // Prepare our insert query
      //
      $this->stmt = $this->mysqli->prepare(
        "INSERT INTO temps( mean, std_dev ) VALUES( ?, ? )" )
          or die( 'Prepare failed: ' . $this->mysqli->error );

      //
      // And bind it
      //
      $this->stmt->bind_param( "dd", $mean, $std_dev )
        or die( 'Bind_param failed: ' . $this->mysqli->error );
        
    }

    public function save_temp()
    {
      //
      // This will execute our previously prepared and bound insert query
      //
      $this->stmt->execute()
        or die( 'Insert failed: ' . $this->mysqli->error );
    }
  }

  $serial = new Serial();
  $db = new Temperature_Database();

  if( $serial->open( "com3:" ) )
  {
    for( ;; )
    {
      $line = $serial->readline();
      $pieces = explode( ",", $line );
      $mean = $pieces[0];
      $std_dev = $pieces[1];
      print( "Mean: " . $mean . ", Standard Deviation: " . $std_dev . PHP_EOL );
      $db->save_temp();
    }
    $serial->close();
  } 
  else
    print( "Can't open com3:" );
?>