-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathget_sensor.php
More file actions
34 lines (29 loc) · 1.27 KB
/
Copy pathget_sensor.php
File metadata and controls
34 lines (29 loc) · 1.27 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
<?php
// ═══════════════════════════════════════════════════════════
// get_sensor.php — returns latest sensor data including gas breakdown
// ═══════════════════════════════════════════════════════════
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json");
$conn = new mysqli("localhost", "root", "", "aqi_sensor");
if ($conn->connect_error) {
echo json_encode(["error" => $conn->connect_error]);
exit;
}
// Latest live reading including gas breakdown
$live = $conn->query(
"SELECT mq135, sound, local_aqi, co2_pct, nh3_pct, nox_pct, smoke_pct, recorded_at
FROM sensor_live
ORDER BY recorded_at DESC LIMIT 1"
)->fetch_assoc();
// Last 7 daily summaries
$rows = $conn->query(
"SELECT date, mq135_avg, mq135_min, mq135_max,
sound_avg, sound_min, sound_max, local_aqi
FROM sensor_daily ORDER BY date DESC LIMIT 7"
);
$daily = [];
while ($r = $rows->fetch_assoc()) $daily[] = $r;
$daily = array_reverse($daily);
echo json_encode(["live" => $live, "daily" => $daily]);
$conn->close();
?>