Skip to content
/ cplug Public

This project transforms an ESP32 microcontroller into a smart plug, controllable via a relay. It features a web interface for network-based API control and an I2C LCD for real-time feedback like connection status and control URLs.

License

Notifications You must be signed in to change notification settings

OGD09/cplug

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CPLUG

This project is a smart plug powered by an ESP32 microcontroller, which controls a relay to switch connected devices on or off. The relay can be controlled through a simple and intuitive web interface, using API calls over the network. The device also displays relevant information on an I2C LCD screen and includes a physical button for direct control of the relay, ensuring usability even without network access.

Assembled Device

diagram

Assembled Device Device's guts

Features

  1. Wi-Fi Modes:

    • Connects to an existing Wi-Fi network using stored credentials.
    • Creates an Access Point (AP) if no network credentials are available.
  2. Dynamic LCD Display:

    • Displays the AP SSID and password when in AP mode without clients.
    • Shows the URL for relay control when a client connects to the AP or when connected to Wi-Fi.
  3. Physical Button Control:

    • Allows direct control of the relay.
    • Button press takes priority over API or web-based control.
  4. Web Interface:

    • Responsive relay control using modern AJAX (XHR).
    • Displays the current relay state without page refreshes.
  5. Non-Volatile Storage:

    • Saves Wi-Fi credentials using the ESP32's Preferences library.
  6. Custom 3D-Printed Case:

    • A 3D-printable STL file for the case is included in the assets folder.

Requirements

Software

  • Arduino IDE or Arduino CLI
  • ESP32 Board Support Package
  • Required Libraries:
    • WiFi.h
    • WebServer.h
    • Preferences.h
    • esp_wifi.h
    • LiquidCrystal_I2C.h

Hardware

  • ESP32 Microcontroller
  • I2C LCD Display: 16x2 with an I2C interface.
  • Relay Module
  • Self-Lock Push Button
  • Voltage Converter: Converts 110V-240V AC to 5V DC for the ESP32.
  • NEMA 5-15R Socket: A standard US receptacle for the relay-controlled device.
  • IEC 320 C14 Socket (AS-05, AC 250 V, 10 A): Common power inlet for devices.

Note: See the Hardware section below for links to the components used.

Installation

  1. Clone this repository or copy the source code to your local machine.
  2. Open the project in Arduino IDE or place it in a suitable directory for the Arduino CLI.
  3. Install the required libraries:
    • Use the Arduino Library Manager or Arduino CLI.
    • Ensure the ESP32 board package is installed.
  4. Connect your ESP32 and upload the code.

Usage

  1. Initial Configuration:

    • When powered on for the first time, the ESP32 will start in AP mode. The LCD will display the SSID and password for the AP.
    • Connect to the AP, and a captive portal will automatically open, guiding you to the network configuration page.
    • Navigate through the captive portal to configure Wi-Fi credentials.
  2. Relay Control:

    • Access the web interface at the URL displayed on the LCD to toggle the relay state.
    • Press the physical button to control the relay directly. The button press overrides web control.
  3. LCD Display:

    • Shows the current mode and necessary connection details:
      • AP mode without clients: SSID and password.
      • AP mode with clients: URL for relay control.
      • Wi-Fi connected: URL for relay control.

Hardware

Component Description Link
ESP32 Microcontroller The heart of the project. Link
I2C LCD Display (16x2) Displays connection details and status. Link
Relay Module Controls the electrical load. Link
Self-Lock Push Button Physical control of the relay. Link
Voltage Converter (110V-240V to 5V) Powers the ESP32 from AC mains. Link
NEMA 5-15R Socket Standard US receptacle for connected devices. Link
IEC 320 C14 Socket (AS-05) Power inlet for AC connections. Link
M3 Inserts M3 insert for lid locking. Link
M3 screws M3 screws. Link

Custom Case

  • A 3D-printable case designed for this project is available in the assets folder.
  • The STL file can be printed with standard PLA or ABS material without support.

Example Web Interface

  • Relay State: Displays whether the relay is ON or OFF.
  • Toggle Button: Instantly toggles the relay state.
  • Uses modern AJAX (XHR) for a seamless user experience.

Known Issues and Future Improvements

  1. Button Behavior:

    • Relay state may not update on the web interface while the button is pressed. This behavior is by design to prioritize physical control.
  2. Suggestions:

    • Add password protection to the web interface for enhanced security.
    • Include OTA (Over-the-Air) updates for easier firmware management.

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

This project transforms an ESP32 microcontroller into a smart plug, controllable via a relay. It features a web interface for network-based API control and an I2C LCD for real-time feedback like connection status and control URLs.

Topics

Resources

License

Stars

Watchers

Forks

Languages