คำตอบที่ได้รับเลือกจากเจ้าของกระทู้
ความคิดเห็นที่ 2
ไปเอามาจากไหน จะเอาไปทำอะไร
▼ กำลังโหลดข้อมูล... ▼
แสดงความคิดเห็น
คุณสามารถแสดงความคิดเห็นกับกระทู้นี้ได้ด้วยการเข้าสู่ระบบ
กระทู้ที่คุณอาจสนใจ
อ่านกระทู้อื่นที่พูดคุยเกี่ยวกับ
คอมพิวเตอร์เซิร์ฟเวอร์
C (ภาษาคอมพิวเตอร์)
วิศวกรรมคอมพิวเตอร์
C++
การพัฒนา Desktop Application
ใครพออธิบายโด้คนี้ได้บ้าง ไม่เก่งเรื่องโด้ค ขอบคุณ
#include <ESP8266WebServer.h>
const char* ssid = "WIFI_SSID";
const char* password = "WIFI_PASSWORD";
const IPAddress ip(192, 168, 1, 100);
const IPAddress subnet(255, 255, 255, 0);
const IPAddress gt(192, 168, 1, 1);
const int SwitchPin = 12; // GPIO12
int SwitchValue = HIGH;
ESP8266WebServer server(80);
void handleNotFound(){
//server.send(404, "text/plain", "404 Not Found");
if(SwitchValue == HIGH) {
server.send(200, "text/plain", "ON");
}else{
server.send(200, "text/plain", "OFF");
}
delay(1000);
}
void setup(void){
// Set Output PIN
pinMode(SwitchPin, OUTPUT);
digitalWrite(SwitchPin, SwitchValue);
// Connect WiFi
WiFi.mode(WIFI_STA);
WiFi.config(ip, gt, subnet);
WiFi.begin(ssid, password);
// Wait for connection
while(WiFi.status() != WL_CONNECTED){
delay(1000);
}
server.on("/", [](){
if(SwitchValue == HIGH) {
server.send(200, "text/plain", "ON");
}else{
server.send(200, "text/plain", "OFF");
}
delay(1000);
});
server.on("/ON", [](){
SwitchValue = HIGH;
server.send(200, "text/plain", "ON");
digitalWrite(SwitchPin, SwitchValue);
delay(1000);
});
server.on("/OFF", [](){
SwitchValue = LOW;
server.send(200, "text/plain", "OFF");
digitalWrite(SwitchPin, SwitchValue);
delay(1000);
});
server.onNotFound(handleNotFound);
server.begin();
}
void loop(void){
server.handleClient();
}