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

Wi-Fi Controlled Smart Home Automation with ESP8266 and Relay Module

View and Edit Project In Cirkit DesignerImage of Wi-Fi Controlled Smart Home Automation with ESP8266 and Relay Module

Circuit Documentation

Summary

This circuit is designed to control a fan and a bulb using an ESP8266 NodeMCU microcontroller and a 2-channel relay module. The ESP8266 is connected to a WiFi network and can be controlled remotely via the SinricPro platform. The circuit also includes a 5V adapter to power the ESP8266 and the relay module, a 9V battery to power the fan, and a socket to power the bulb.

Component List

  1. ESP8266 NodeMCU

    • Description: A low-cost Wi-Fi microchip with full TCP/IP stack and microcontroller capability.
    • Pins: D0, D1, D2, D3, D4, 3V3, GND, D5, D6, D7, D8, RX, TX, A0, RSV, SD3, SD2, SD1, CMD, SD0, CLK, EN, RST, VIN
  2. 2-Channel Relay

    • Description: A relay module with two channels, used to control high voltage devices.
    • Pins: NC, COM, NO, VCC, IN2, IN1, GND
  3. 5V Adapter

    • Description: A power adapter that converts AC to 5V DC.
    • Pins: AC In 1, AC In 2, 5V, GND
  4. Bulb

    • Description: A standard light bulb.
    • Pins: GND, power
  5. Socket

    • Description: A standard electrical socket.
    • Pins: earth, life, neutral
  6. Fan

    • Description: A 5V DC fan.
    • Pins: GND, 5V
  7. 9V Battery

    • Description: A standard 9V battery.
    • Pins: +, -

Wiring Details

ESP8266 NodeMCU

2-Channel Relay

5V Adapter

Bulb

Socket

Fan

9V Battery

Code Documentation

Sketch (sketch.ino)

// Uncomment the following line to enable serial debug output
//#define ENABLE_DEBUG

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

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "SinricPro.h"
#include "SinricProSwitch.h"

#include <map>

#define WIFI_SSID         "Hanuman-jio"    
#define WIFI_PASS         "Madcry400"
#define APP_KEY           "188e94f1-830d-49e0-a81b-e67ac28b3c02"      // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
#define APP_SECRET        "83441171-a795-4a2a-98b6-6145784a460d-cc3e80bc-0092-4c07-8712-a3dd9f1328b4"   // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"

//Enter the device IDs here
#define device_ID_1   "674091d18916d6b80a1f32bb"
#define device_ID_2   "6740a3e3716c0bcb0881b9d8"
#define device_ID_3   "6742c99e13f98f1416f3f796"
#define device_ID_4   "xxxxxxxxxxxxxxxxxxxxxxxx"

// define the GPIO connected with Relays and switches
#define RelayPin1 5  //D1
#define RelayPin2 4  //D2
#define RelayPin3 14 //D5
#define RelayPin4 12 //D6

#define SwitchPin1 10  //SD3
#define SwitchPin2 0   //D3 
#define SwitchPin3 13  //D7
#define SwitchPin4 3   //RX

#define wifiLed   16   //D0

// comment the following line if you use a toggle switches instead of tactile buttons
//#define TACTILE_BUTTON 1

#define BAUD_RATE   9600

#define DEBOUNCE_TIME 250

typedef struct {      // struct for the std::map below
  int relayPIN;
  int flipSwitchPIN;
} deviceConfig_t;


// this is the main configuration
// please put in your deviceId, the PIN for Relay and PIN for flipSwitch
// this can be up to N devices...depending on how much pin's available on your device ;)
// right now we have 4 devicesIds going to 4 relays and 4 flip switches to switch the relay manually
std::map<String, deviceConfig_t> devices = {
    //{deviceId, {relayPIN,  flipSwitchPIN}}
    {device_ID_1, {  RelayPin1, SwitchPin1 }},
    {device_ID_2, {  RelayPin2, SwitchPin2 }},
    {device_ID_3, {  RelayPin3, SwitchPin3 }},
    {device_ID_4, {  RelayPin4, SwitchPin4 }}     
};

typedef struct {      // struct for the std::map below
  String deviceId;
  bool lastFlipSwitchState;
  unsigned long lastFlipSwitchChange;
} flipSwitchConfig_t;

std::map<int, flipSwitchConfig_t> flipSwitches;    // this map is used to map flipSwitch PINs to deviceId and handling debounce and last flipSwitch state checks
                                                  // it will be setup in "setupFlipSwitches" function, using informations from devices map

void setupRelays() { 
  for (auto &device : devices) {           // for each device (relay, flipSwitch combination)
    int relayPIN = device.second.relayPIN; // get the relay pin
    pinMode(relayPIN, OUTPUT);             // set relay pin to OUTPUT
    digitalWrite(relayPIN, HIGH);
  }
}

void setupFlipSwitches() {
  for (auto &device : devices)  {                     // for each device (relay / flipSwitch combination)
    flipSwitchConfig_t flipSwitchConfig;              // create a new flipSwitch configuration

    flipSwitchConfig.deviceId = device.first;         // set the deviceId
    flipSwitchConfig.lastFlipSwitchChange = 0;        // set debounce time
    flipSwitchConfig.lastFlipSwitchState = true;     // set lastFlipSwitchState to false (LOW)--

    int flipSwitchPIN = device.second.flipSwitchPIN;  // get the flipSwitchPIN

    flipSwitches[flipSwitchPIN] = flipSwitchConfig;   // save the flipSwitch config to flipSwitches map
    pinMode(flipSwitchPIN, INPUT_PULLUP);                   // set the flipSwitch pin to INPUT
  }
}

bool onPowerState(String deviceId, bool &state)
{
  Serial.printf("%s: %s\r\n", deviceId.c_str(), state ? "on" : "off");
  int relayPIN = devices[deviceId