← กลับ Dashboard

IoT REST API Manual

คู่มือ REST API

IoT Energy Monitor — ส่งและรับข้อมูล sensor ผ่าน HTTP

GET + POST JSON Response ESP32 Compatible

สารบัญ

Dashboard API

ใช้สำหรับหน้า Dashboard — เรียกผ่าน browser หรือ JavaScript fetch

GET api.php?action=meta ดึงข้อมูลห้องและช่วงวันที่
# ดึงข้อมูล meta (ห้อง + ช่วงวันที่)
curl -s "http://YOUR_HOST/iot-dashboard/api.php?action=meta" | python -m json.tool
200 OKResponse
{
  "rooms": ["ห้องของฉัน"],
  "min": "2022-08-05 01:37:53",
  "max": "2022-08-09 18:04:53"
}
GET api.php?action=summary สรุปค่าล่าสุด + สถิติ
พารามิเตอร์ประเภทจำเป็นคำอธิบาย
roomintจำเป็นห้องของฉัน
fromstringไม่บังคับวันที่เริ่มต้น YYYY-MM-DD
tostringไม่บังคับวันที่สิ้นสุด 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
200 OKResponse
{
  "last_ddate": "2022-08-09 18:04:53",
  "last_temp2": 27.8,
  "last_humi1": 68.6,
  "last_humi2": 62.2,
  "record_count": 1000,
  ...
}
GET api.php?action=timeseries ข้อมูลแบบ Time Series (สำหรับกราฟ)
พารามิเตอร์ประเภทจำเป็นคำอธิบาย
roomintจำเป็นห้องของฉัน
fromstringจำเป็นวันที่เริ่มต้น YYYY-MM-DD
tostringจำเป็นวันที่สิ้นสุด YYYY-MM-DD
groupstringไม่บังคับ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"
200 OKResponse
{
  "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, ...],
    ...
  }
}
GET api.php?action=data ตารางข้อมูลแบบแยกหน้า
พารามิเตอร์ประเภทจำเป็นคำอธิบาย
roomintจำเป็นห้องของฉัน
fromstringจำเป็นวันที่เริ่มต้น
tostringจำเป็นวันที่สิ้นสุด
pageintไม่บังคับหน้า (default: 1)
per_pageintไม่บังคับจำนวนแถวต่อหน้า (default: 25, max: 200)
searchstringไม่บังคับคำค้นหา
# ดึงข้อมูลหน้า 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"
GET api.php?action=daily สถิติรวมรายวัน
curl -s "http://YOUR_HOST/iot-dashboard/api.php?action=daily&room=ห้องของฉัน&from=2022-08-05&to=2022-08-09" | python -m json.tool
GET api.php?action=export ดาวน์โหลด CSV
ดาวน์โหลดข้อมูลเป็นไฟล์ CSV (Excel/Google Sheets เปิดได้) ตามช่วงวันที่ที่เลือก
พารามิเตอร์ประเภทจำเป็นคำอธิบาย
roomintจำเป็นห้องของฉัน
fromstringจำเป็นวันที่เริ่มต้น
tostringจำเป็นวันที่สิ้นสุด
searchstringไม่บังคับกรองด้วยคำค้นหา
คอลัมน์คำอธิบาย
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"

ส่งข้อมูล POST

ใช้สำหรับ ESP32 / ESP8266 ส่งข้อมูล sensor เข้าฐานข้อมูล

POST api/v1/post.php ส่งข้อมูล sensor ใหม่
รองรับ 2 รูปแบบ: application/json และ x-www-form-urlencoded (เหมาะกับ ESP8266/ESP32)
ฟิลด์ประเภทจำเป็นคำอธิบาย
roomintจำเป็นห้องของฉัน
ddatestringไม่บังคับวันที่/เวลา YYYY-MM-DD HH:MM:SS (เว้น = เวลาเซิร์ฟเวอร์)
Temp2floatไม่บังคับอุณหภูมิ (°C)
Humi1floatไม่บังคับความชื้นดิน (%)
Humi2floatไม่บังคับความชื้นอากาศ (%)
# ส่งข้อมูลแบบ 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
201 Createdสำเร็จ
{
  "status": "ok",
  "message": "Data inserted",
  "id": 1001
}
400 Bad Request缺少 room
{
  "status": "error",
  "message": "Missing required field: room"
}
405 Method Not Allowedใช้ method ผิด
{
  "status": "error",
  "message": "Method not allowed. Use POST."
}

ดึงข้อมูล GET

ใช้สำหรับดึงข้อมูลจากฐานข้อมูลผ่าน ESP32 / โปรแกรมอื่นๆ

GET api/v1/get.php ดึงข้อมูล sensor
พารามิเตอร์ประเภทค่า defaultคำอธิบาย
actionstringlistlist | latest | range
roomintทั้งหมดห้องของฉัน
fromstringไม่ระบุวันที่เริ่มต้น YYYY-MM-DD
tostringไม่ระบุวันที่สิ้นสุด YYYY-MM-DD
limitint100จำนวนแถวสูงสุด (max 500)
offsetint0ข้ามแถว
orderstringdescasc | 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
200 OKResponse
{
  "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 (Try It Out)

ทดลองเรียก API ได้ทันทีจากหน้านี้ — กรอกข้อมูลแล้วกด "ส่งคำขอ"

Base URL
เปลี่ยน URL ตามเซิร์ฟเวอร์ของคุณได้ (ค่าเริ่มต้น = โดเมนปัจจุบัน)
API Key (X-API-Key)
Key ของเราเอง — จะถูกแนบใน Header X-API-Key ให้อัตโนมัติทุกคำขอ (GET/POST tester และการจำลอง alert)
GET ทดสอบ GET Request
POST ทดสอบ POST Request


            

            
            
        

รูปแบบการ GET / POST

เมธอดEndpointคำอธิบายต้องใช้ Key
GETapi.php?action=metaรายการห้องและช่วงข้อมูลที่มี—
GETapi.php?action=summaryค่าล่าสุดและสรุป—
GETapi.php?action=timeseriesข้อมูลแนวโน้ม (group=raw/hour/day)—
GETapi.php?action=dataตารางข้อมูลแบ่งหน้า—
GETapi.php?action=dailyสรุปรายวัน—
GETapi.php?action=exportดาวน์โหลด CSV—
GETapi/v1/get.phpดึงข้อมูลเวอร์ชัน v1 (list/latest/range/meta)✅ ต้องส่ง
POSTapi/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"

การทดสอบ GET / POST (ระบุ API Key ของเราเอง)

API Key ที่เปิดใช้อยู่: iot-2026-18207
Key นี้ถูกกรอกไว้แล้วในช่อง API Key ด้านบน — tester ด้านล่างจะแนบไปใน Header X-API-Key ให้อัตโนมัติ คุณเปลี่ยน key เป็นของตัวเองได้ที่ security.php (กำหนด const API_KEY = '...') แล้วแก้ช่อง API Key ให้ตรงกัน
  1. กรอก Base URL (ค่าเริ่มต้น = โดเมนปัจจุบันที่เปิดหน้านี้)
  2. ตรวจช่อง API Key = iot-2026-18207
  3. กด ส่ง GET Request หรือ ส่ง POST Request ด้านล่าง ผลลัพธ์จะแสดงหน่วยความเร็ว/สถานะ/ข้อมูล
ยังไม่มีการทดสอบ — ใช้ tester GET / POST ด้านบนเพื่อทดสอบจริง

การจำลอง Alert ต่าง ๆ

จำลองสถานการณ์ alert ที่ระบบตอบกลับ — กดแต่ละปุ่มเพื่อส่งคำขอจริงและดู response ที่เกิดขึ้น (ตาม key ที่กรอกไว้ด้านบน)

ฟิลด์ทั้งหมด (Fields)

ฟิลด์หน่วยคำอธิบายตัวอย่างค่า
Temp2°Cอุณหภูมิอากาศ (DHT22)27.2
Humi1%ความชื้นในดิน (Capacitive Sensor)65.3
Humi2%ความชื้นอากาศ (DHT22)60.1

การยืนยันตัวตน (API Key)

ปัจจุบัน API Key ปิดอยู่ (เว้นว่างใน 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
401 UnauthorizedKey ไม่ถูกต้อง
{
  "status": "error",
  "message": "Invalid API key"
}

ตัวอย่างโค้ด ESP32

#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();
}