เริ่มต้นเนื้อหา
ควบคุมรีเลย์ผ่าน-WiFi จาก-Web ด้วย-ESP32 AP Mode
โค๊ดสำหรับประยุกต์ใช้งาน ProtoAutomation ESP32 AC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFi.h> | |
const char* ssid = "Automation"; | |
const char* password = "123456789"; | |
WiFiServer server(80); | |
String header; | |
String ch1state = "off"; | |
String ch2state = "off"; | |
String ch3state = "off"; | |
String ch4state = "off"; | |
const int ch1 = 26; | |
const int ch2 = 25; | |
const int ch3 = 33; | |
const int ch4 = 32; | |
void setup() { | |
Serial.begin(115200); | |
pinMode(ch1, OUTPUT); | |
pinMode(ch2, OUTPUT); | |
pinMode(ch3, OUTPUT); | |
pinMode(ch4, OUTPUT); | |
digitalWrite(ch1, LOW); | |
digitalWrite(ch2, LOW); | |
digitalWrite(ch3, LOW); | |
digitalWrite(ch4, LOW); | |
Serial.print("Setting AP (Access Point)…"); | |
WiFi.softAP(ssid, password); | |
IPAddress IP = WiFi.softAPIP(); | |
Serial.print("AP IP address: "); | |
Serial.println(IP); | |
server.begin(); | |
} | |
void loop(){ | |
WiFiClient client = server.available(); | |
if (client) { | |
Serial.println("New Client."); | |
String currentLine = ""; | |
while (client.connected()) { | |
if (client.available()) { | |
char c = client.read(); | |
Serial.write(c); | |
header += c; | |
if (c == '\n') { | |
if (currentLine.length() == 0) { | |
client.println("HTTP/1.1 200 OK"); | |
client.println("Content-type:text/html"); | |
client.println("Connection: close"); | |
client.println(); | |
if (header.indexOf("GET /26/on") >= 0) { | |
Serial.println("GPIO 26 on"); | |
ch1state = "on"; | |
digitalWrite(ch1, HIGH); | |
} else if (header.indexOf("GET /26/off") >= 0) { | |
Serial.println("GPIO 26 off"); | |
ch1state = "off"; | |
digitalWrite(ch1, LOW); | |
} | |
else if (header.indexOf("GET /25/on") >= 0) { | |
Serial.println("GPIO 25 on"); | |
ch2state = "on"; | |
digitalWrite(ch2, HIGH); | |
} else if (header.indexOf("GET /25/off") >= 0) { | |
Serial.println("GPIO 25 off"); | |
ch2state = "off"; | |
digitalWrite(ch2, LOW); | |
} | |
else if (header.indexOf("GET /33/on") >= 0) { | |
Serial.println("GPIO 33 on"); | |
ch3state = "on"; | |
digitalWrite(ch3, HIGH); | |
} else if (header.indexOf("GET /33/off") >= 0) { | |
Serial.println("GPIO 33 off"); | |
ch3state = "off"; | |
digitalWrite(ch3, LOW); | |
} | |
else if (header.indexOf("GET /32/on") >= 0) { | |
Serial.println("GPIO 32 on"); | |
ch4state = "on"; | |
digitalWrite(ch4, HIGH); | |
} else if (header.indexOf("GET /32/off") >= 0) { | |
Serial.println("GPIO 32 off"); | |
ch4state = "off"; | |
digitalWrite(ch4, LOW); | |
} | |
client.println("<!DOCTYPE html><html>"); | |
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"); | |
client.println("<link rel=\"icon\" href=\"data:,\">"); | |
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}"); | |
client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;"); | |
client.println("text-decoration: none; font-size: 25px; margin: 2px; cursor: pointer;}"); | |
client.println(".button2 {background-color: #555555;}</style></head>"); | |
client.println("<body><h1>Automation</h1>"); | |
if (ch1state=="off") { | |
client.print("<p><a href=\"/26/on\"><button class=\"button\">CH1 ON</button></a></p>"); | |
} else { | |
client.print("<p><a href=\"/26/off\"><button class=\"button button2\">CH1 OFF</button></a></p>"); | |
} | |
if (ch2state=="off") { | |
client.println("<p><a href=\"/25/on\"><button class=\"button\">CH2 ON</button></a></p>"); | |
} else { | |
client.println("<p><a href=\"/25/off\"><button class=\"button button2\">CH2 OFF</button></a></p>"); | |
} | |
if (ch3state=="off") { | |
client.println("<p><a href=\"/33/on\"><button class=\"button\">CH3 ON</button></a></p>"); | |
} else { | |
client.println("<p><a href=\"/33/off\"><button class=\"button button2\">CH3 OFF</button></a></p>"); | |
} | |
if (ch4state=="off") { | |
client.println("<p><a href=\"/32/on\"><button class=\"button\">CH4 ON</button></a></p>"); | |
} else { | |
client.println("<p><a href=\"/32/off\"><button class=\"button button2\">CH4 OFF</button></a></p>"); | |
} | |
client.println(""); | |
client.println("<body><h4>www.imiconsystem.com</h4>"); | |
client.println("</body></html>"); | |
client.println(); | |
break; | |
} else { | |
currentLine = ""; | |
} | |
} else if (c != '\r') { | |
currentLine += c; | |
} | |
} | |
} | |
header = ""; | |
client.stop(); | |
Serial.println("Client disconnected."); | |
Serial.println(""); | |
} | |
} |
Connecting wifi to…
SSID : Automation
PWD : 123456789
URL in this case, it is 192.168.4.1