EEIoT Dashboard · คู่มือ REST API

การส่งข้อมูลผ่าน POST / GET พร้อมตัวอย่างการทดสอบด้วย curl และระบบ API Key

ภาพรวม

ภาพรวม REST API

REST API คืออะไร

REST API เป็นวิธีแลกเปลี่ยนข้อมูลระหว่างอุปกรณ์ IoT (ESP32/ESP8266 เซนเซอร์ หรือเว็บแอป) กับเซิร์ฟเวอร์ผ่าน HTTP

  • POST = ส่งข้อมูลเข้าเซิร์ฟเวอร์ (บันทึกค่าจากเซนเซอร์)
  • GET = ขอดึงข้อมูลจากเซิร์ฟเวอร์ (อ่านค่าที่บันทึกไว้)
  • ข้อมูลที่ส่ง/รับเป็นรูปแบบ JSON
  • ทุกคำขอต้องมี API Key (ดูหัวข้อถัดไป)

Base URL ของระบบ

POSThttps://project12569.theeeiot.com/EE66172310060/api/v1/post.php
GEThttps://project12569.theeeiot.com/EE66172310060/api/v1/get.php
GEThttps://project12569.theeeiot.com/EE66172310060/api.php
🧩 ข้อกำหนด: ต้องเปิด XAMPP (Apache) ก่อน และเครื่องต้องมี curl (curl --version)
📚 ข้อมูล: ตาราง data_iot_test ของ MySQL · ห้อง 18207 · ความถี่การวัดทุก ~5 นาที · หน่วย: °C / % / V / A / W / kWh / Hz / PF
KEY

API Key - กุญแจเข้าถึงระบบ

ทำไมต้อง API Key

ระบบกำหนดให้ ทุกคำขอ (GET/POST) ต้องมี API Key เพื่อยืนยันตัวตน ถ้าไม่มีหรือใส่ผิดจะได้รับ error 401 Unauthorized

🔑 API Key ของระบบ: iot-dashboard-2024

วิธีส่ง API Key (2 แบบ)

แบบที่ 1: ส่งผ่าน HTTP Header X-API-Key (แนะนำ เหมาะกับทุกเมธอด):

Header
-H "X-API-Key: iot-dashboard-2024"

แบบที่ 2: ใส่เป็น query parameter ?api_key= (สะดวกสำหรับ GET / ทดสอบในเบราว์เซอร์):

URL
https://project12569.theeeiot.com/EE66172310060/api/v1/get.php?action=latest&api_key=iot-dashboard-2024
⚠️ กรณีไม่มี API Key: จะได้รับ error 401 Unauthorized พร้อมข้อความแจ้งเตือน
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)

FieldTypeRequiredคำอธิบาย
roomintegerOptionalรหัสห้อง/จุดติดตั้ง เช่น 18207
temp1numberOptionalอุณหภูมิเซนเซอร์ 1 (°C)
temp2numberOptionalอุณหภูมิเซนเซอร์ 2 (°C)
humi1numberOptionalความชื้นเซนเซอร์ 1 (%)
humi2numberOptionalความชื้นเซนเซอร์ 2 (%)
gas1numberOptionalค่าแก๊ส/ควัน MQ2 (PPM) — ESP8266+DHT22+MQ2
volt1numberOptionalแรงดันไฟฟ้า (V)
current1numberOptionalกระแสไฟฟ้า (A)
power1numberOptionalกำลังไฟฟ้า (W)
kwh1numberOptionalพลังงานสะสม (kWh)
freq1numberOptionalความถี่ไฟฟ้า (Hz)
pf1numberOptionalPower 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..."}
GET

GET API - ดึงข้อมูล IoT

ข้อมูลทั่วไป

ใช้ดึงข้อมูลจากฐานข้อมูล โดยเลือกชนิดผ่าน parameter action (ค่าเริ่มต้น: latest) และต้องมี api_key เสมอ

GET/iot-dashboard/api/v1/get.php

Parameters

ParameterTypeRequiredคำอธิบาย
api_keystringRequiredกุญแจเข้าถึงระบบ
actionstringOptionallatest | all | date | range | summary | stats | command
roomintegerOptionalรหัสห้อง (ต้องเป็นตัวเลข เช่น 18207)
limitintegerOptionalจำนวนสูงสุด 1-1000 (ค่าเริ่มต้น 10)
datestringOptionalวันที่ รูปแบบ DD/MM/YYYY
fromstringOptionalวันที่เริ่มต้น DD/MM/YYYY
tostringOptionalวันที่สิ้นสุด DD/MM/YYYY
📌 คำแนะนำ: ส่ง API Key ผ่าน header X-API-Key หรือต่อท้าย URL &api_key=iot-dashboard-2024 วิธีไหนก็ได้

Action ที่ใช้ได้

ActionคำอธิบายParameters
latestข้อมูลล่าสุด 1 รายการ (ทุกห้อง/ตามห้อง)room
allข้อมูลหลายรายการ เรียง ID ล่าสุดก่อนroom, limit, from, to
dateข้อมูลเฉพาะวันที่date, room
rangeข้อมูลภายในช่วงวันที่from, to, room, limit
summaryสรุปสถิติ (avg/min/max/sum)room, from, to
statsค่าล่าสุดแบบสั้น-
commandดึงคำสั่งที่รออุปกรณ์-

ตัวอย่าง curl ทุก Action (ใช้ api_key ผ่าน URL)

ข้อมูลล่าสุด:

curl.exe
curl.exe "https://project12569.theeeiot.com/EE66172310060/api/v1/get.php?action=latest&api_key=iot-dashboard-2024"

ข้อมูลล่าสุดของห้อง:

curl.exe
curl.exe "https://project12569.theeeiot.com/EE66172310060/api/v1/get.php?action=latest&room=18207&api_key=iot-dashboard-2024"

หลายรายการ + limit:

curl.exe
curl.exe "https://project12569.theeeiot.com/EE66172310060/api/v1/get.php?action=all&room=18207&limit=10&api_key=iot-dashboard-2024"

ตามวันที่ (DD/MM/YYYY):

curl.exe
curl.exe "https://project12569.theeeiot.com/EE66172310060/api/v1/get.php?action=date&date=09/08/2022&room=18207&api_key=iot-dashboard-2024"

ช่วงวันที่:

curl.exe
curl.exe "https://project12569.theeeiot.com/EE66172310060/api/v1/get.php?action=range&from=05/08/2022&to=09/08/2022&room=18207&limit=50&api_key=iot-dashboard-2024"

สรุปสถิติ:

curl.exe
curl.exe "https://project12569.theeeiot.com/EE66172310060/api/v1/get.php?action=summary&room=18207&api_key=iot-dashboard-2024"

แสดงผล JSON จัดรูปแบบ (PowerShell):

curl.exe | python
curl.exe -s "https://project12569.theeeiot.com/EE66172310060/api/v1/get.php?action=latest&api_key=iot-dashboard-2024" | python -m json.tool

บันทึกเป็นไฟล์:

curl.exe -o
curl.exe -s "https://project12569.theeeiot.com/EE66172310060/api/v1/get.php?action=all&room=18207&limit=100&api_key=iot-dashboard-2024" -o get_response.json
❌ ทดสอบ error: ลองดึงโดยไม่ใส่ api_key จะได้ {"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ส่งออกทั้งหมดเป็น JSONdate_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
curl

เทคนิคการใช้ curl

เช็คว่าเครื่องมี curl หรือไม่

Terminal
curl.exe --version
🪟 Windows 10/11 มี curl ในตัวอยู่แล้ว

option ที่ใช้บ่อย

Optionความหมายตัวอย่าง
-Xกำหนดเมธอด-X POST
-Hกำหนด Header (ใช้กับ API Key)-H "X-API-Key: iot-dashboard-2024"
-dส่งข้อมูล body-d '{"room":18207}'
-vแสดงรายละเอียด (debug)-v
-sเงียบ ไม่แสดง progress-s
-oบันทึกผลลงไฟล์-o out.json
-wแสดง status/เวลา-w "%{http_code}"
TEST

ทดสอบ POST (ส่งตรงจากหน้านี้)

POST Request Tester

กำลังรอคำขอ...
TEST

ทดสอบ GET (ส่งตรงจากหน้านี้)

GET Request Tester

กำลังรอคำขอ...

สรุป 3 ขั้นตอนการทดสอบ API

  1. เข้าดัชบอร์ดได้ที่ https://project12569.theeeiot.com/EE66172310060 (หรือ http://localhost/iot-dashboard/ สำหรับทดสอบบนเครื่อง)
  2. เตรียม API Key: iot-dashboard-2024 ใส่ใน header X-API-Key หรือ URL ?api_key=
  3. ทดสอบด้วย curl หรือแบบฟอร์มด้านบน แล้วแต่ว่าจะ POST (ส่งข้อมูล) หรือ GET (ดึงข้อมูล)
🔑 API Key เริ่มต้น: iot-dashboard-2024 · เปลี่ยนได้ในไฟล์ api/v1/post.php, api/v1/get.php, api.php