<?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: receiveDate.php - PROTECTED
Description: Receive data from the Arduino and save it to database.
Date: 2020/09/07
Version: 1.0.0
Date: first release
*/
set_time_limit(0);
ignore_user_abort(true);
$receivedData = explode(';', $_POST['values']); // Get parameters with the POST method and split the string in an array
// If enough parameters
if(count($receivedData) == 35) {
// Access to database
$dbh = new PDO('HOST;DBNAME', 'USERNAME', 'PASSWORD');
// Line to Neutral voltage
$sth = $dbh->prepare('INSERT INTO pa_vln (timestamp, L1N, L2N, L3N) VALUES (CURRENT_TIMESTAMP, ?, ?, ?)');
$sth->execute([$receivedData[0]/10, $receivedData[1]/10, $receivedData[2]/10]);
// Line to Line voltage
$sth = $dbh->prepare('INSERT INTO pa_vll (timestamp, L12, L13, L23) VALUES (CURRENT_TIMESTAMP, ?, ?, ?)');
$sth->execute([$receivedData[3]/10, $receivedData[4]/10, $receivedData[5]/10]);
// Current
$sth = $dbh->prepare('INSERT INTO pa_current (timestamp, L1, L2, L3, total) VALUES (CURRENT_TIMESTAMP, ?, ?, ?, ?)');
$sth->execute([$receivedData[6]*10, $receivedData[7]*10, $receivedData[8]*10, $receivedData[30]*10]);
// Active power
$sth = $dbh->prepare('INSERT INTO pa_activePower (timestamp, L1, L2, L3, total) VALUES (CURRENT_TIMESTAMP, ?, ?, ?, ?)');
$sth->execute([$receivedData[9], $receivedData[10], $receivedData[11], $receivedData[31]*10]);
// Reactive power
for($i = 12; $i < 15; $i++) if($receivedData[$i] > 32767) $receivedData[$i] -= 65535;
if($receivedData[32] > 32767) $receivedData[32] -= 65535;
$sth = $dbh->prepare('INSERT INTO pa_reactivePower (timestamp, L1, L2, L3, total) VALUES (CURRENT_TIMESTAMP, ?, ?, ?, ?)');
$sth->execute([$receivedData[12], $receivedData[13], $receivedData[14], $receivedData[32]*10]);
// Apparent power
$sth = $dbh->prepare('INSERT INTO pa_apparentPower (timestamp, L1, L2, L3, total) VALUES (CURRENT_TIMESTAMP, ?, ?, ?, ?)');
$sth->execute([$receivedData[15], $receivedData[16], $receivedData[17], $receivedData[33]*10]);
// Phase difference
for($i = 18; $i < 21; $i++) if($receivedData[$i] > 32767) $receivedData[$i] -= 65535;
if($receivedData[28] > 32767) $receivedData[28] -= 65535;
$sth = $dbh->prepare('INSERT INTO pa_phaseDifference (timestamp, L1, L2, L3, total) VALUES (CURRENT_TIMESTAMP, ?, ?, ?, ?)');
$sth->execute([$receivedData[18]/100, $receivedData[19]/100, $receivedData[20]/100, $receivedData[28]/100]);
// Total Harmonic Distortion (voltage)
$sth = $dbh->prepare('INSERT INTO pa_THD_u (timestamp, L1, L2, L3) VALUES (CURRENT_TIMESTAMP, ?, ?, ?)');
$sth->execute([$receivedData[21]/10, $receivedData[22]/10, $receivedData[23]/10]);
// Total Harmonic Distortion (current)
$sth = $dbh->prepare('INSERT INTO pa_THD_i (timestamp, L1, L2, L3) VALUES (CURRENT_TIMESTAMP, ?, ?, ?)');
$sth->execute([$receivedData[24]/10, $receivedData[25]/10, $receivedData[26]/10]);
// Frequency and phase sequence
$sth = $dbh->prepare('INSERT INTO pa_frequency (timestamp, L1, sequence) VALUES (CURRENT_TIMESTAMP, ?, ?)');
$sth->execute([$receivedData[27]/100, $receivedData[29]]);
$sth = null;
$dbh = null;
}
?>