This project allows students to control a relay connected to an ESP32 through a web interface. The interface is accessible over a local Wi-Fi network and provides a toggle switch to turn the relay ON or OFF. The relay can be used to control a light, pump, or any low-voltage device.
ESP32 GPIO27 —> IN (Relay)
ESP32 GND —> GND
ESP32 3.3V/5V —> VCC (Relay)
from machine import Pin
import socket, network
# Wi-Fi credentials
ssid = 'YOUR_WIFI_SSID'
password = 'YOUR_WIFI_PASSWORD'
# Connect to Wi-Fi
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while not wlan.isconnected():
pass
ip = wlan.ifconfig()[0]
print('Web server running on:', ip)
# Relay setup
relay = Pin(27, Pin.OUT)
relay.off()
# HTML + CSS + JS
html = """<!DOCTYPE html>
<html>
<head>
<title>Relay Control</title>
<style>
body { font-family: Arial; background: #111; color: white; text-align: center; padding-top: 50px; }
.switch { position: relative; display: inline-block; width: 80px; height: 34px; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0;
background-color: #ccc; transition: .4s; border-radius: 34px; }
.slider:before { position: absolute; content: ""; height: 26px; width: 26px; left: 4px; bottom: 4px;
background-color: white; transition: .4s; border-radius: 50%; }
input:checked + .slider { background-color: #2196F3; }
input:checked + .slider:before { transform: translateX(46px); }
</style>
</head>
<body>
<h1>Relay Switch</h1>
<label class="switch">
<input type="checkbox" id="relayToggle" onchange="toggleRelay()">
<span class="slider"></span>
</label>
<script>
async function toggleRelay() {
const state = document.getElementById('relayToggle').checked ? 'on' : 'off';
await fetch('/relay?state=' + state);
}
</script>
</body>
</html>"""
# Web server
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
while True:
cl, addr = s.accept()
request = cl.recv(1024).decode()
if '/relay?state=on' in request:
relay.on()
elif '/relay?state=off' in request:
relay.off()
if 'GET /' in request or 'GET /relay' in request:
cl.send('HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n')
cl.send(html)
cl.close()
You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.
You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.
You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.
You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.