Overview
API health probe
Recent API activity
Notes
- Base URL auto-resolves:
- Read endpoints need
?device=GNSS01(default). - POST requires header
X-API-Key: <device key>andContent-Type: application/json. - Payload must follow JSON Schema
v1.0(see Request Format tab). - Rate limits: POST 60/min/device, GET 120/min/IP. X-RateLimit headers on every response.
- Duplicates return
200withduplicate: true; conflicting same-uuid payloads return409.
Test API — รายการ endpoint เรียกเร็ว ๆ
กดปุ่มเดียวต่อ endpoint เพื่อตรวจว่า reachable — เกณฑ์ PASS = status < 500 (เขียว <300 · เหลือง 4xx · แดง 5xx/network error). หมายเหตุ: satellite-track ที่ไม่ส่ง constellation จะคืน 422 ตาม design. ระบบรันอัตโนมัติครั้งแรกเมื่อเปิด tab นี้ (ครั้งเดียวต่อการโหลดเพจ ป้องกัน rate limit).
| endpoint | status | ms | result |
|---|
GET request builder
Response
Request history
| method | url | status | ms |
|---|
POST /api/v1/gnss.php
Response
Arduino IDE Code — Firmware ESP32-S3 จริง (GNSS01 · v1.0.3)
โค้ด firmware ที่รันอยู่บนบอร์ดล่าสุด — แสดงทุกไฟล์ในโฟลเดอร์โปรเจกต์ พร้อม Copy / Download ใส่ Arduino IDE
เลือกไฟล์เพื่อดู
วิธีใช้ใน Arduino IDE (ละเอียด)
1) ติดตั้งบอร์ด ESP32 (ครั้งเดียว)
- File → Preferences → ช่อง "Additional boards manager URLs" ใส่:
https://dl.espressif.com/dl/package_esp32_index.json - Tools → Board → Boards Manager… → ค้นหา
esp32→ ติดตั้ง esp32 by Espressif Systems
2) ติดตั้งไลบรารี (Sketch → Include Library → Manage Libraries…)
- ArduinoJson (Benoit Blanchon) — สร้าง/แยก JSON ใน payload
- OneWire (Paul Stoffregen) — โพรโทคอล 1-Wire
- DallasTemperature (Miles Burton) — เซนเซอร์ DS18B20
3) เลือกบอร์ดและตั้งค่า (สำคัญมาก)
| การตั้งค่า (Tools) | ค่า |
|---|---|
| Board | ESP32S3 Dev Module |
| Flash Size | 16MB (128Mb) |
| PSRAM | OPI PSRAM |
| USB CDC On Boot | Disabled (ใช้ UART Serial) |
| Partition Scheme | Custom (มีส่วน LittleFS สำหรับคิวอัปโหลด) |
| Upload Speed | 115200 |
| Port | พอร์ต COM ของบอร์ด (เช่น COM13) |
4) ต่อสายจริง (อิงจาก config.h)
| ขา ESP32-S3 | ต่อกับ | สาย | หมายเหตุ |
|---|---|---|---|
| GPIO4 | DS18B20 #1 (ภายใน) | DATA (เหลือง) | ต่อ 4.7kΩ pull-up DATA → 3V3 |
| GPIO5 | DS18B20 #2 (ภายนอก) | DATA (เหลือง) | ต่อ 4.7kΩ pull-up DATA → 3V3 |
| GPIO16 (UART RX) | โมดูล GNSS NEO-M8N · TX | TX→RX | TTL 3V3 · baud 38400 (auto) |
| GPIO17 (UART TX) | โมดูล GNSS NEO-M8N · RX | RX→TX | TTL 3V3 |
| 3V3 | VIN โมดูล GNSS + DS18B20 (VDD) | แดง | GNSS ห้าม ใช้ 5V |
| GND | GND ของทุกโมดูล (รวม ground) | ดำ | must share GND |
DS18B20: VDD=3V3, GND, DATA พร้อม 4.7kΩ ระหว่าง DATA กับ 3V3 (ต่อได้ 1 ตัว/ขา ตามโค้ด)
โมดูล GNSS ใช้ไฟ 3.3V และใช้ระดับสัญญาณ TTL 3.3V — ห้ามต่อ 5V หรือ RS-232 โดยตรง
5) ตั้งค่า config.h แล้วอัปโหลด
- เปิด
config.hเติมค่าจริง:WIFI_SSID,WIFI_PASSWORD,API_KEY(key ของ device นี้) - กด Upload (→) แล้วเปิด Serial Monitor baud 115200
- เห็น log:
LOCAL=… collecting=1 fix=1 … sats=20 … WiFi=1แปลว่ารันปกติ (อัปโหลดทุก 5 นาที = INTERVAL_SECONDS 300)
Request Format (JSON Schema v1.0)
| field | กลุ่ม | type | required | ต้องมีค่า | constraint | unit | ตัวอย่าง | ที่มา |
|---|
ตัวอย่าง payload
Response Format
Success envelope
HTTP 200/201
{
"success": true,
"status": 200,
"message": "ok",
"data": { ... }
}
Error envelope
HTTP 4xx/5xx
{
"success": false,
"status": 422,
"error": {
"code": "INVALID_TIMESTAMP",
"message": "คำอธิบาย",
"field": "position.latitude" // optional
}
}
Status code ที่ใช้
| code | ความหมาย |
|---|---|
| 200 | OK (GET / duplicate POST) |
| 201 | Created — record ใหม่ถูกบันทึกแล้ว |
| 400 | Bad request / JSON ไม่ถูกต้อง |
| 401 | Unauthorized — API key ผิดหรือหายไป |
| 404 | Not found |
| 405 | Method not allowed |
| 409 | Conflict — uuid ซ้ำแต่ payload ขัดแย้ง |
| 413 | Payload too large |
| 422 | Unprocessable — validation failed |
| 429 | Too many requests (rate limit) |
| 500 | Internal server error |
รายละเอียดเพิ่มเติม
- ทุก response เป็น JSON:
Content-Type: application/json; charset=utf-8(ยกเว้น export-csv/excel ที่ดาวน์โหลดไฟล์) - POST 201 คืน
data = {"measurement_id": <id>, "duplicate": false}· POST 200 (duplicate) คืน{"measurement_id": …, "duplicate": true} messageมีในทั้ง success และ error envelopedataเป็น object{}ว่าง สำหรับ success ที่ไม่มีข้อมูลเพิ่มเติม- error
fieldมีเฉพาะ validation fail บางรายการ (เช่นposition.latitude) — ไม่ใช่ทุก error - ทุก response มี header rate limit:
X-RateLimit-Limit / Remaining / Reset· 429 เพิ่มRetry-After 404 Not found= เส้นทาง API ไม่มี (web server) ส่วน endpoint ที่ device ไม่มีข้อมูลจะตอบ200 NO_DATAไม่ใช่ 404- GET ที่ query ได้แต่ไม่มีข้อมูลตามช่วงจะยังตอบ
200โดยdata.device.status="NO_DATA"และdata.measurement=null(ไม่ใช่ error)
Authentication
- GET endpoints ไม่ต้องใช้ key (เปิดอ่านได้) — อาจเจอ rate limit GET 120/นาที/IP
- GET มีตัวเลือก
device(defaultGNSS01) ให้ระบุ device ที่ต้องการอ่าน - POST ต้องส่ง header
X-API-Key: <device API key>(ไม่รองรับAuthorization: Bearer) - Header อื่นที่ต้องใช้:
Content-Type: application/json(ผิด → 415INVALID_REQUEST) - Key ถูก hash ด้วย SHA-256 เก็บในตาราง
devices.api_key_hash(ไม่เก็บ plaintext) - key ผูกเฉพาะ device —
device_idใน payload ต้องตรงกับ device ของ key (ไม่ตรง → 422INVALID_DEVICE_ID) - key ถูกต้องแต่ device ถูกปิด (
enabled=0) →403 DEVICE_DISABLED - key ไม่มีในระบบ →
401 INVALID_API_KEY· ขาด header →401 MISSING_API_KEY - ทุกคำขอ POST ผ่าน auth ก่อนแล้วจึงนับ rate limit ต่อ device
วิธีได้ key
ลงทะเบียน device ในระบบ / request จากผู้ดูแลระบบ แล้วใส่ในช่อง API Key ด้านบนก่อน POST.
ลำดับการประมวลผล POST (gnss.php)
- Method ตรวจ (ต้อง POST)
- Auth ผ่าน X-API-Key (401/403 ก่อนอย่างอื่น)
- อ่าน payload JSON (grade: 400 INVALID_JSON / 413 too large / 415 wrong Content-Type)
- ตรวจ device_id ไม่ตรงกับ key → 422
- Rate limit POST 60/นาที/device (429 + Retry-After)
- Validate schema v1.0 (422 ต่อ field)
- Idempotency: uuid ซ้ำ timestamp ตรง → 200 duplicate · uuid ซ้ำ payload ต่าง → 409 · device+timestamp ซ้ำ → 409
- Transaction: insert measurement + satellites + last_seen → 201
Application error codes
| HTTP | code | ความหมาย | สาเหตุ | auto-retry | เก็บ payload | ลบ payload | แนวทางแก้ |
|---|
Rate limit (window 60 วินาที, นับด้วยตาราง api_request_logs)
- POST: 60 requests / นาที / device (นับหลัง auth ผ่าน แล้วค่อยตรวจ)
- GET: 120 requests / นาที / IP (ตรวจก่อนเข้า endpoint ทุกตัว)
- ทุก response (สำเร็จหรือ error) มี
X-RateLimit-Limit,X-RateLimit-Remaining,X-RateLimit-Reset(epoch seconds) - เกิน limit →
429 RATE_LIMIT_EXCEEDEDพร้อม headerRetry-After= วินาทีที่เหลือจนกว่าจะรีเซ็ต - นับรวมทุก endpoint/method เดียวกัน — เช่น เปิด tab Quick Test อัตโนมัติ 15 GETs กินโควตา GET ด้วย
- firmware ต้องอ่าน
Retry-Afterแล้ว schedule ใหม่ (backoff) ไม่รีส่งต่อเนื่อง
Automated test suite
อ่านข้อมูลแบบ read-only 15 cases + ถ้าใส่ API Key จะเพิ่ม 3 POST cases (รวม 18). หมายเหตุ: สำเร็จ = status < 500 (เช่น satellite-track ที่คืน 422 โดย design ยังถือว่า PASS).
| # | case | result | status | ms | detail |
|---|
API request logs
| time | method | endpoint | code | status | ms | size | ip | dup |
|---|
API usage charts (aggregate จาก api-logs)
Requests by endpoint
Requests by status
Requests over time (per hour)
API documentation
รวม endpoint ทั้งหมดของระบบ พร้อมพารามิเตอร์ GET (device เป็นค่าพื้นฐานที่ append ให้อัตโนมัติ)
| endpoint | params | หมายเหตุ |
|---|
ตัวอย่าง curl
# GET curl "https://project12569.theeeiot.com/EE67172310228/api/v1/latest.php?device=GNSS01" # POST curl -X POST "https://project12569.theeeiot.com/EE67172310228/api/v1/gnss.php" \ -H "Content-Type: application/json" \ -H "X-API-Key: <YOUR_DEVICE_KEY>" \ -d @payload.json