กำลังโหลด...

📡 SmartFarm API

API สำหรับอุปกรณ์เซนเซอร์ส่งข้อมูลเข้าระบบ (ESP8266, ESP32, Arduino ฯลฯ)
ทุก request ต้องแนบ apiKey ทุกครั้ง

v1.0
Base URL https://<region>-<project-id>.cloudfunctions.net
POST /sensorData ส่งข้อมูลเซนเซอร์เข้าระบบ
คำอธิบาย

รับข้อมูลจากอุปกรณ์เซนเซอร์และบันทึกลงฐานข้อมูล
ระบบจะค้นหา Sensor UID เพื่อระบุโรงเรือนที่ผูกไว้ แล้วบันทึก ทั้ง ค่าล่าสุด (current) และ ประวัติ (history) โดยอัตโนมัติ

Request Body (Content-Type: application/json)
Field Type Required คำอธิบาย
uid string required UID ของเซนเซอร์ที่ลงทะเบียนในระบบ (ดูได้จากหน้า เซนเซอร์)
apiKey string required API Key สำหรับยืนยันตัวตน กำหนดไว้ใน functions/.env
temperature number optional อุณหภูมิ (°C) เช่น 28.5
humidity number optional ความชื้นสัมพัทธ์ (%) เช่น 65.0
light number optional ความเข้มแสง (lux) เช่น 1200
หมายเหตุ: ส่งได้หลาย field พร้อมกัน เช่น temperature + humidity ในคราวเดียว
ส่ง field ใดไม่ส่ง field นั้นจะไม่ถูกบันทึก (ไม่ overwrite ค่าเดิม)
ตัวอย่าง Request Body
{ "uid": "ABC123xyz", "apiKey": "your-secret-api-key", "temperature": 28.5, "humidity": 65.2 }
Response Codes
200
OK — บันทึกข้อมูลสำเร็จ
{ "success": true, "uid": "ABC123xyz", "greenhouse": "gh-id-xxx", "data": { "uid": "ABC123xyz", "sensorName": "เซนเซอร์ A", "sensorType": "temperature", "temperature": 28.5, "humidity": 65.2, "timestamp": 1700000000000 } }
400
Bad Request — ไม่มี uid หรือเซนเซอร์ไม่ได้ผูกโรงเรือน
{"error": "Missing required field: uid"}
{"error": "Sensor is not linked to any greenhouse."}
403
Forbidden — apiKey ไม่ถูกต้อง
{"error": "Invalid API key."}
404
Not Found — ไม่พบ UID ในระบบ
{"error": "Sensor UID not found: ABC123xyz"}
405
Method Not Allowed — ใช้ได้เฉพาะ POST
{"error": "Method not allowed. Use POST."}
ตัวอย่างโค้ด
ESP8266 / Arduino
ESP32 (WiFiClient)
cURL
Python
// ต้องติดตั้ง library: ESP8266WiFi, ESP8266HTTPClient #include <ESP8266WiFi.h> #include <ESP8266HTTPClient.h> #include <ArduinoJson.h> const char* ssid = "YOUR_WIFI_SSID"; const char* password = "YOUR_WIFI_PASSWORD"; const char* apiUrl = "https://<region>-<project>.cloudfunctions.net/sensorData"; const char* apiKey = "YOUR_SECRET_KEY"; const char* sensorUid = "YOUR_SENSOR_UID"; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); } } void sendSensorData(float temp, float humid) { if (WiFi.status() != WL_CONNECTED) return; WiFiClientSecure client; client.setInsecure(); // สำหรับ HTTPS HTTPClient http; http.begin(client, apiUrl); http.addHeader("Content-Type", "application/json"); StaticJsonDocument<200> doc; doc["uid"] = sensorUid; doc["apiKey"] = apiKey; doc["temperature"] = temp; doc["humidity"] = humid; String body; serializeJson(doc, body); int code = http.POST(body); Serial.println(String("HTTP: ") + code); http.end(); } void loop() { sendSensorData(28.5, 65.0); // แทนด้วยค่าจาก sensor จริง delay(30000); // ส่งทุก 30 วินาที }
// ESP32 — ใช้ WiFiClientSecure #include <WiFi.h> #include <HTTPClient.h> #include <WiFiClientSecure.h> #include <ArduinoJson.h> const char* ssid = "YOUR_WIFI_SSID"; const char* password = "YOUR_WIFI_PASSWORD"; const char* apiUrl = "https://<region>-<project>.cloudfunctions.net/sensorData"; const char* apiKey = "YOUR_SECRET_KEY"; const char* sensorUid = "YOUR_SENSOR_UID"; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); } } void sendData(float temp, float humid, float light) { WiFiClientSecure client; client.setInsecure(); HTTPClient http; http.begin(client, apiUrl); http.addHeader("Content-Type", "application/json"); StaticJsonDocument<256> doc; doc["uid"] = sensorUid; doc["apiKey"] = apiKey; doc["temperature"] = temp; doc["humidity"] = humid; doc["light"] = light; String body; serializeJson(doc, body); int code = http.POST(body); Serial.printf("HTTP %d\n", code); http.end(); } void loop() { sendData(28.5, 65.0, 1200); delay(30000); }
curl -X POST \ "https://<region>-<project>.cloudfunctions.net/sensorData" \ -H "Content-Type: application/json" \ -d '{ "uid": "YOUR_SENSOR_UID", "apiKey": "YOUR_SECRET_KEY", "temperature": 28.5, "humidity": 65.0, "light": 1200 }'
import requests url = "https://<region>-<project>.cloudfunctions.net/sensorData" apiKey = "YOUR_SECRET_KEY" payload = { "uid": "YOUR_SENSOR_UID", "apiKey": apiKey, "temperature": 28.5, "humidity": 65.0, "light": 1200, } resp = requests.post(url, json=payload) print(resp.status_code, resp.json())
การบันทึกข้อมูล

เมื่อส่งข้อมูลสำเร็จ ระบบจะบันทึกลง Firebase Realtime Database 2 ตำแหน่ง:

// ค่าล่าสุด (overwrite ทุกครั้ง) sensors/{greenhouseId}/current/{sensorUid} // ประวัติ (push ทุกครั้ง ไม่ลบ) sensors/{greenhouseId}/history/{sensorUid}/{pushKey}