Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $solartracker = "/home/pi/Documents/Projects/ArduinoSerialImport/solartracker.db";
- $dsn = "sqlite:$solartracker";
- try {
- $db = new PDO($dsn);
- $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- } catch (PDOException $error) {
- echo "Failed to connect to the database using DSN:<br>$dsn<br>";
- throw $error;
- }
- $stmt = $db->prepare("SELECT voltage, current, datestamp FROM stuffToPlot");
- $stmt->execute();
- $rows = array();
- $table = array();
- $table['cols'] = array(
- array('label' => 'Time', 'type' => 'date'),
- array('label' => 'Power', 'type' => 'number')
- );
- while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
- $voltage[] = $row['voltage'];
- $current[] = $row['current'];
- $voltage = array();
- $current = array();
- $date1 = new DateTime();
- $date2 = "Date(".date_format($date1, 'Y').", ".((int) date_format($date1, 'm') - 1).", ".date_format($date1, 'd').", ".date_format($date1, 'H').", ".date_format($date1, 'i').", ".date_format($date1, 's').")";
- $power = array();
- foreach ($voltage as $key => $door){
- $power[] = $door * $current[$key];}
- $temp = array();
- $temp[] = array('v' => (float) $power);
- $temp[] = array ('v' => (string) $date2);
- $rows[] = array ('c' => $temp);
- }
- $table['rows'] = $rows;
- echo json_encode($table);
- $fp = fopen('data.json', 'w');
- fwrite($fp, json_encode(array('cols' =>$table)));
- fclose($fp);
- ?>
Add Comment
Please, Sign In to add comment