How do you turn an esp32 into a web server?
Did you know that you can turn an ESP32 into a mini web server? It's perfect, for example, for testing websites, or it can simply be used as a fun project.
The ESP32 is a microcontroller that stands out from others because it can connect to Wi-Fi. It can also create its own network (access point), and there is a very wide range of projects you can build with it. For example, it can send sensor data to a Telegram bot.
It can also be used for hacking-related experiments, but I recommend trying only educational projects that do not affect other websites, do not disrupt Wi-Fi networks, and do not cause any harm.
Unfortunately, this web server will only be accessible to devices connected to the same network as the ESP32. If you want to build a real web server, I recommend using an old computer. However, for a simple project, the ESP32 is perfect.
In the code below, you need to replace SSID with the name of the Wi-Fi network the ESP32 will connect to, and password with that network's password.
To access the website, you need a device connected to the same network. In the Serial Monitor, you should see the ESP32's IP address (in Arduino IDE, go to Tools → Serial Monitor or press Ctrl + Shift + M). Then, from that device, open a web browser and enter the IP address.
That's all. Simple, right?
You can find the code below. Good luck! 🚀
Code:
#include <WiFi.h>
const char* ssid = "SSID";
const char* password = "password";
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println();
Serial.print("Conectare la Wi-Fi / Connecting to Wi-Fi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
delay(500);
Serial.print(".");
attempts++;
}
Serial.println();
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Conectat la Wi-Fi / Connected to Wi-Fi");
Serial.print("IP ESP32: ");
Serial.println(WiFi.localIP());
} else {
Serial.println("Nu s-a putut conecta la Wi-Fi / Could not connect to Wi-Fi");
}
server.begin();
}
void loop() {
WiFiClient client = server.available();
if (client) {
String request = client.readStringUntil('\r');
client.flush();
// Conținut personalizat cu emoticoane pe site
String continut =
"Salut, aici ESP-ul tău te salută cu un zâmbet și câteva sfaturi utile! 😀<br>"
"Poți folosi acest server pentru a experimenta tot felul de proiecte IoT. 🚀<br>"
"De exemplu, să creezi mini-jocuri web, să monitorizezi date sau să testezi HTML direct din ESP! 😎";
// Pagina web cu stil și emoji
String html = "<!DOCTYPE html><html>";
html += "<head><meta charset='UTF-8'>";
html += "<title>ESP32 Web Server</title>";
html += "<style>";
html += "body { font-family: Arial, sans-serif; text-align: center; background-color: #f0f8ff; padding: 50px; }";
html += "h1 { color: #ff6600; }";
html += "p { color: #333333; font-size: 18px; line-height: 1.6; }";
html += "a { color: #0066cc; text-decoration: none; font-weight: bold; }";
html += "</style></head>";
html += "<body>";
html += "<h1>Salut de la ESP32! 😀</h1>";
html += "<p>" + continut + "</p>";
html += "<p>Pentru mai multe proiecte interesante accesați ";
html += "<a href='https://www.arduinolab.com' target='_blank'>www.arduinolab.com</a></p>";
html += "</body></html>";
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
client.println(html);
delay(1);
client.stop();
}
}
