Home

Smart Switch Board

Project Overview

This project turns regular electrical switches into smart switches that can be controlled remotely. It combines hardware design, embedded programming, cloud connectivity, and mobile app development.

Users can control lights, fans, and other devices in two ways: using physical switches on the wall or through a Flutter mobile app. The system uses BLE (Bluetooth Low Energy) for WiFi setup, MQTT for cloud communication, and NVS storage to remember settings after power off.

My Approach

I built this project from scratch, handling everything from design to deployment:

Hardware Design & Development

CAD Modeling

I designed the physical parts using CAD software:

Perf Board Circuit Design

The circuit board includes:

Project Gallery

CAD Model of Smart Switch Button

CAD Model of the switch button and box

Circuit Diagram

Circuit diagram showing ESP32, relays, MOSFETs, and power supply

Perf Board Design

Perf board with ESP32, relay modules, and MOSFET drivers

Final Smart Switch Installation

Final product installed and working

App Demo video.

Demo video for button control.

ESP32 Firmware Architecture

Overview

The ESP32 firmware has these features:

Core Components

1. Device Management

The system controls 6 devices. Each device has:

2. BLE Provisioning System

BLE provisioning lets you set up WiFi without hardcoding it:

JSON Format: {"ssid":"MyWiFi","password":"12345678"}

3. MQTT Communication

MQTT is used to send and receive messages:

Commands you can send: ON, OFF, TOGGLE, STATUS

4. NVS Storage

These settings are saved even after power off:

5. GPIO & Button Handling

Physical buttons work like this:

6. Heartbeat System

Every 5 seconds, the board sends this JSON:

{
    "board": "esp_12ab34",
    "devices": [
        {"id": "light_1", "name": "Living Room", "type": "light"},
        {"id": "fan_1", "name": "Bedroom Fan", "type": "fan"},
        ...
    ]
}

This keeps the app updated with the latest status.

7. Renaming System

You can rename the board or devices using MQTT:

The system checks the name is valid, saves it to NVS, updates MQTT topics, and confirms the change.

How the Firmware Boots Up

  1. Initialize NVS: Start the storage system
  2. Initialize Network: Set up WiFi and event handling
  3. Check Setup Status:
    • Read the setup flag from NVS
    • Load WiFi credentials and board ID
  4. Load Device Names: Get custom names from NVS
  5. If Setup is Done:
    • Show board and device info
    • Set up button GPIO with interrupts
    • Set up LED GPIO as outputs
    • Connect to WiFi
    • Start MQTT client
    • Subscribe to command topics
    • Start sending heartbeat messages
  6. If Setup is NOT Done:
    • Use default board ID
    • Start BLE provisioning
    • Wait for phone app to send WiFi details

Flutter Mobile App

The mobile app is built with Flutter and lets you:

The app was "vibe coded" - built quickly with focus on making it easy to use.

Technical Details

WiFi & Network

MQTT Setup

Security

Power Management

Technologies & Tools

ESP32 ESP-IDF MQTT TLS/SSL BLE NVS FreeRTOS cJSON Flutter Dart CAD Software Relays MOSFETs Cloud MQTT Broker

Challenges & Solutions

Challenge 1: High-Voltage Safety

Problem: Need to control 220V AC using 3.3V signals from ESP32 without danger

Solution: Used optocoupled relays and MOSFETs to completely isolate low voltage from high voltage circuits

Challenge 2: Easy WiFi Setup

Problem: Users shouldn't need to reprogram the device to change WiFi

Solution: Built BLE provisioning - the phone app sends WiFi details via Bluetooth, ESP32 saves them to NVS

Challenge 3: Keeping Everything in Sync

Problem: App, physical buttons, and remote commands need to stay synchronized

Solution: Used MQTT pub/sub - every state change publishes a status update. Heartbeat messages every 5 seconds keep everything synced

Challenge 4: Renaming Devices

Problem: Allow renaming while keeping MQTT connections working

Solution: When renaming happens, unsubscribe old topics, save new name to NVS, then subscribe to new topics

Challenge 5: Button Bounce

Problem: Physical buttons send multiple signals when pressed once

Solution: Added software debouncing and used a task queue to handle button presses reliably

Challenge 6: Learning Flutter

Problem: Limited Flutter experience but needed working app fast

Solution: "Vibe coded" it - built basic features first, then improved the UI based on testing

What I Learned

Future Improvements