1746087627

ESP32 Web Server for Home Control


Creating a web server on the ESP32 for home automation is a fantastic way to control your devices through any web browser. Let me walk you through how to set this up in a way that's both functional and user-friendly. ## <br>The Basics: How It Works When you power up your ESP32, it connects to your WiFi network and starts a small web server. This server listens for incoming requests and serves web pages that you can access from any device on the same network - your phone, laptop, or tablet. The magic happens when you click buttons on the web interface. Each button sends a command to the ESP32, which then controls your connected devices like lights, fans, or appliances through its GPIO pins. ## <br>Key Components You'll Need 1. **ESP32 Board** - The brains of the operation 2. **Relay Module** - To safely control high-power devices 3. **Basic Electronics** - Jumper wires, breadboard (optional) 4. **Arduino IDE** - For programming the ESP32 ## <br>Sample Code Implementation Here's a clean, functional example to get you started: ```cpp #include <WiFi.h> #include <WebServer.h> const char* ssid = "YourWiFiName"; const char* password = "YourWiFiPassword"; WebServer server(80); // Create server on port 80 // HTML content with simple styling const char* htmlContent = R"rawliteral( <!DOCTYPE html> <html> <head> <title>Home Control</title> <style> body { font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; } h1 { color: #2c3e50; } .btn { background-color: #3498db; color: white; border: none; padding: 10px 20px; margin: 5px; border-radius: 4px; cursor: pointer; } .btn-on { background-color: #2ecc71; } .btn-off { background-color: #e74c3c; } .status { font-weight: bold; color: #2c3e50; } </style> </head> <body> <h1>Home Control Panel</h1> <p>Welcome to your smart home control system.</p> <div> <h2>Living Room Light</h2> <p>Current status: <span class="status">%LIVINGROOM%</span></p> <button class="btn btn-on" onclick="controlDevice('livingroom', 'on')">Turn On</button> <button class="btn btn-off" onclick="controlDevice('livingroom', 'off')">Turn Off</button> </div> <script> function controlDevice(device, action) { fetch(`/control?device=${device}&action=${action}`) .then(response => location.reload()); } </script> </body> </html> )rawliteral"; void handleRoot() { String page = htmlContent; page.replace("%LIVINGROOM%", digitalRead(2) ? "ON" : "OFF"); server.send(200, "text/html", page); } void handleControl() { String device = server.arg("device"); String action = server.arg("action"); if (device == "livingroom") { if (action == "on") { digitalWrite(2, HIGH); } else if (action == "off") { digitalWrite(2, LOW); } } server.send(200, "text/plain", "OK"); } void setup() { pinMode(2, OUTPUT); digitalWrite(2, LOW); Serial.begin(115200); 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()); server.on("/", handleRoot); server.on("/control", handleControl); server.begin(); } void loop() { server.handleClient(); } ``` ## <br>Important Features to Note 1. **Responsive Design** - Works on mobile and desktop 2. **Visual Feedback** - Shows current device status 3. **Simple Color Coding** - Green for on, red for off 4. **Modern UI** - Clean buttons with rounded corners 5. **Efficient Updates** - Uses JavaScript fetch API for smooth control ## <br>Enhancing Your System For a more robust solution, consider adding: - **Password protection** for security - **More devices** by expanding the control logic - **Status updates** without page reload (using AJAX) - **Mobile app integration** by wrapping the web interface Remember to always exercise caution when controlling electrical devices. Use proper insulation and consider adding safety features like manual override switches.

(1) Comments
amargo85
amargo85
1746088026

what an interesting project. Is it possible to implement this in devices that are not connected to the wifi network? like air conditioning, lamps etc?


Welcome to Chat-to.dev, a space for both novice and experienced programmers to chat about programming and share code in their posts.

About | Privacy | Donate
[2025 © Chat-to.dev]