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: receiveDate.php - PROTECTED
  12. Description: Receive data from the Arduino and save it to database.
  13. Date: 2020/09/07
  14.  
  15. Version: 1.0.0
  16. Date: first release
  17. */
  18.  
  19. set_time_limit(0);
  20. ignore_user_abort(true);
  21.  
  22. $receivedData = explode(';', $_POST['values']); // Get parameters with the POST method and split the string in an array
  23.  
  24. // If enough parameters
  25. if(count($receivedData) == 35) {
  26.  
  27. // Access to database
  28. $dbh = new PDO('HOST;DBNAME', 'USERNAME', 'PASSWORD');
  29.  
  30. // Line to Neutral voltage
  31. $sth = $dbh->prepare('INSERT INTO pa_vln (timestamp, L1N, L2N, L3N) VALUES (CURRENT_TIMESTAMP, ?, ?, ?)');
  32. $sth->execute([$receivedData[0]/10, $receivedData[1]/10, $receivedData[2]/10]);
  33.  
  34. // Line to Line voltage
  35. $sth = $dbh->prepare('INSERT INTO pa_vll (timestamp, L12, L13, L23) VALUES (CURRENT_TIMESTAMP, ?, ?, ?)');
  36. $sth->execute([$receivedData[3]/10, $receivedData[4]/10, $receivedData[5]/10]);
  37.  
  38. // Current
  39. $sth = $dbh->prepare('INSERT INTO pa_current (timestamp, L1, L2, L3, total) VALUES (CURRENT_TIMESTAMP, ?, ?, ?, ?)');
  40. $sth->execute([$receivedData[6]*10, $receivedData[7]*10, $receivedData[8]*10, $receivedData[30]*10]);
  41.  
  42. // Active power
  43. $sth = $dbh->prepare('INSERT INTO pa_activePower (timestamp, L1, L2, L3, total) VALUES (CURRENT_TIMESTAMP, ?, ?, ?, ?)');
  44. $sth->execute([$receivedData[9], $receivedData[10], $receivedData[11], $receivedData[31]*10]);
  45.  
  46. // Reactive power
  47. for($i = 12; $i < 15; $i++) if($receivedData[$i] > 32767) $receivedData[$i] -= 65535;
  48. if($receivedData[32] > 32767) $receivedData[32] -= 65535;
  49. $sth = $dbh->prepare('INSERT INTO pa_reactivePower (timestamp, L1, L2, L3, total) VALUES (CURRENT_TIMESTAMP, ?, ?, ?, ?)');
  50. $sth->execute([$receivedData[12], $receivedData[13], $receivedData[14], $receivedData[32]*10]);
  51.  
  52. // Apparent power
  53. $sth = $dbh->prepare('INSERT INTO pa_apparentPower (timestamp, L1, L2, L3, total) VALUES (CURRENT_TIMESTAMP, ?, ?, ?, ?)');
  54. $sth->execute([$receivedData[15], $receivedData[16], $receivedData[17], $receivedData[33]*10]);
  55.  
  56. // Phase difference
  57. for($i = 18; $i < 21; $i++) if($receivedData[$i] > 32767) $receivedData[$i] -= 65535;
  58. if($receivedData[28] > 32767) $receivedData[28] -= 65535;
  59. $sth = $dbh->prepare('INSERT INTO pa_phaseDifference (timestamp, L1, L2, L3, total) VALUES (CURRENT_TIMESTAMP, ?, ?, ?, ?)');
  60. $sth->execute([$receivedData[18]/100, $receivedData[19]/100, $receivedData[20]/100, $receivedData[28]/100]);
  61.  
  62. // Total Harmonic Distortion (voltage)
  63. $sth = $dbh->prepare('INSERT INTO pa_THD_u (timestamp, L1, L2, L3) VALUES (CURRENT_TIMESTAMP, ?, ?, ?)');
  64. $sth->execute([$receivedData[21]/10, $receivedData[22]/10, $receivedData[23]/10]);
  65.  
  66. // Total Harmonic Distortion (current)
  67. $sth = $dbh->prepare('INSERT INTO pa_THD_i (timestamp, L1, L2, L3) VALUES (CURRENT_TIMESTAMP, ?, ?, ?)');
  68. $sth->execute([$receivedData[24]/10, $receivedData[25]/10, $receivedData[26]/10]);
  69.  
  70. // Frequency and phase sequence
  71. $sth = $dbh->prepare('INSERT INTO pa_frequency (timestamp, L1, sequence) VALUES (CURRENT_TIMESTAMP, ?, ?)');
  72. $sth->execute([$receivedData[27]/100, $receivedData[29]]);
  73.  
  74. $sth = null;
  75. $dbh = null;
  76. }
  77. ?>