esp8266 การควบคุม
// Load Wi-Fi library
#include <ESP8266WiFi.h>
//#include <WiFiClient.h>
#include <ESP8266WebServer.h>
const char* ssid = "…………"; //เพิ่มชื่อไวไฟ
const char* password = "................."; /เพิ่มระหัสผ่านของไวไฟ
ESP8266WebServer server(80); // Set web server port number to 80
// Variable to store the HTTP request
String header;
// Auxiliar variables to store the current output state
String output1State = "off";
String output2State = "off";
// Assign output variables to GPIO pins
const int output1 = D1;
const int output2 = D2;
#define output1_OFF digitalWrite(output1, LOW)
#define output1_ON digitalWrite(output1, HIGH)
#define output2_OFF digitalWrite(output2, LOW)
#define output2_ON digitalWrite(output2, HIGH)
//HTML code
const String HtmlHtml = "<html><head>"
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" /></head>";
const String HtmlHtmlClose = "</html>";
const String HtmlTitle = "<h1>ESP8266 WebServer control</h1><br/>\n"; /////เพิ่มหัวข้อเว็ปที่ต้องการ
const String HtmlCenter = "<center>";
const String HtmlCloseCenter = "</center>";
const String HtmlButton1_ON = "<a href=\"LED1Off\"><button style=\"display: block; background-color: #FF0000; height: 50px; width: 100px;\">ON</button></a><br/>";
const String HtmlButton1_OFF = "<a href=\"LED1On\"><button style=\"display: block; background-color: #00FF00; height: 50px; width: 100px;\">OFF</button></a><br/>";
const String HtmlButton2_ON = "<a href=\"LED2Off\"><button style=\"display: block; background-color: #FF0000; height: 50px; width: 100px;\">ON</button></a><br/>";
const String HtmlButton2_OFF = "<a href=\"LED2On\"><button style=\"display: block; background-color: #00FF00; height: 50px; width: 100px;\">OFF</button></a><br/>";
//String HtmlButtons; = "<a href=\"LEDOff\"><button style=\"display: block; background-color: #00FF00; height: 50px; width: 100px;\">OFF</button></a><br/>";
String HtmlLedState1, HtmlButtons1, HtmlLedState2, HtmlButtons2;
void handleRoot() {
output1_OFF;
HtmlLedState1 = "<big>LED is now <b>OFF</b></big><br/>\n";
HtmlButtons1 = HtmlButton1_OFF;
output2_OFF;
HtmlLedState2 = "<big>LED is now <b>OFF</b></big><br/>\n";
HtmlButtons2 = HtmlButton2_OFF;
response();
}
void handleLed1On() {
output1_ON;
HtmlLedState1 = "<big>LED is now <b>ON</b></big><br/>\n";
HtmlButtons1 = HtmlButton1_ON;
response();
}
void handleLed1Off() {
output1_OFF;
HtmlLedState1 = "<big>LED is now <b>OFF</b></big><br/>\n";
HtmlButtons1 = HtmlButton1_OFF;
response();
}
void handleLed2On() {
output2_ON;
HtmlLedState2 = "<big>LED is now <b>ON</b></big><br/>\n";
HtmlButtons2 = HtmlButton2_ON;
response();
}
void handleLed2Off() {
output2_OFF;
HtmlLedState2 = "<big>LED is now <b>OFF</b></big><br/>\n";
HtmlButtons2 = HtmlButton2_OFF;
response();
}
void response(){
String htmlRes = HtmlHtml + HtmlCenter;
htmlRes += HtmlTitle;
htmlRes += HtmlLedState1;
htmlRes += HtmlButtons1;
htmlRes += HtmlLedState2;
htmlRes += HtmlButtons2;
htmlRes += HtmlCloseCenter;
htmlRes += HtmlHtmlClose;
server.send (200, "text/html", htmlRes);
}
void setup() {
Serial.begin(115200);
// Initialize the output variables as outputs
pinMode(output1, OUTPUT);
pinMode(output2, OUTPUT);
output1_OFF;
output2_OFF;
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");เพิ่ม ip เครื่อง
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.on("/LED1On", handleLed1On);
server.on("/LED1Off", handleLed1Off);
server.on("/LED2On", handleLed2On);
server.on("/LED2Off", handleLed2Off);
server.begin();
}
void loop(){
server.handleClient();
}
ความคิดเห็น
แสดงความคิดเห็น