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:

  1. Connect to ESP32 hotspot (Charger_AP)

  2. Open 192.168.4.1

  3. Enter hours, minutes, and seconds

  4. Press Start Charging

  5. Relay turns ON → Charger gets power

  6. 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

ComponentQuantity
ESP32 Development Board1
5V Relay Module (recommended: opto-isolated)1
AC Plug + Socket1 set
USB Cable (ESP32 power)1
Jumper WiresFew
Project Box / EnclosureOptional
Tools: Screwdriver, wire stripper, soldering ironAs required

๐Ÿ”Œ Circuit & Wiring – ESP32 to Relay

๐Ÿ”น Low Voltage (DC) Side

Relay PinESP32 Pin
VCC5V
GNDGND
INGPIO 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

SettingValue
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:

00 : 00 : 10

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

Popular posts from this blog

๐ŸŽฏ Build Your Own RFID Attendance System Using Arduino + ESP32 + Google Sheets

How to make a Rfid id tag to turn on the Motor

Restoring an RC Car from E-Waste – DIY Bluetooth-Controlled RC Car Using Arduino