-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrcvsoiltempdata.php
More file actions
75 lines (65 loc) · 2.44 KB
/
rcvsoiltempdata.php
File metadata and controls
75 lines (65 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
// rev2 change lastdata to temperature; add soil moisture
// rev3 add initial soil percent calculation
// rev5 add fail count
$servername = "localhost";
$username = "yourbitwebname";
$password = "yourpwd";
$dbname = "soildata";
$tablename = "SoilDataTemp";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connect failed: " . $conn->error);
}
$sql = "SELECT * FROM " . "SoilDataTemp";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// only row 0 is updated
$row = $result->fetch_assoc();
$moisture_min = $row["moisture_min"];
$moisture_max = $row["moisture_max"];
}
// Force timestamp update by writing -55.0 invalid/different first
$sql = "UPDATE " . $tablename . " SET temperature='-55.0' WHERE id=0";
if ($conn->query($sql) === TRUE) {
// echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$soiltempf = $_POST['temperature'] * 9 / 5 + 32; // Farenheight
$soiltempf = number_format((float)$soiltempf, 1, '.', ''); // limit resolution
$moisture = $_POST['moisture'];
$battvoltage = $_POST['batt'];
$failcnt = $_POST['fail'];
if ($moisture < $moisture_min) {
$moisture_min = $moisture;
} else if ($moisture > $moisture_max) {
$moisture_max = $moisture;
}
$moisture_percent = 100.0 - (100 * ($moisture - $moisture_min)/($moisture_max - $moisture_min));
$moisture_percent = number_format($moisture_percent, 1, '.', ''); // limit to one decimal point
$sql = "INSERT INTO SoilDataLog " .
"(temperature, moisture, battvolt, moisture_min, moisture_max, moisture_percent, failcnt) VALUES (" .
$soiltempf . ", " . $moisture . ", " . $battvoltage . ", " . $moisture_min . ", " . $moisture_max . ", " .
$moisture_percent . ", " . $failcnt . ")";
if ($conn->query($sql) === TRUE) {
// echo "update<br>";
// echo "success<br>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$sql = "UPDATE " . $tablename .
" SET temperature='" . $soiltempf . "', moisture='" . $moisture . "', battvolt='" . $battvoltage .
"', moisture_min='" . $moisture_min . "', moisture_max='" . $moisture_max .
"', moisture_percent='" . $moisture_percent . "', failcnt='" . $failcnt .
"' WHERE id=0";
if ($conn->query($sql) === TRUE) {
// echo "update<br>";
echo "success<br>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>