<?php
/*
Author: Niklas Menke <niklas-menke.de>
Project: Power analyser
Description: This project represent electrical parameters from a multifunction power analyser on a website.
The electrical parameters are recorded by a Janitza UMG96S multifunction power analyser.
An Arduino queries the data via MODBUS RTU and sends it to a database.
The website loads the parameters from the database and updates itself automatically via AJAX.
File: transmitData.php - PROTECTED
Description: Get data from the database and print it out.
Date: 2020/12/07
Version: 1.0.0
Date: first release
*/
$dbh = new PDO('HOST;DBNAME', 'USERNAME', 'PASSWORD');
$type = $_GET['type'];
if($type == 'all') {
echo implode(";",$dbh->query("SELECT timestamp, L1N, L2N, L3N FROM `pa_vln` ORDER BY `timestamp` DESC LIMIT 1")->fetch(PDO::FETCH_ASSOC)).";"; // Line to Neutral voltage (and once the time)
echo implode(";",$dbh->query("SELECT L12, L13, L23 FROM `pa_vll` ORDER BY `timestamp` DESC LIMIT 1")->fetch(PDO::FETCH_ASSOC)).";"; // Line to Line voltage
echo implode(";",$dbh->query("SELECT L1, L2, L3, total FROM `pa_current` ORDER BY `timestamp` DESC LIMIT 1")->fetch(PDO::FETCH_ASSOC)).";"; // Current
echo implode(";",$dbh->query("SELECT L1, L2, L3, total FROM `pa_activePower` ORDER BY `timestamp` DESC LIMIT 1")->fetch(PDO::FETCH_ASSOC)).";"; // Active power
echo implode(";",$dbh->query("SELECT L1, L2, L3, total FROM `pa_reactivePower` ORDER BY `timestamp` DESC LIMIT 1")->fetch(PDO::FETCH_ASSOC)).";"; // Reactive power
echo implode(";",$dbh->query("SELECT L1, L2, L3, total FROM `pa_apparentPower` ORDER BY `timestamp` DESC LIMIT 1")->fetch(PDO::FETCH_ASSOC)).";"; // Apparent Power
echo implode(";",$dbh->query("SELECT L1, L2, L3, total FROM `pa_phaseDifference` ORDER BY `timestamp` DESC LIMIT 1")->fetch(PDO::FETCH_ASSOC)).";"; // Phase difference
echo implode(";",$dbh->query("SELECT L1, L2, L3 FROM `pa_THD_u` ORDER BY `timestamp` DESC LIMIT 1")->fetch(PDO::FETCH_ASSOC)).";"; // Total harmonic voltage distortion (THD U)
echo implode(";",$dbh->query("SELECT L1, L2, L3 FROM `pa_THD_i` ORDER BY `timestamp` DESC LIMIT 1")->fetch(PDO::FETCH_ASSOC)).";"; // Total harmonic current distortion (THD I)
echo implode(";",$dbh->query("SELECT L1, sequence FROM `pa_frequency` ORDER BY `timestamp` DESC LIMIT 1")->fetch(PDO::FETCH_ASSOC)).";"; // Frequency and phase sequence
} else {
if($type == 'vln') $sth = $dbh->prepare("SELECT timestamp, L1N, L2N, L3N FROM `pa_vln` WHERE `timestamp` > TIMESTAMP(?) AND `timestamp` < TIMESTAMP(?)"); // Line to Neutral voltage (and once the time)
else if($type == 'vll') $sth = $dbh->prepare("SELECT timestamp, L12, L13, L23 FROM `pa_vll` WHERE `timestamp` > TIMESTAMP(?) AND `timestamp` < TIMESTAMP(?)"); // Line to Line voltage
else if($type == 'current') $sth = $dbh->prepare("SELECT timestamp, L1, L2, L3, total FROM `pa_current` WHERE `timestamp` > TIMESTAMP(?) AND `timestamp` < TIMESTAMP(?)"); // Current
else if($type == 'activePower') $sth = $dbh->prepare("SELECT timestamp, L1, L2, L3, total FROM `pa_activePower` WHERE `timestamp` > TIMESTAMP(?) AND `timestamp` < TIMESTAMP(?)"); // Active power
else if($type == 'reactivePower') $sth = $dbh->prepare("SELECT timestamp, L1, L2, L3, total FROM `pa_reactivePower` WHERE `timestamp` > TIMESTAMP(?) AND `timestamp` < TIMESTAMP(?)"); // Reactive power
else if($type == 'apparentPower') $sth = $dbh->prepare("SELECT timestamp, L1, L2, L3, total FROM `pa_apparentPower` WHERE `timestamp` > TIMESTAMP(?) AND `timestamp` < TIMESTAMP(?)"); // Apparent Power
else if($type == 'phaseDifference') $sth = $dbh->prepare("SELECT timestamp, L1, L2, L3, total FROM `pa_phaseDifference` WHERE `timestamp` > TIMESTAMP(?) AND `timestamp` < TIMESTAMP(?)"); // Phase difference
else if($type == 'THD_u') $sth = $dbh->prepare("SELECT timestamp, L1, L2, L3 FROM `pa_THD_u` WHERE `timestamp` > TIMESTAMP(?) AND `timestamp` < TIMESTAMP(?)"); // Total harmonic voltage distortion (THD U)
else if($type == 'THD_i') $sth = $dbh->prepare("SELECT timestamp, L1, L2, L3 FROM `pa_THD_i` WHERE `timestamp` > TIMESTAMP(?) AND `timestamp` < TIMESTAMP(?)"); // Total harmonic current distortion (THD I)
else if($type == 'frequency') $sth = $dbh->prepare("SELECT timestamp, L1 FROM `pa_frequency` WHERE `timestamp` > TIMESTAMP(?) AND `timestamp` < TIMESTAMP(?)"); // Frequency and phase sequence
if($sth) {
$sth->execute([$_GET['begin'],$_GET['end']]);
while($cache = $sth->fetch(PDO::FETCH_ASSOC)) echo implode(";",$cache).";";
}
}
?>