REST API Documentation

IoT Sensor Data API for ESP32 / ESP8266 | Server: 119.59.96.93

Overview

ItemValue
Base URLhttp://119.59.96.93/api/v1/
Local URLhttp://localhost/api/v1/
API Keyiot_rmuti_2024
DatabaseDB12569_EE66172310386
Tabledata_iot_test
FormatJSON
Port3306 (MySQL) / 80 (HTTP)
API Key ต้องส่งใน POST body เท่านั้น สำหรับ GET แนะนำให้ส่งผ่าน query string

POST API - ส่งข้อมูลจาก ESP

POST
http://119.59.96.93/api/v1/post.php

ส่งข้อมูล sensor จาก ESP32/ESP8266 ไปเก็บใน MySQL

Request Headers

Content-Type: application/json

Request Body (JSON)

FieldTypeRequiredExampleDescription
api_keystringREQUIRED"iot_rmuti_2024"API Key สำหรับยืนยันตัวตน
RoomintegerREQUIRED"18207"รหัสห้อง
Temp1floatoptional"27.50"อุณหภูมิ sensor 1
Temp2floatoptional"28.10"อุณหภูมิ sensor 2
Humi1floatoptional"68.50"ความชื้น sensor 1
Humi2floatoptional"65.20"ความชื้น sensor 2
Volt1floatoptional"227.50"แรงดันไฟฟ้า (V)
Current1floatoptional"0.50"กระแสไฟฟ้า (A)
Power1floatoptional"113.75"กำลังไฟฟ้า (W)
Kwh1floatoptional"372.14"พลังงานไฟฟ้า (kWh)
Freq1floatoptional"49.90"ความถี่ (Hz)
Pf1floatoptional"1.00"Power Factor
(ddate, date1, time1) จะถูกสร้างอัตโนมัติจาก server time ไม่ต้องส่งมา

Response สำเร็จ 200 OK

{
    "status": "success",
    "message": "Data inserted successfully",
    "id": 1001,
    "datetime": "2026-08-25 13:37:45"
}

Response ผิดพลาด

// 401 - API Key ผิด
{ "status": "error", "message": "Invalid API key" }

// 400 - ข้อมูลไม่ครบ
{ "status": "error", "message": "Missing required field: Room" }

// 400 - JSON ผิดรูปแบบ
{ "status": "error", "message": "Invalid JSON body" }

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

GET
http://119.59.96.93/api/v1/get.php

ดึงข้อมูลจาก MySQL กลับมาในรูปแบบ JSON

Query Parameters

ParameterTypeRequiredExampleDescription
api_keystringoptional?api_key=iot_rmuti_2024API Key
lateststringoptional?latest=1ดึงข้อมูลล่าสุด 1 record
limitintegeroptional?limit=50จำนวน records สูงสุด 1000 (default: 10)
roomintegeroptional?room=18207กรองตามห้อง
fromstringoptional?from=2022-08-05วันที่เริ่มต้น (YYYY-MM-DD)
tostringoptional&to=2022-08-06วันที่สิ้นสุด (YYYY-MM-DD)
idintegeroptional?id=100ดึงข้อมูลตาม ID

Request Examples

URLคำอธิบาย
/get.phpดึง 10 รายการล่าสุด
/get.php?latest=1ดึงข้อมูลล่าสุด 1 record
/get.php?limit=50ดึง 50 รายการล่าสุด
/get.php?room=18207&limit=20ดึง 20 รายการของห้อง 18207
/get.php?from=2022-08-05&to=2022-08-06ดึงข้อมูลวันที่ 5-6 ส.ค.
/get.php?id=100ดึง record ID=100

Response 200 OK

{
    "status": "success",
    "total": 2,
    "data": [
        {
            "ID": "1000",
            "ddate": "2022-08-09 18:04:53",
            "Room": "18207",
            "Temp1": "29.10",
            "Temp2": "27.80",
            "Humi1": "68.60",
            "Humi2": "62.20",
            "Volt1": "224.60",
            "Current1": "0.00",
            "Power1": "0.00",
            "Kwh1": "388.00",
            "Freq1": "50.00",
            "Pf1": "1.00",
            "date1": "09/08/2022",
            "time1": "18:04:53"
        }
    ]
}

cURL Test

เปิด Command Prompt หรือ PowerShell แล้วรันคำสั่งเหล่านี้

1. ทดสอบ POST - ส่งข้อมูล

:: POST ส่งข้อมูล sensor ไปเก็บ
curl -X POST http://119.59.96.93/api/v1/post.php ^
  -H "Content-Type: application/json" ^
  -d "{\"api_key\":\"iot_rmuti_2024\",\"Room\":\"18207\",\"Temp1\":\"28.50\",\"Temp2\":\"27.80\",\"Humi1\":\"70.20\",\"Humi2\":\"63.10\",\"Volt1\":\"226.30\",\"Current1\":\"0.75\",\"Power1\":\"169.35\",\"Kwh1\":\"388.50\",\"Freq1\":\"50.00\",\"Pf1\":\"0.98\"}"

2. ทดสอบ GET - ดึงข้อมูลล่าสุด

:: ดึงข้อมูลล่าสุด 1 record
curl http://119.59.96.93/api/v1/get.php?latest=1

:: ดึง 50 รายการล่าสุด
curl http://119.59.96.93/api/v1/get.php?limit=50

:: ดึงข้อมูลห้อง 18207
curl http://119.59.96.93/api/v1/get.php?room=18207&limit=10

:: ดึงข้อมูลตามวันที่
curl http://119.59.96.93/api/v1/get.php?from=2022-08-05&to=2022-08-06

:: ดึง record ID=100
curl http://119.59.96.93/api/v1/get.php?id=100

3. cURL สำหรับ PowerShell

# PowerShell - POST
$body = '{"api_key":"iot_rmuti_2024","Room":"18207","Temp1":"28.50","Temp2":"27.80","Humi1":"70.20","Humi2":"63.10","Volt1":"226.30","Current1":"0.75","Power1":"169.35","Kwh1":"388.50","Freq1":"50.00","Pf1":"0.98"}'
Invoke-RestMethod -Uri "http://119.59.96.93/api/v1/post.php" -Method POST -Body $body -ContentType "application/json"

# PowerShell - GET
Invoke-RestMethod -Uri "http://119.59.96.93/api/v1/get.php?latest=1"

4. cURL สำหรับ Linux/Mac

# Linux/Mac - POST
curl -X POST http://119.59.96.93/api/v1/post.php \
  -H "Content-Type: application/json" \
  -d '{"api_key":"iot_rmuti_2024","Room":"18207","Temp1":"28.50","Temp2":"27.80","Humi1":"70.20","Humi2":"63.10","Volt1":"226.30","Current1":"0.75","Power1":"169.35","Kwh1":"388.50","Freq1":"50.00","Pf1":"0.98"}'

# Linux/Mac - GET
curl http://119.59.96.93/api/v1/get.php?latest=1

Try It - POST

ส่งข้อมูล POST ไปที่ Server

Response will appear here...

Try It - GET

ดึงข้อมูลจาก Server

Response will appear here...

ESP32 Arduino Code

// ============================================
//  ESP32 IoT Sensor -> REST API
// ============================================

#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>

const char* WIFI_SSID     = "YOUR_WIFI_SSID";
const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";
const char* SERVER_URL    = "http://119.59.96.93/api/v1/post.php";
const char* API_KEY       = "iot_rmuti_2024";

void setup() {
  Serial.begin(115200);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }
  Serial.println("WiFi Connected!");
}

void loop() {
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    http.begin(SERVER_URL);
    http.addHeader("Content-Type", "application/json");

    StaticJsonDocument<512> doc;
    doc["api_key"]   = API_KEY;
    doc["Room"]      = "18207";
    doc["Temp1"]     = "28.50";
    doc["Humi1"]     = "70.20";
    doc["Volt1"]     = "226.30";
    doc["Current1"]  = "0.75";
    doc["Power1"]    = "169.35";
    doc["Kwh1"]      = "388.50";
    doc["Freq1"]     = "50.00";
    doc["Pf1"]       = "0.98";

    String json;
    serializeJson(doc, json);

    int code = http.POST(json);
    Serial.println(http.getString());
    http.end();
  }
  delay(300000); // ทุก 5 นาที
}

ESP8266 Arduino Code

// ============================================
//  ESP8266 IoT Sensor -> REST API
// ============================================

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>

const char* WIFI_SSID     = "YOUR_WIFI_SSID";
const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";
const char* SERVER_URL    = "http://119.59.96.93/api/v1/post.php";
const char* API_KEY       = "iot_rmuti_2024";

WiFiClient wifiClient;

void setup() {
  Serial.begin(115200);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }
  Serial.println("WiFi Connected!");
}

void loop() {
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    http.begin(wifiClient, SERVER_URL);
    http.addHeader("Content-Type", "application/json");

    StaticJsonDocument<512> doc;
    doc["api_key"]   = API_KEY;
    doc["Room"]      = "18207";
    doc["Temp1"]     = "28.50";
    doc["Humi1"]     = "70.20";
    doc["Volt1"]     = "226.30";

    String json;
    serializeJson(doc, json);

    int code = http.POST(json);
    Serial.println(http.getString());
    http.end();
  }
  delay(300000); // ทุก 5 นาที
}

File Locations

FilePath
API ConfigC:\xampp\htdocs\api\v1\config.php
POST APIC:\xampp\htdocs\api\v1\post.php
GET APIC:\xampp\htdocs\api\v1\get.php
DashboardC:\xampp\htdocs\index.php
API DocsC:\xampp\htdocs\postget.html
ESP32 CodeC:\Users\Win10pro\Desktop\ไมโคร\arduino\ESP32_IoT_Sensor\ESP32_IoT_Sensor.ino
ESP8266 CodeC:\Users\Win10pro\Desktop\ไมโคร\arduino\ESP8266_IoT_Sensor\ESP8266_IoT_Sensor.ino