Build Your Own Timer Plug – Automatic Mobile Charger Cut-Off Using ESP32 (Hotspot Mode)
Build Your Own Timer Plug – Automatic Mobile Charger Cut-Off Using ESP32 (Hotspot Mode)
By Dinesh D | DIY Electronics & Tech Projects | Nov 2025
⭐ Introduction
Mobile batteries degrade over time, especially when overcharged overnight. While commercial timer plugs exist, they are often costly and lack flexibility.
In this project, you will learn how to build a custom automatic timer plug using an ESP32 and a relay module. This timer plug switches OFF your charger automatically after a set duration — and the best part?
It works completely offline. No router. No internet. Only ESP32 hotspot mode.
The ESP32 creates its own WiFi Access Point. Once you connect your smartphone to it, you can open a simple webpage, enter the timer (HH:MM:SS), and the relay will turn OFF automatically when the time completes.
This is a perfect beginner-friendly IoT project with real-world usefulness.
๐ง How the Timer Plug Works
The ESP32 is programmed in Access Point (AP) mode, which means:
-
It broadcasts its own Wi-Fi network
-
Your phone connects to this Wi-Fi
-
You open a built-in webpage served by the ESP32
The workflow:
-
Connect to ESP32 hotspot (
Charger_AP) -
Open 192.168.4.1
-
Enter hours, minutes, and seconds
-
Press Start Charging
-
Relay turns ON → Charger gets power
-
When timer completes, relay turns OFF → Charging cuts off
This setup also works for lamps, fans, and other small appliances (within relay rating).
๐งฐ Components Required
| Component | Quantity |
|---|---|
| ESP32 Development Board | 1 |
| 5V Relay Module (recommended: opto-isolated) | 1 |
| AC Plug + Socket | 1 set |
| USB Cable (ESP32 power) | 1 |
| Jumper Wires | Few |
| Project Box / Enclosure | Optional |
| Tools: Screwdriver, wire stripper, soldering iron | As required |
๐ Circuit & Wiring – ESP32 to Relay
๐น Low Voltage (DC) Side
| Relay Pin | ESP32 Pin |
|---|---|
| VCC | 5V |
| GND | GND |
| IN | GPIO 5 |
๐น High Voltage (AC Mains) Side
⚠️ Only proceed if familiar with AC safety.
-
Mains Live (Hot) → Relay COM
-
Relay NO (Normally Open) → Live to charger
-
Neutral → Direct to charger (not switched)
-
Earth → Earth to socket (if present)
๐ Always keep AC and DC sides physically separated and inside an insulated enclosure.
๐ ESP32 Program (Hotspot + Timer + Auto Cut-Off)
Copy this to Arduino IDE, select your ESP32 board, and upload.
#include <WiFi.h>
#include <WebServer.h>
// Hotspot credentials (change if you want)
const char* ssid = "Charger_AP";
const char* password = "12345678";
WebServer server(80);
const int relayPin = 5; // GPIO5 -> Relay IN
unsigned long timerDuration = 0; // milliseconds
unsigned long startTime = 0;
bool isCharging = false;
// Simple HTML UI
String webPage = R"rawliteral(
<!DOCTYPE html><html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Timer Plug - Auto Cut Off</title>
<style>
body { font-family: Arial, sans-serif; text-align:center; padding:24px; }
h2 { color:#1a4d8f; }
.time { font-size:18px; margin:6px; width:80px; padding:8px; border-radius:6px; border:1px solid #ccc; }
button { padding:12px 20px; margin:10px; border:none; color:#fff; border-radius:6px; background:#1a4d8f; cursor:pointer; }
.stop { background:#d9534f; }
</style>
</head>
<body>
<h2>⏱️ Mobile Charger Auto Cut-Off</h2>
<form action="/start">
<input class="time" type="number" name="hours" placeholder="HH" min="0" max="24">
<input class="time" type="number" name="minutes" placeholder="MM" min="0" max="59">
<input class="time" type="number" name="seconds" placeholder="SS" min="0" max="59"><br>
<button type="submit">Start Charging</button>
</form>
<form action="/stop">
<button class="stop" type="submit">Stop Charging</button>
</form>
<p>Connect your phone to WiFi: <b>Charger_AP</b> (pwd: 12345678)<br>Open: <b>192.168.4.1</b></p>
</body>
</html>
)rawliteral";
void setup() {
Serial.begin(115200);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH); // Ensure relay is OFF initially (HIGH = OFF for many modules)
// Start hotspot
WiFi.softAP(ssid, password);
Serial.println("Access Point started");
Serial.print("IP: "); Serial.println(WiFi.softAPIP());
// Routes
server.on("/", []() {
server.send(200, "text/html", webPage);
});
server.on("/start", []() {
int h = server.hasArg("hours") ? server.arg("hours").toInt() : 0;
int m = server.hasArg("minutes") ? server.arg("minutes").toInt() : 0;
int s = server.hasArg("seconds") ? server.arg("seconds").toInt() : 0;
timerDuration = ((unsigned long)h * 3600UL + (unsigned long)m * 60UL + (unsigned long)s) * 1000UL;
startTime = millis();
digitalWrite(relayPin, LOW); // Turn relay ON (LOW for many modules)
isCharging = true;
Serial.printf("Started: %dh %dm %ds -> %lu ms\n", h, m, s, timerDuration);
server.send(200, "text/html", "<h2>Charging Started</h2><a href='/'>Back</a>");
});
server.on("/stop", []() {
digitalWrite(relayPin, HIGH); // Turn relay OFF
isCharging = false;
Serial.println("Stopped manually");
server.send(200, "text/html", "<h2>Charging Stopped</h2><a href='/'>Back</a>");
});
server.begin();
}
void loop() {
server.handleClient();
if (isCharging) {
unsigned long elapsed = millis() - startTime;
if (elapsed >= timerDuration) {
digitalWrite(relayPin, HIGH); // Auto cut-off
isCharging = false;
Serial.println("Timer complete - Relay OFF");
}
}
}
๐ก️ Safety Precautions
Working with 230V AC is dangerous. Always follow these rules:
✔ Use a relay rated for your mains voltage & current
✔ Insulate all AC connections
✔ Never touch live wires
✔ Keep ESP32 circuitry isolated
✔ If unsure, get help from an electrician
๐งช How to Test Your Timer Plug
1. Upload the program
Use Arduino IDE to upload the provided code into the ESP32.
2. Power the ESP32
Use a USB charger or power bank.
3. Connect Your Phone to ESP32 WiFi
| Setting | Value |
|---|---|
| WiFi Name: | Charger_AP |
| Password: | 12345678 |
Turn OFF mobile data if needed.
4. Open the Control Page
After connecting to ESP32 WiFi, open your browser and go to:
๐ http://192.168.4.1
๐ Important:
This page will work only when your phone is connected to the ESP32 hotspot.
It will NOT open with:
❌ Mobile data
❌ Another WiFi network
❌ ESP32 powered off
5. Start a Test Timer
Enter something like:
Press Start Charging.
Expected behavior:
✔ Relay clicks ON → Charger gets power
✔ After 10 seconds, relay clicks OFF → Charging stops
This confirms your timer plug is functioning perfectly.
๐ Final Results & Benefits
Your completed timer plug:
✔ Works fully offline (no router required)
✔ Provides a clean web interface
✔ Prevents mobile overcharging
✔ Saves power and improves battery health
✔ Supports various appliances (within relay limit)
✔ Helps you learn practical IoT + ESP32 concepts
Comments
Post a Comment