Hi, everyone I'm a beginner in programming and would like to make an esp32 webserver to control LED.
I need webserver properties below
- authentication login before redirecting to the control page.
- admin page to change password and also can access control page
I have completed this code but it can access only one person .
#include <WiFi.h>
const char* ssid = "ESP32";
const char* password = NULL;
WiFiServer server(80);
String header;
String output26State = "off";
String output27State = "off";
const int output26 = 26;
const int output27 = 27;
//RTC_DATA_ATTR int bootCount = 0;
unsigned long currentTime = millis();
unsigned long previousTime = 0;
const long timeoutTime = 2000;
const char* base64Encoding = "YWRtaW46MTExMTE="; // base64encoding :11111 - "YWRtaW46MTExMTE="
void setup() {
Serial.begin(115200);
delay(1000);
//++bootCount;
Serial.println("Connecting to : ");
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);
Serial.print("IP Address : ");
Serial.println(WiFi.softAPIP());
pinMode(output26, OUTPUT);
pinMode(output27, OUTPUT);
digitalWrite(output26, LOW);
digitalWrite(output27, LOW);
server.begin();
}
void loop(){
WiFiClient client = server.available();
if (client) {
currentTime = millis();
previousTime = currentTime;
Serial.println("New Client.");
String currentLine = "";
while (client.connected() && currentTime - previousTime <= timeoutTime) {
currentTime = millis();
if (client.available()) {
char c = client.read();
Serial.write(c);
header += c;
if (c == '\n') {
if (currentLine.length() == 0) {
if (header.indexOf(base64Encoding)>=0)
{
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
if (header.indexOf("/admin/change_password") >= 0) {
String new_password = ""; // initialize a new password string
// read the new password from the request header
int start_pos = header.indexOf("password=") + 9;
int end_pos = header.indexOf("HTTP/1.1") - 1;
new_password = header.substring(start_pos, end_pos);
// update the password variable with the new password
password = new_password.c_str();
// send a success message back to the client
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
client.println("<html><body>Password updated successfully.</body></html>");
client.println();
break;}
if (header.indexOf("GET /26/on") >= 0) {
Serial.println("GPIO 26 on");
output26State = "on";
digitalWrite(output26, HIGH);
} else if (header.indexOf("GET /26/off") >= 0) {
Serial.println("GPIO 26 off");
output26State = "off";
digitalWrite(output26, LOW);
}
else if (header.indexOf("GET /27/on") >= 0) {
Serial.println("GPIO 27 on");
output27State = "on";
digitalWrite(output27, HIGH);
} else if (header.indexOf("GET /27/off") >= 0) {
Serial.println("GPIO 27 off");
output27State = "off";
digitalWrite(output27, 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: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #555555;}</style></head>");
client.println("<body><h1>ESP32 Web Server</h1>");
client.println("<p>GPIO 26 - State " + output26State + "</p>");
if (output26State=="off") {
client.println("<p><a href=\"/26/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/26/off\"><button class=\"button button2\">OFF</button></a></p>");
}
client.println("<p>GPIO 27 - State " + output27State + "</p>");
if (output27State=="off") {
client.println("<p><a href=\"/27/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/27/off\"><button class=\"button button2\">OFF</button></a></p>");
}
client.println("</body></html>");
client.println();
break;
}
else{
client.println("HTTP/1.1 401 Unauthorized");
client.println("WWW-Authenticate: Basic realm=\"Secure\"");
client.println("Content-Type: text/html");
client.println();
client.println("<html>Authentication failed</html>");
}
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
}
}
header = "";
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
and I have try to make admin page but I cant connect to wifi and stuck in the result " connecting to wifi.. "
#include <WiFi.h>
#include <WebServer.h>
WebServer server(80);
const char* ssid = "esp32";
const char* password = "";
const char* admin_username = "admin";
const char* admin_password = "12345";
bool isLoggedIn = false;
void handleRoot() {
if (isLoggedIn) {
String html = "<h1>Welcome Admin!</h1><br><a href='/https/forum.arduino.cc/logout'>Logout</a>";
server.send(200, "text/html", html);
} else {
String html = "<h1>Welcome!</h1><br><form method='POST' action='/login'>\
Username: <input type='text' name='username'><br>\
Password: <input type='password' name='password'><br>\
<input type='submit' value='Login'></form>";
server.send(200, "text/html", html);
}
}
void handleLogin() {
String username = server.arg("username");
String password = server.arg("password");
if (username == admin_username && password == admin_password) {
isLoggedIn = true;
server.sendHeader("Location", "/");
server.send(302);
} else {
String html = "<h1>Unauthorized</h1><br><a href='/'>Back to Home</a>";
server.send(401, "text/html", html);
}
}
void handleLogout() {
isLoggedIn = false;
server.sendHeader("Location", "/");
server.send(302);
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, password);
Serial.print("IP Address: ");
Serial.println(WiFi.softAPIP());
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("WiFi connected");
server.on("/", handleRoot);
server.on("/login", HTTP_POST, handleLogin);
server.on("/logout", handleLogout);
server.begin();
Serial.println("Server started");
}
void loop() {
server.handleClient();
}
could anyone help me, please...
best regard.