1. <?php
  2. /*
  3. Author: Niklas Menke <niklas-menke.de>
  4.  
  5. Project: Power analyser
  6. Description: This project represent electrical parameters from a multifunction power analyser on a website.
  7. The electrical parameters are recorded by a Janitza UMG96S multifunction power analyser.
  8. An Arduino queries the data via MODBUS RTU and sends it to a database.
  9. The website loads the parameters from the database and updates itself automatically via AJAX.
  10.  
  11. File: transmitData.php - PROTECTED
  12. Description: Get data from the database and print it out.
  13. Date: 2020/12/07
  14.  
  15. Version: 1.0.0
  16. Date: first release
  17. */
  18.  
  19.  
  20. $dbh = new PDO('HOST;DBNAME', 'USERNAME', 'PASSWORD');
  21. $type = $_GET['type'];
  22.  
  23. if($type == 'all') {
  24. 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)
  25. 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
  26. echo implode(";",$dbh->query("SELECT L1, L2, L3, total FROM `pa_current` ORDER BY `timestamp` DESC LIMIT 1")->fetch(PDO::FETCH_ASSOC)).";"; // Current
  27. echo implode(";",$dbh->query("SELECT L1, L2, L3, total FROM `pa_activePower` ORDER BY `timestamp` DESC LIMIT 1")->fetch(PDO::FETCH_ASSOC)).";"; // Active power
  28. echo implode(";",$dbh->query("SELECT L1, L2, L3, total FROM `pa_reactivePower` ORDER BY `timestamp` DESC LIMIT 1")->fetch(PDO::FETCH_ASSOC)).";"; // Reactive power
  29. echo implode(";",$dbh->query("SELECT L1, L2, L3, total FROM `pa_apparentPower` ORDER BY `timestamp` DESC LIMIT 1")->fetch(PDO::FETCH_ASSOC)).";"; // Apparent Power
  30. echo implode(";",$dbh->query("SELECT L1, L2, L3, total FROM `pa_phaseDifference` ORDER BY `timestamp` DESC LIMIT 1")->fetch(PDO::FETCH_ASSOC)).";"; // Phase difference
  31. 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)
  32. 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)
  33. echo implode(";",$dbh->query("SELECT L1, sequence FROM `pa_frequency` ORDER BY `timestamp` DESC LIMIT 1")->fetch(PDO::FETCH_ASSOC)).";"; // Frequency and phase sequence
  34. } else {
  35. 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)
  36. 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
  37. else if($type == 'current') $sth = $dbh->prepare("SELECT timestamp, L1, L2, L3, total FROM `pa_current` WHERE `timestamp` > TIMESTAMP(?) AND `timestamp` < TIMESTAMP(?)"); // Current
  38. else if($type == 'activePower') $sth = $dbh->prepare("SELECT timestamp, L1, L2, L3, total FROM `pa_activePower` WHERE `timestamp` > TIMESTAMP(?) AND `timestamp` < TIMESTAMP(?)"); // Active power
  39. else if($type == 'reactivePower') $sth = $dbh->prepare("SELECT timestamp, L1, L2, L3, total FROM `pa_reactivePower` WHERE `timestamp` > TIMESTAMP(?) AND `timestamp` < TIMESTAMP(?)"); // Reactive power
  40. else if($type == 'apparentPower') $sth = $dbh->prepare("SELECT timestamp, L1, L2, L3, total FROM `pa_apparentPower` WHERE `timestamp` > TIMESTAMP(?) AND `timestamp` < TIMESTAMP(?)"); // Apparent Power
  41. else if($type == 'phaseDifference') $sth = $dbh->prepare("SELECT timestamp, L1, L2, L3, total FROM `pa_phaseDifference` WHERE `timestamp` > TIMESTAMP(?) AND `timestamp` < TIMESTAMP(?)"); // Phase difference
  42. 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)
  43. 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)
  44. else if($type == 'frequency') $sth = $dbh->prepare("SELECT timestamp, L1 FROM `pa_frequency` WHERE `timestamp` > TIMESTAMP(?) AND `timestamp` < TIMESTAMP(?)"); // Frequency and phase sequence
  45.  
  46. if($sth) {
  47. $sth->execute([$_GET['begin'],$_GET['end']]);
  48. while($cache = $sth->fetch(PDO::FETCH_ASSOC)) echo implode(";",$cache).";";
  49. }
  50. }
  51.  
  52. ?>