156 lines
4.5 KiB
PHP
Executable File
156 lines
4.5 KiB
PHP
Executable File
<?php
|
|
|
|
/*
|
|
Rui Santos
|
|
Complete project details at https://RandomNerdTutorials.com/esp32-esp8266-mysql-database-php/
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files.
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
*/
|
|
|
|
$servername = "localhost";
|
|
|
|
// REPLACE with your Database name
|
|
$dbname = "sensors";
|
|
// REPLACE with Database user
|
|
$username = "sensors";
|
|
// REPLACE with Database user password
|
|
$password = "datel123";
|
|
|
|
// Keep this API Key value to be compatible with the ESP32 code provided in the project page.
|
|
// If you change this value, the ESP32 sketch needs to match
|
|
$api_key_value = "tPmAT5Ab3j7F9";
|
|
|
|
$conn = new mysqli($servername, $username, $password, $dbname);
|
|
if (isset($_GET['api_key'])) {
|
|
|
|
|
|
|
|
|
|
$api_key= $sensor = $location = $value1 = $value2 = $value3 = "";
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "GET") {
|
|
$api_key = test_input($_GET["api_key"]);
|
|
if($api_key == $api_key_value) {
|
|
$sensor = test_input($_GET["sensor"]);
|
|
$location = test_input($_GET["location"]);
|
|
$value1 = test_input($_GET["value1"]);
|
|
$value2 = test_input($_GET["value2"]);
|
|
$value3 = test_input($_GET["value3"]);
|
|
|
|
if ($conn->connect_error) {
|
|
die("Connection failed: " . $conn->connect_error);
|
|
}
|
|
|
|
$sqlnew = "select location as light from SensorData where sensor like '$sensor' ORDER BY id DESC LIMIT 1";
|
|
if ($result = $conn->query($sqlnew)) {
|
|
while ($row = $result->fetch_assoc()) {
|
|
$row_light = $row["light"];
|
|
}
|
|
$result->free();
|
|
}
|
|
|
|
|
|
|
|
$percent = (1 - $location / $row_light) * 100;
|
|
|
|
//$percent = 0 - $percent;
|
|
|
|
$anone = number_format($percent, 0);
|
|
$anone = 0 - $anone;
|
|
// echo $location."<br>";
|
|
// echo $row_light."<br>";
|
|
// echo $anone;
|
|
|
|
|
|
if($anone >=80 ){
|
|
$location = $row_light;
|
|
}
|
|
|
|
//echo "konec:".$location;
|
|
|
|
$sql = "INSERT INTO SensorData (sensor, location, value1, value2, value3)
|
|
VALUES ('" . $sensor . "', '" . $location . "', '" . $value1 . "', '" . $value2 . "', '" . $value3 . "')";
|
|
|
|
if ($conn->query($sql) === TRUE) {
|
|
echo "New record created successfully";
|
|
}
|
|
else {
|
|
echo "Error: " . $sql . "<br>" . $conn->error;
|
|
}
|
|
|
|
$conn->close();
|
|
}
|
|
else {
|
|
echo "Wrong API Key provided.";
|
|
}
|
|
|
|
}
|
|
else {
|
|
echo "No data posted with HTTP POST.";
|
|
}
|
|
}
|
|
|
|
else {
|
|
|
|
$sql = "SELECT id, sensor, location, value1, value2, value3, reading_time FROM SensorData order by reading_time desc limit 10";
|
|
|
|
echo '<table cellspacing="0" cellpadding="5" border="0" align="center">
|
|
<tr>
|
|
<td>Kde</td>
|
|
<td>Svetlo</td>
|
|
<td>Vlhkost</td>
|
|
<td>Teplota</td>
|
|
<td>Tlak</td>
|
|
<td>Timestamp</td>
|
|
</tr>';
|
|
|
|
if ($result = $conn->query($sql)) {
|
|
while ($row = $result->fetch_assoc()) {
|
|
// $row_id = $row["id"];
|
|
$row_sensor = $row["sensor"];
|
|
$row_location = $row["location"];
|
|
$row_value1 = $row["value1"];
|
|
$row_value2 = $row["value2"];
|
|
$row_value3 = $row["value3"];
|
|
$row_reading_time = $row["reading_time"];
|
|
//$row_reading_time = date("Y-m-d H:i:s", strtotime("$row_reading_time + 4 hours"));
|
|
|
|
if($row_sensor == "Byt"){
|
|
echo '<tr bgcolor="red">
|
|
<td>' . $row_sensor . '</td>
|
|
<td>' . $row_location . '</td>
|
|
<td>' . $row_value1 . '</td>
|
|
<td>' . $row_value2 . '</td>
|
|
<td>' . $row_value3 . '</td>
|
|
<td>' . date("l dS \o\f F Y h:i:s A",strtotime("$row_reading_time")) . '</td>
|
|
</tr>';
|
|
} else {
|
|
echo '<tr>
|
|
<td>' . $row_sensor . '</td>
|
|
<td>' . $row_location . '</td>
|
|
<td>' . $row_value1 . '</td>
|
|
<td>' . $row_value2 . '</td>
|
|
<td>' . $row_value3 . '</td>
|
|
<td>' . date("l dS \o\f F Y h:i:s A",strtotime("$row_reading_time")) . '</td>
|
|
</tr>';
|
|
}
|
|
}
|
|
$result->free();
|
|
}
|
|
|
|
$conn->close();
|
|
}
|
|
|
|
|
|
|
|
function test_input($data) {
|
|
$data = trim($data);
|
|
$data = stripslashes($data);
|
|
$data = htmlspecialchars($data);
|
|
return $data;
|
|
}
|