//Khai báo thư viện để kết nối wifi cho esp32 webserver
#include <WiFi.h>
//Khai báo GPIO sử dụng LED
const int led1 = 12;
const int led2 = 13;
const char* ssid = "Gia Vi";
const char* password = "1234554321";
//Tao mot web server tai cong 80 - cong mặc dinh cho esp32 webserver
WiFiServer webServer(80);
String led1Status = "OFF";
String led2Status = "OFF";
String header;
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
void setup()
{
Serial.begin(115200); // Khởi tạo kết nối UART
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
// Set outputs to LOW - Mạc định LED không sáng
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
Serial.print("Connecting to wifi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
//Khởi tạo webserver
webServer.begin();
}//
void loop()
{
WiFiClient webClient = webServer.available(); // Neu co client kết noi với esp32 webserver
if (webClient)
{
//Khoi tao gia tri ban dau cho time
currentTime = millis();
previousTime = currentTime;
Serial.println("New web Client");
//Biến lưu giá trị response
String currentLine = "";
//Nếu có client connect và không quá thời gian time out
while(webClient.connected() && currentTime - previousTime <= timeoutTime)
{
//Đọc giá trị timer tại thời điểm hiện tại
currentTime = millis();
//Nếu client còn kết nối
if (webClient.available())
{
//Đọc giá trị truyền từ client theo từng byte kiểu char
char c = webClient. read();
Serial.write(c);
header += c;
if (c == '\n') // Nếu đọc được kí tự xuống dòng (hết chuỗi truyền tới)
{
if(currentLine.length()==0)
{
webClient.println("HTTP/1.1 200 OK");
webClient.println("Content-type:text/html");
webClient.println("Connection: close");
webClient.println();
// Nếu trong file header có giá trị
if (header.indexOf("GET /led1/on") >= 0)
{
Serial.println("Led1 on");
led1Status = "on"; // bật sáng led
digitalWrite(led1, HIGH);
}
else if (header.indexOf("GET /led1/off") >= 0)
{
Serial.println("Led1 off");
led1Status ="off"; // tắt led
digitalWrite(led1, LOW);
}
else if (header.indexOf("GET /led2/on") >= 0) {
Serial.println("Led2 on");
led2Status = "on"; // bật sáng led
digitalWrite(led2, HIGH);
}
else if (header.indexOf("GET /led2/off") >= 0) {
Serial.println("Led2 off");
led2Status = "off"; // tắt led
digitalWrite(led2, LOW);
}
// Response trang HTML cua esp32 webserver
webClient.println(" <! DOCTYPE html><html>");
webClient.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
//css cho toan bo trang
// Web Page Heading H1 with CSS
webClient.println("<body><h1 style=\"color: Tomato; \">ESP32 Station Web Server</h1>");
webClient.println("<body><h1 style=\"color: Tomato; \">LTGTTB K20</h1>");
// Display current state, and ON/OFF buttons for Led1
webClient.println("<p>Led1 - State " + led1Status + "</p>");
// If the Led1Status is off, it displays the ON button
if (led1Status == "off")
{
//Khởi tạo một nút nhấn có đường · dẫn đích là /led1/on
webClient.println("<p><a href=\"/led1/on\"><button class=\"button\">ON</button></a></p>");
}
else
{
//Khởi tạo một nút nhấn có đường dẫn đích là /led1/off
webClient.println("<p><a href=\"/led1/off\"><button class=\"button button2\">OFF</button></a></p>");
}
// Display current state, and ON/OFF buttons for Led1
webClient.println("<p>Led2 - State " + led2Status + "</p>");
if (led2Status == "off") {
//Khởi tạo một nút nhấn có đường · dẫn đích là /led1/on
webClient.println("<p><a href=\"/led2/on\"><button class=\"button\">ON</button></a></p>");
} else {
//Khởi tạo một nút nhấn có đường dẫn đích là /led1/off
webClient.println("<p><a href=\"/led2/off\"><button class=\"button button2\">OFF</button></a></p>");
}
webClient.println("</body><html>");
webClient.println();
break;
}
else
{
currentLine="";
}
}
else if(c!='\r')
{
currentLine += c; //Lưu giá trị vào biến
}
}
}
// Xoá header để sử dụng cho lần tới
header = "";
// ngắt kết nối với client sử dụng cho lần tới trên esp32 webserver
webClient.stop();
Serial.println("Client disconnected");
Serial.println("");
}
}