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

ESP8266 NodeMCU Wi-Fi Controlled Smart Home Automation with LCD Display

View and Edit Project In Cirkit DesignerImage of ESP8266 NodeMCU Wi-Fi Controlled Smart Home Automation with LCD Display

Circuit Documentation

Summary

This circuit is a smart home automation system based on the ESP8266 NodeMCU microcontroller. It includes a 4-channel relay module to control AC bulbs, an LCD screen for displaying status, pushbuttons for manual control, and an LED indicator for WiFi status. The system connects to WiFi and can be controlled remotely using the Sinric Pro platform.

Component List

  1. ESP8266 NodeMCU

    • Description: A low-cost WiFi microcontroller with GPIO pins.
    • 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. Relay 4 Channel 5V

    • Description: A relay module to control high voltage devices.
    • Pins: GND, IN1, IN2, IN3, IN4, VCC, COM1, COM2, COM3, COM4, NO1, NO2, NO3, NO4, NC1, NC2, NC3, NC4
  3. AC Bulb

    • Description: Standard AC bulb.
    • Pins: P, N
  4. LCD Screen 16x2 I2C

    • Description: A 16x2 character LCD with I2C interface.
    • Pins: SCL, SDA, VCC, GND
  5. Pushbutton

    • Description: A simple pushbutton switch.
    • Pins: Pin 3 (out), Pin 4 (out), Pin 1 (in), Pin 2 (in)
  6. LED: Two Pin (Red)

    • Description: A red LED.
    • Pins: Cathode, Anode
  7. 9V Battery

    • Description: A 9V battery for power supply.
    • Pins: +, -
  8. Resistor

    • Description: A resistor with specified resistance.
    • Pins: Pin1, Pin2
    • Properties:
      • Resistance: 200 Ohms
      • Resistance: 8000 Ohms
  9. AC Supply

    • Description: AC power supply.
    • Pins: +ve, -ve

Wiring Details

ESP8266 NodeMCU

Relay 4 Channel 5V

AC Bulb

LCD Screen 16x2 I2C

Pushbutton

LED: Two Pin (Red)

9V Battery

Resistor

AC Supply

Code Documentation

#include <ESP8266WiFi.h>
#include <SinricPro.h>
#include <SinricProSwitch.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Replace with your WiFi credentials
#define WIFI_SSID "Galaxy M35 5G BE86"
#define WIFI_PASSWORD "5gpunxb5xsvtwms"

// Replace with your Sinric Pro credentials
#define APP_KEY "16969b32-c75a-4450-9552-bf51f59899e1"
#define APP_SECRET "7740363e-d9eb-454f-a96b-d5bc748696f7-1852fbd7-ea9e-4d6c-b632-ccb39f7c4353"

// Replace with your device IDs from Sinric Pro
#define DEVICE_ID_LOAD1 "6759e87b13f98f1416070b10"  // LOAD 1
#define DEVICE_ID_LOAD2 "67633cee13f98f14160e8732"  // LOAD 2
#define DEVICE_ID_LOAD3 "6759e91213f98f1416070bfd"  // LOAD 3

// Pin definitions for the relays (use safer GPIO pins)
#define RELAY_1_PIN 2  // GPIO D4
#define RELAY_2_PIN 15 // GPIO D8
#define RELAY_3_PIN 13 // GPIO D7

// Pin definitions for push buttons
#define BUTTON_1_PIN 12 // GPIO D6
#define BUTTON_2_PIN 14 // GPIO D5
#define BUTTON_3_PIN 0  // GPIO D3

// Pin for WiFi connection LED
#define WIFI_LED_PIN 16 // GPIO D0 (common LED pin)

// LCD display address (change if needed)
LiquidCrystal_I2C lcd(0x27, 16, 2);  // 16x2 LCD

bool relay1State = false;
bool relay2State = false;
bool relay3State = false;

bool lastWifiState = false;

// Variables for periodic display update
unsigned long lastDisplayUpdate = 0;  // Tracks last LCD update time
unsigned int currentLoadToDisplay = 1;  // Tracks the current load being displayed
unsigned long displayInterval = 2000;  // Interval (ms) to show each load

// Callback function to handle relay states from Sinric Pro
bool onPowerState(const String &deviceId, bool &state) {
  if (deviceId == DEVICE_ID_LOAD1) {
    digitalWrite(RELAY_1_PIN, state ? LOW : HIGH); // Active LOW relay control
    relay1State = state;
  } else if (deviceId == DEVICE_ID_LOAD2) {
    digitalWrite(RELAY_2_PIN, state ? LOW : HIGH);
    relay2State = state;
  } else if (deviceId == DEVICE_ID_LOAD3) {
    digitalWrite(RELAY_3_PIN, state ? LOW : HIGH);
    relay3State = state;
  }
  return true; // Action handled
}

void setup() {
  Serial.begin(9600);

  // Set relay pins as output and turn them off initially
  pinMode(RELAY_1_PIN, OUTPUT);
  pinMode(RELAY_2_PIN, OUTPUT);
  pinMode(RELAY_3_PIN, OUTPUT);
  digitalWrite(RELAY_1_PIN, HIGH);
  digitalWrite(RELAY_2_PIN, HIGH);
  digitalWrite(RE