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FsV3JpdGUocmVsYXlQaW4sIExPVyk7IC8vIOC5gOC4m+C4tOC4lOC4meC5ieC4swogICAgc2VydmVyLnNlbmQoMjAwLCAmcXVvdDt0ZXh0L3BsYWluJnF1b3Q7LCAmcXVvdDtXYXRlciBpcyBPTiZxdW90Oyk7CiAgfSk7CgogIHNlcnZlci5vbigmcXVvdDsvY2xvc2UmcXVvdDssIEhUVFBfR0VULCBbXSgpIHsKICAgIGRpZ2l0YWxXcml0ZShyZWxheVBpbiwgSElHSCk7IC8vIOC4m+C4tOC4lOC4meC5ieC4swogICAgc2VydmVyLnNlbmQoMjAwLCAmcXVvdDt0ZXh0L3BsYWluJnF1b3Q7LCAmcXVvdDtXYXRlciBpcyBPRkYmcXVvdDspOwogIH0pOwoKICBzZXJ2ZXIub25Ob3RGb3VuZChbXSgpIHsKICAgIHNlcnZlci5zZW5kKDQwNCwgJnF1b3Q7dGV4dC9wbGFpbiZxdW90OywgJnF1b3Q7Tm90IGZvdW5kJnF1b3Q7KTsKICB9KTsKCiAgc2VydmVyLmJlZ2luKCk7CiAgU2VyaWFsLnByaW50bG4oJnF1b3Q7SFRUUCBzZXJ2ZXIgc3RhcnRlZCZxdW90Oyk7Cn0KCnZvaWQgbG9vcCgpIHsKICBzZXJ2ZXIuaGFuZGxlQ2xpZW50KCk7Cn0=
#include <WiFi.h>
#include <WebServer.h>
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
WebServer server(80);
const int relayPin = 5; // พินที่เชื่อมต่อกับรีเลย์
void setup() {
Serial.begin(115200);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH); // ปิดน้ำเริ่มต้น
// เชื่อมต่อกับ Wi-Fi
WiFi.begin(ssid, password);
Serial.print("Connecting to Wi-Fi...");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi");
// กำหนด endpoint ให้กับ API
server.on("/open", HTTP_GET, []() {
digitalWrite(relayPin, LOW); // เปิดน้ำ
server.send(200, "text/plain", "Water is ON");
});
server.on("/close", HTTP_GET, []() {
digitalWrite(relayPin, HIGH); // ปิดน้ำ
server.send(200, "text/plain", "Water is OFF");
});
server.onNotFound([]() {
server.send(404, "text/plain", "Not found");
});
server.begin();
Serial.println("HTTP server started");
}
void loop() {
server.handleClient();
}