-
Notifications
You must be signed in to change notification settings - Fork 3
/
TiltPythonMySQLcrontab.php
80 lines (68 loc) · 2.1 KB
/
TiltPythonMySQLcrontab.php
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
76
77
78
79
<?php
//Reduce errors
error_reporting(~E_WARNING);
$tiltStr = "XX:XX:XX:XX:XX:XX"; // put your tilt MAC address here
$htmlAddrStr = "/var/www/html";
$escapShellCmdStr = "sudo python $htmlAddrStr/tiltblescan.py";
// Connect to the database
$servername = "localhost";
$username = "tilt";
$password = "tilt";
$database = "tilt";
$link = mysqli_connect($servername,$username,$password,$database);
if (!$link)
die("Connection failed: " . mysqli_connect_error());
$sql = "select abv from device";
$rowVal = mysqli_query($link, $sql);
echo "rows = ",mysqli_num_rows($rowVal)."\n";
if(mysqli_num_rows($rowVal) > 0)
{
$firstRow = FALSE;
$sql = "select sg from device order by time asc limit 1;";
$rowVal = mysqli_query($link, $sql);
$row = mysqli_fetch_assoc($rowVal);
$ogStr = (float) $row["sg"];
echo "\$ogStr = $ogStr\n";
}else{
$firstRow = TRUE;
}
$bmFile = fopen("/var/log/tilt.log", "a+") or die("Unable to open file!");
$tilt = "/$tiltStr/i";
//echo "\$tiltStr = $tiltStr, \$tilt = $tilt\n";
$command = escapeshellcmd($escapShellCmdStr);
$output = shell_exec($command);
$tiltArray = explode("\n", $output);
foreach($tiltArray as $value)
{
if(preg_match($tilt, $value))
{
$result = explode(",", $value);
break;
}
}
// echo "$result[2],$result[3]\n";
$now = time();
$sgStr = (float) ($result[3] / 1000);
$tempStr = $result[2];
$dateStr = date("Y-m-d H:i:s", $now);
if($firstRow == TRUE)
{
$abvStr = 0.0;
}else{
$abvStr = (float) (((float) $ogStr - (float) $sgStr) * (float) 131.25);
}
$abvStr = round($abvStr, 2);
echo "\$sgStr = $sgStr, \$tempStr = $tempStr, \$abvStr=$abvStr \n";
$bmStr = "name - Red, MAC = $tiltStr, Time - $now - $dateStr, SG - $sgStr, Temp - $tempStr, degrees, ABV - $abvStr percent\n";
fwrite ($bmFile, $bmStr);
if($tempStr > 0)
{
$sql= "INSERT INTO `device`(`name`,`mac`,`time`, `sg`, `temp`, `abv`) VALUES('Red','$tiltStr','$now','$sgStr','$tempStr','$abvStr')";
if (!mysqli_query($link, $sql))
{
$bmErrStr= "Error: " . $sql . "<br>" . mysqli_error($link);
fwrite ($bmFile, $bmErrStr);
}
}
fclose($bmFile);
?>