Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Wi-Fi Controlled Smart Light with ESP32 and Relay Module

View and Edit Project In Cirkit DesignerImage of Wi-Fi Controlled Smart Light with ESP32 and Relay Module

Circuit Documentation

Summary

This circuit is designed to control an AC bulb and an LED filament using an ESP32 microcontroller. The circuit includes a relay module to switch the AC bulb, a resistor to limit current to the LED filament, and switches for manual control. The ESP32 is programmed to connect to a WiFi network and can be controlled remotely via the SinricPro platform.

Component List

  1. AC Bulb

    • Description: Standard AC bulb.
    • Pins: P (Phase), N (Neutral)
  2. LED Filament 3V

    • Description: 3V LED filament.
    • Pins: GND, Vcc
  3. 1 Channel 5V Relay Module

    • Description: Relay module for switching high voltage devices.
    • Pins: VCC+, VCC- (GND), IN, N.O. (Normally Open), COM (Common), N.C. (Normally Closed)
  4. Resistor

    • Description: 75 Ohms resistor.
    • Pins: pin1, pin2
    • Properties: Resistance: 75 Ohms
  5. 5V Adapter

    • Description: Adapter to convert 240V AC to 5V DC.
    • Pins: AC In 1, AC In 2, 5V, GND
  6. Flush Switch

    • Description: Flush-mounted switch.
    • Pins: GND, VCC
  7. Rocker Switch (SPST)

    • Description: Single Pole Single Throw (SPST) rocker switch.
    • Pins: 1, 2
  8. ESP32 (30 pin)

    • Description: ESP32 microcontroller with 30 pins.
    • Pins: EN, VP, VN, D34, D35, D32, D33, D25, D26, D27, D14, D12, D13, GND, Vin, D23, D22, TX0, RX0, D21, D19, D18, D5, TX2, RX2, D4, D2, D15, 3V3
  9. 240v Power Source

    • Description: Standard 240V AC power source.
    • Pins: Live, Neutral
  10. USB power

    • Description: USB power source.
    • Pins: +, -

Wiring Details

AC Bulb

LED Filament 3V

1 Channel 5V Relay Module

Resistor

5V Adapter

Flush Switch

Rocker Switch (SPST)

ESP32 (30 pin)

240v Power Source

USB power

Code Documentation

sketch.ino

#ifdef ENABLE_DEBUG
    #define DEBUG_ESP_PORT Serial
    #define NODEBUG_WEBSOCKETS
    #define NDEBUG
#endif 

#include <WiFi.h>
#include "SinricPro.h"
#include "SinricProSwitch.h"

#define WIFI_SSID         "Wifi1"
#define WIFI_PASS         "U9mpdfjj3"
#define APP_KEY           "8073da43-50a1-415d-bb7d-09b7d66f7e74"
#define APP_SECRET        "af4bb54f-bab6-41df-88a1-b31308a3dc55-4a2218f5-d905-4700-a956-25b532156dcf"
#define SWITCH_ID         "6772b86f13f98f141618c214"

#define BAUD_RATE         115200               // Change baudrate to your need
#define SwitchPin1        13
#define SwitchPin2        12
#define RelayPin1         23
#define RelayPin2         19
#define wifiLed           2      //D2

int toggleState_1 = 0; // Define integer to remember the toggle state for relay 1
int toggleState_2 = 0; // Define integer to remember the toggle state for relay 2

bool SwitchState_1 = LOW;
bool SwitchState_2 = LOW;
bool myPowerState = false;
unsigned long lastBtnPress = 0;

bool onPowerState1(const String &deviceId, bool &state) {
  Serial.printf("Device %s turned %s (via SinricPro) \\r\\n", deviceId.c_str(), state ? "on" : "off");
  myPowerState = state;
  digitalWrite(RelayPin1, myPowerState ? LOW : HIGH);
  return true; // request handled properly
}

bool onPowerState2(const String &deviceId, bool &state) {
  Serial.printf("Device %s turned %s (via SinricPro) \\r\\n", deviceId.c_str(), state ? "on" : "off");
  myPowerState = state;
  digitalWrite(RelayPin2, myPowerState ? LOW : HIGH);
  return true; // request handled properly
}

void handleButtonPress() {
  unsigned long actualMillis = millis(); // get actual millis() and keep it in variable actualMillis
  
  if (digitalRead(SwitchPin1) == LOW && SwitchState_1 == LOW && actualMillis - lastBtnPress > 1000) {
    myPowerState = !myPowerState; // flip myPowerState
    digitalWrite(RelayPin1, myPowerState ? LOW : HIGH); // toggle relay state
    SwitchState_1 = HIGH;
    Serial.println("Switch-1 toggled");
    lastBtnPress = actualMillis;
  }

  if (digitalRead(SwitchPin1) == HIGH && SwitchState_1 == HIGH) {
    SwitchState_1 = LOW;
  }

  if (digitalRead(SwitchPin2) == LOW && SwitchState_2 == LOW && actualMillis - lastBtnPress > 1000