IoT Energy Monitor — ส่งและรับข้อมูล sensor ผ่าน HTTP
ใช้สำหรับหน้า Dashboard — เรียกผ่าน browser หรือ JavaScript fetch
# ดึงข้อมูล meta (ห้อง + ช่วงวันที่) curl -s "http://YOUR_HOST/iot-dashboard/api.php?action=meta" | python -m json.tool
{
"rooms": ["ห้องของฉัน"],
"min": "2022-08-05 01:37:53",
"max": "2022-08-09 18:04:53"
}
| พารามิเตอร์ | ประเภท | จำเป็น | คำอธิบาย |
|---|---|---|---|
room | int | จำเป็น | ห้องของฉัน |
from | string | ไม่บังคับ | วันที่เริ่มต้น YYYY-MM-DD |
to | string | ไม่บังคับ | วันที่สิ้นสุด YYYY-MM-DD |
# ดึงสรุปข้อมูล ห้องของฉัน curl -s "http://YOUR_HOST/iot-dashboard/api.php?action=summary&room=ห้องของฉัน&from=2022-08-05&to=2022-08-09" | python -m json.tool
{
"last_ddate": "2022-08-09 18:04:53",
"last_temp2": 27.8,
"last_humi1": 68.6,
"last_humi2": 62.2,
"record_count": 1000,
...
}
| พารามิเตอร์ | ประเภท | จำเป็น | คำอธิบาย |
|---|---|---|---|
room | int | จำเป็น | ห้องของฉัน |
from | string | จำเป็น | วันที่เริ่มต้น YYYY-MM-DD |
to | string | จำเป็น | วันที่สิ้นสุด YYYY-MM-DD |
group | string | ไม่บังคับ | hour (default) | day | raw |
# ดึงข้อมูลรายชั่วโมง curl -s "http://YOUR_HOST/iot-dashboard/api.php?action=timeseries&room=ห้องของฉัน&from=2022-08-05&to=2022-08-09&group=hour" | python -m json.tool # ดึงข้อมูลรายวัน curl -s "http://YOUR_HOST/iot-dashboard/api.php?action=timeseries&room=ห้องของฉัน&from=2022-08-05&to=2022-08-09&group=day" # ดึงข้อมูลทุกจุด (raw) curl -s "http://YOUR_HOST/iot-dashboard/api.php?action=timeseries&room=ห้องของฉัน&from=2022-08-05&to=2022-08-09&group=raw"
{
"labels": ["2022-08-05 01:00", "2022-08-05 02:00", ...],
"series": {
"Temp2": [28.28, 28.25, ...],
"Humi1": [68.62, 69.48, ...],
"Humi2": [62.10, 61.85, ...],
...
}
}
| พารามิเตอร์ | ประเภท | จำเป็น | คำอธิบาย |
|---|---|---|---|
room | int | จำเป็น | ห้องของฉัน |
from | string | จำเป็น | วันที่เริ่มต้น |
to | string | จำเป็น | วันที่สิ้นสุด |
page | int | ไม่บังคับ | หน้า (default: 1) |
per_page | int | ไม่บังคับ | จำนวนแถวต่อหน้า (default: 25, max: 200) |
search | string | ไม่บังคับ | คำค้นหา |
# ดึงข้อมูลหน้า 1 จำนวน 10 แถว curl -s "http://YOUR_HOST/iot-dashboard/api.php?action=data&room=ห้องของฉัน&from=2022-08-05&to=2022-08-09&page=1&per_page=10" | python -m json.tool # ค้นหาข้อมูล curl -s "http://YOUR_HOST/iot-dashboard/api.php?action=data&room=ห้องของฉัน&from=2022-08-05&to=2022-08-09&search=29"
curl -s "http://YOUR_HOST/iot-dashboard/api.php?action=daily&room=ห้องของฉัน&from=2022-08-05&to=2022-08-09" | python -m json.tool
| พารามิเตอร์ | ประเภท | จำเป็น | คำอธิบาย |
|---|---|---|---|
room | int | จำเป็น | ห้องของฉัน |
from | string | จำเป็น | วันที่เริ่มต้น |
to | string | จำเป็น | วันที่สิ้นสุด |
search | string | ไม่บังคับ | กรองด้วยคำค้นหา |
| คอลัมน์ | คำอธิบาย |
|---|---|
ID | หมายเลขเรคคอร์ด |
วันที่/เวลา | เวลาที่เซนเซอร์ส่งข้อมูล |
ห้องของฉัน | ชื่อห้อง (แสดงเป็น "ห้องของฉัน" สำหรับห้องหลัก) |
อุณหภูมิ (°C) | อุณหภูมิอากาศจาก DHT22 |
ความชื้นดิน (%) | ความชื้นในดินจาก capacitive sensor |
ความชื้นอากาศ (%) | ความชื้นสัมพัทธ์ของอากาศ |
# ดาวน์โหลดไฟล์ CSV curl -s -o data.csv "http://YOUR_HOST/iot-dashboard/api.php?action=export&room=ห้องของฉัน&from=2022-08-05&to=2022-08-09" # ดาวน์โหลดพร้อมค้นหา curl -s -o filtered.csv "http://YOUR_HOST/iot-dashboard/api.php?action=export&room=ห้องของฉัน&from=2022-08-05&to=2022-08-09&search=29"
ใช้สำหรับ ESP32 / ESP8266 ส่งข้อมูล sensor เข้าฐานข้อมูล
| ฟิลด์ | ประเภท | จำเป็น | คำอธิบาย |
|---|---|---|---|
room | int | จำเป็น | ห้องของฉัน |
ddate | string | ไม่บังคับ | วันที่/เวลา YYYY-MM-DD HH:MM:SS (เว้น = เวลาเซิร์ฟเวอร์) |
Temp2 | float | ไม่บังคับ | อุณหภูมิ (°C) |
Humi1 | float | ไม่บังคับ | ความชื้นดิน (%) |
Humi2 | float | ไม่บังคับ | ความชื้นอากาศ (%) |
# ส่งข้อมูลแบบ JSON curl -X POST -H "Content-Type: application/json" -d '{ "room": "ห้องของฉัน", "Temp2": 27.2, "Humi1": 65.3, "Humi2": 60.1 }' "http://YOUR_HOST/iot-dashboard/api/v1/post.php" | python -m json.tool
# ส่งข้อมูลแบบ form-urlencoded (เหมาะกับ ESP8266) curl -X POST -d "room=ห้องของฉัน&Temp2=27.2&Humi1=65.3&Humi2=60.1" "http://YOUR_HOST/iot-dashboard/api/v1/post.php" | python -m json.tool
# ส่งแค่ room + อุณหภูมิ + ความชื้น (ฟิลด์อื่นเว้นได้) curl -X POST -H "Content-Type: application/json" -d '{"room":"ห้องของฉัน","Temp2":29.1,"Humi1":72.4}' "http://YOUR_HOST/iot-dashboard/api/v1/post.php" | python -m json.tool # ส่งแค่ room (ค่า sensor ทั้งหมด = NULL, ใช้เวลาเซิร์ฟเวอร์) curl -X POST -d "room=ห้องของฉัน" "http://YOUR_HOST/iot-dashboard/api/v1/post.php" | python -m json.tool
{
"status": "ok",
"message": "Data inserted",
"id": 1001
}
{
"status": "error",
"message": "Missing required field: room"
}
{
"status": "error",
"message": "Method not allowed. Use POST."
}
ใช้สำหรับดึงข้อมูลจากฐานข้อมูลผ่าน ESP32 / โปรแกรมอื่นๆ
| พารามิเตอร์ | ประเภท | ค่า default | คำอธิบาย |
|---|---|---|---|
action | string | list | list | latest | range |
room | int | ทั้งหมด | ห้องของฉัน |
from | string | ไม่ระบุ | วันที่เริ่มต้น YYYY-MM-DD |
to | string | ไม่ระบุ | วันที่สิ้นสุด YYYY-MM-DD |
limit | int | 100 | จำนวนแถวสูงสุด (max 500) |
offset | int | 0 | ข้ามแถว |
order | string | desc | asc | desc |
# ดึงข้อมูลล่าสุด 100 แถว (default) curl -s "http://YOUR_HOST/iot-dashboard/api/v1/get.php?room=ห้องของฉัน" | python -m json.tool # ดึงข้อมูลล่าสุดแค่ 1 แถว curl -s "http://YOUR_HOST/iot-dashboard/api/v1/get.php?latest=1&room=ห้องของฉัน" | python -m json.tool # ดึงข้อมูลช่วงวันที่ จำกัด 50 แถว เรียงจากเก่าไปใหม่ curl -s "http://YOUR_HOST/iot-dashboard/api/v1/get.php?action=range&room=ห้องของฉัน&from=2022-08-05&to=2022-08-06&limit=50&order=asc" | python -m json.tool # ดึงข้อมูลหน้าที่ 2 (ข้าม 100 แถวแรก) curl -s "http://YOUR_HOST/iot-dashboard/api/v1/get.php?room=ห้องของฉัน&limit=100&offset=100" | python -m json.tool
{
"status": "ok",
"total": 1000,
"count": 2,
"offset": 0,
"limit": 100,
"data": [
{
"ID": 1000,
"ddate": "2022-08-09 18:04:53",
"Room": "ห้องของฉัน",
"Temp2": 27.8,
"Humi1": 68.6,
"Humi2": 62.2
}
]
}
ทดลองเรียก API ได้ทันทีจากหน้านี้ — กรอกข้อมูลแล้วกด "ส่งคำขอ"
X-API-Key ให้อัตโนมัติทุกคำขอ (GET/POST tester และการจำลอง alert)| เมธอด | Endpoint | คำอธิบาย | ต้องใช้ Key |
|---|---|---|---|
| GET | api.php?action=meta | รายการห้องและช่วงข้อมูลที่มี | — |
| GET | api.php?action=summary | ค่าล่าสุดและสรุป | — |
| GET | api.php?action=timeseries | ข้อมูลแนวโน้ม (group=raw/hour/day) | — |
| GET | api.php?action=data | ตารางข้อมูลแบ่งหน้า | — |
| GET | api.php?action=daily | สรุปรายวัน | — |
| GET | api.php?action=export | ดาวน์โหลด CSV | — |
| GET | api/v1/get.php | ดึงข้อมูลเวอร์ชัน v1 (list/latest/range/meta) | ✅ ต้องส่ง |
| POST | api/v1/post.php | ส่งข้อมูลจากเซนเซอร์ (JSON / form) | ✅ ต้องส่ง |
# ไม่ต้องใช้ Key (api.php หลัก) curl -s "https://YOUR_HOST/iot-dashboard/api.php?action=summary&room=ห้องของฉัน&from=2026-09-01&to=2026-09-30" # ต้องใช้ Key (api/v1/get.php) — ส่งผ่าน Header หรือ Query curl -s -H "X-API-Key: iot-2026-18207" "https://YOUR_HOST/iot-dashboard/api/v1/get.php?latest=1&room=ห้องของฉัน" curl -s "https://YOUR_HOST/iot-dashboard/api/v1/get.php?room=ห้องของฉัน&limit=10&key=iot-2026-18207"
# JSON body + Key ใน Header curl -X POST -H "Content-Type: application/json" -H "X-API-Key: iot-2026-18207" -d '{"room":"ห้องของฉัน","Temp2":27.2,"Humi1":65.3,"Humi2":60.1}' "https://YOUR_HOST/iot-dashboard/api/v1/post.php" # form-urlencoded + Key ใน Header curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -H "X-API-Key: iot-2026-18207" -d "room=ห้องของฉัน&Temp2=27.2&Humi1=65.3&Humi2=60.1" "https://YOUR_HOST/iot-dashboard/api/v1/post.php" # Query ?key= (ส่งถูกต้อง แต่แนะนำ Header ดีกว่าเพื่อความปลอดภัย) curl -X POST -d "room=ห้องของฉัน&Temp2=27.2" "https://YOUR_HOST/iot-dashboard/api/v1/post.php?key=iot-2026-18207"
iot-2026-18207X-API-Key ให้อัตโนมัติ คุณเปลี่ยน key เป็นของตัวเองได้ที่ security.php (กำหนด const API_KEY = '...') แล้วแก้ช่อง API Key ให้ตรงกันiot-2026-18207| ฟิลด์ | หน่วย | คำอธิบาย | ตัวอย่างค่า |
|---|---|---|---|
Temp2 | °C | อุณหภูมิอากาศ (DHT22) | 27.2 |
Humi1 | % | ความชื้นในดิน (Capacitive Sensor) | 65.3 |
Humi2 | % | ความชื้นอากาศ (DHT22) | 60.1 |
security.php) — ทุกคนเข้าถึงได้ เหมาะกับทดสอบในเครื่องsecurity.php : const API_KEY = 'your-secret-key';# วิธีที่ 1: ส่งผ่าน Header curl -X POST -H "Content-Type: application/json" \ -H "X-API-Key: your-secret-key" \ -d '{"room":"ห้องของฉัน","Temp2":27.2}' \ "http://YOUR_HOST/iot-dashboard/api/v1/post.php" # วิธีที่ 2: ส่งผ่าน Query String curl -s "http://YOUR_HOST/iot-dashboard/api/v1/get.php?room=ห้องของฉัน&key=your-secret-key" | python -m json.tool
{
"status": "error",
"message": "Invalid API key"
}
#include <WiFi.h> #include <HTTPClient.h> const char* ssid = "YOUR_WIFI_SSID"; const char* password = "YOUR_WIFI_PASS"; const char* serverUrl = "http://YOUR_HOST/iot-dashboard/api/v1/post.php"; const char* room = "ห้องของฉัน"; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nWiFi connected!"); } void loop() { if (WiFi.status() == WL_CONNECTED) { // --- อ่านค่า sensor --- float temp2 = readTemperature(); float humi1 = readSoilMoisture(); float humi2 = readHumidity(); // --- สร้าง JSON --- String json = "{\"room\":\"" + String(room) + "\"" + ",\"Temp2\":" + String(temp2, 1) + ",\"Humi1\":" + String(humi1, 1) + ",\"Humi2\":" + String(humi2, 1) + "}"; // --- ส่ง POST --- HTTPClient http; http.begin(serverUrl); http.addHeader("Content-Type", "application/json"); int code = http.POST(json); Serial.println("Response: " + String(code)); http.end(); } delay(300000); // ส่งทุก 5 นาที }
#include <ESP8266WiFi.h> #include <ESP8266HTTPClient.h> #include <WiFiClient.h> const char* serverUrl = "http://YOUR_HOST/iot-dashboard/api/v1/post.php"; void sendData(float temp, float humi1, float humi2) { WiFiClient client; HTTPClient http; http.begin(client, serverUrl); http.addHeader("Content-Type", "application/x-www-form-urlencoded"); String body = "room=ห้องของฉัน&Temp2=" + String(temp, 1) + "&Humi1=" + String(humi1, 1) + "&Humi2=" + String(humi2, 1); int code = http.POST(body); Serial.println("Response: " + String(code)); http.end(); }