POST
POST API - ส่งข้อมูลจากอุปกรณ์ IoT
ข้อมูลทั่วไป
ใช้ส่งค่าจากเซนเซอร์ (ESP32/ESP8266) เข้าระบบ ระบบบันทึกวัน/เวลาให้อัตโนมัติ (ddate) และเพิ่ม ID ใหม่ทุกครั้ง
POST/iot-dashboard/api/v1/post.php
📌 Header: Content-Type: application/json + X-API-Key: iot-dashboard-2024
🔒 รองรับเฉพาะเมธอด POST เมธอดอื่นจะได้ error 405 · ฟิลด์ที่ไม่ส่งจะถูกบันทึกเป็น 0
ฟิลด์ของ Body (JSON)
| Field | Type | Required | คำอธิบาย |
| room | integer | Optional | รหัสห้อง/จุดติดตั้ง เช่น 18207 |
| temp1 | number | Optional | อุณหภูมิเซนเซอร์ 1 (°C) |
| temp2 | number | Optional | อุณหภูมิเซนเซอร์ 2 (°C) |
| humi1 | number | Optional | ความชื้นเซนเซอร์ 1 (%) |
| humi2 | number | Optional | ความชื้นเซนเซอร์ 2 (%) |
| gas1 | number | Optional | ค่าแก๊ส/ควัน MQ2 (PPM) — ESP8266+DHT22+MQ2 |
| volt1 | number | Optional | แรงดันไฟฟ้า (V) |
| current1 | number | Optional | กระแสไฟฟ้า (A) |
| power1 | number | Optional | กำลังไฟฟ้า (W) |
| kwh1 | number | Optional | พลังงานสะสม (kWh) |
| freq1 | number | Optional | ความถี่ไฟฟ้า (Hz) |
| pf1 | number | Optional | Power Factor (0-1) |
ตัวอย่าง Body (JSON)
JSON Body
{
"room": 18207,
"temp1": 28.5,
"temp2": 27.3,
"humi1": 65.2,
"humi2": 62.8,
"gas1": 150.5,
"volt1": 220.5,
"current1": 5.2,
"power1": 1146.6,
"kwh1": 125.8,
"freq1": 50.0,
"pf1": 0.95
}
✅ Response สำเร็จ (200): {"status":"ok","id":N,"message":"Data saved successfully","timestamp":"..."}
❌ error 401 (ไม่มี API Key) / error 400 (Body ว่าง) / error 405 (Method ผิด)
curl - POST (Windows cmd)
cmd ใช้ ^ ขยายบรรทัด
curl -X POST https://project12569.theeeiot.com/EE66172310060/api/v1/post.php ^
-H "Content-Type: application/json" ^
-H "X-API-Key: iot-dashboard-2024" ^
-d "{\"room\":18207,\"temp1\":28.5,\"humi1\":65.2,\"volt1\":220.5}"
curl - POST (PowerShell)
PowerShell ใช้ ` ขยายบรรทัด
curl.exe -X POST https://project12569.theeeiot.com/EE66172310060/api/v1/post.php `
-H "Content-Type: application/json" `
-H "X-API-Key: iot-dashboard-2024" `
-d '{"room":18207,"temp1":28.5,"humi1":65.2,"volt1":220.5}'
⚠️ สำคัญ: ใน PowerShell ต้องพิมพ์ curl.exe ไม่ใช่ curl (ถูกแทนที่ด้วย Invoke-WebRequest)
curl - POST (Linux / Mac)
Bash ใช้ \ ขยายบรรทัด
curl -X POST https://project12569.theeeiot.com/EE66172310060/api/v1/post.php \
-H "Content-Type: application/json" \
-H "X-API-Key: iot-dashboard-2024" \
-d '{"room":18207,"temp1":28.5,"humi1":65.2,"volt1":220.5}'
ทดลองเพิ่มเติม
ส่งแบบ x-www-form-urlencoded (เหมือนฟอร์ม):
curl.exe
curl.exe -X POST https://project12569.theeeiot.com/EE66172310060/api/v1/post.php `
-H "X-API-Key: iot-dashboard-2024" `
-d "room=18207&temp1=28.5&humi1=65.2"
แสดง debug headers (-v) + วัดเวลา (-w):
curl.exe
curl.exe -v -X POST https://project12569.theeeiot.com/EE66172310060/api/v1/post.php `
-H "X-API-Key: iot-dashboard-2024" `
-H "Content-Type: application/json" `
-d '{"room":18207,"temp1":25.0}' `
-w "`nHTTP: %{http_code} · เวลา: %{time_total}s`n"
🧪 ทดสอบ error: ลองส่งโดยไม่ใส่ key (ตัดบรรทัด -H) จะได้ {"status":"error","message":"Unauthorized..."}
API
Main API (api.php) - ใช้กับดัชบอร์ด
ข้อมูลทั่วไป
ใช้สำหรับกราฟ/แดชบอร์ด วันที่รูปแบบ YYYY-MM-DD (ระบบแปลงให้อัตโนมัติ) ทุกคำขอต้องมี api_key
GET/iot-dashboard/api.php
Action & parameters
| Action | คำอธิบาย | Parameters |
| summary | สรุปสถิติ + ห้อง + วันที่ | date_from, date_to |
| timeseries | ข้อมูลลำดับเวลาสำหรับกราฟ | date_from, date_to, limit (max 2000) |
| daily | สรุปรายวัน | date_from, date_to |
| data | ข้อมูลดิบแบบแบ่งหน้า | date_from, date_to, page, per_page |
| export | ส่งออกทั้งหมดเป็น JSON | date_from, date_to |
ตัวอย่าง curl
สรุป
curl.exe "https://project12569.theeeiot.com/EE66172310060/api.php?action=summary&api_key=iot-dashboard-2024"
Time series (กราฟ)
curl.exe "https://project12569.theeeiot.com/EE66172310060/api.php?action=timeseries&limit=100&api_key=iot-dashboard-2024"
ส่งออกข้อมูล
curl.exe "https://project12569.theeeiot.com/EE66172310060/api.php?action=export&api_key=iot-dashboard-2024" -o iot_export.json
TEST
ทดสอบ POST (ส่งตรงจากหน้านี้)