ESP8266 4-Relay IoT Control With Arduino Cloud & Push Buttons

by ElectroIoTIN in Circuits > Arduino

25 Views, 0 Favorites, 0 Comments

ESP8266 4-Relay IoT Control With Arduino Cloud & Push Buttons

demo.jpg

In this project, we will use an ESP8266 NodeMCU to control four relays using both Arduino IoT Cloud and manual push buttons. This allows remote switching via the cloud while maintaining local control. The project is ideal for home automation applications, allowing you to control lights, fans, and other appliances via the internet.


Features

Remote Control – Operate relays via the Arduino IoT Cloud dashboard

Manual Control – Use physical push buttons to toggle relays

WiFi Connectivity Indicator – An LED shows the status of the WiFi connection

State Synchronization – Whether toggled manually or remotely, relay states remain synchronized


Supplies

Circuit_Diagram2.JPG
CircuitDiagram.JPG

Components Required

  1. ESP8266 NodeMCU
  2. 4-Channel Relay Module
  3. Push Buttons (x4)
  4. WiFi Router for Internet Connectivity
  5. Jumper Wires
  6. Power Adapter (5V, 2A recommended)

Circuit Diagram




Connections


Relays


RelayESP8266 Pin

Relay 1

D1 (GPIO 5)

Relay 2

D2 (GPIO 4)

Relay 3

D5 (GPIO 14)

Relay 4

D6 (GPIO 12)


Push Buttons


SwitchESP8266 Pin

Switch 1

SD3 (GPIO 10)

Switch 2

D3 (GPIO 0)

Switch 3

D7 (GPIO 13)

Switch 4

RX (GPIO 3)

WiFi LED


WiFi Status LED → D0 (GPIO 16)

Create a New Thing

Go to Arduino IoT Cloud

Create a new "Thing"

Add four Boolean variables:

  1. relay1
  2. relay2
  3. relay3
  4. relay4

Set each as Read & Write with ON_CHANGE trigger.

Configure Device

  1. Select ESP8266 NodeMCU as the device
  2. Link your WiFi credentials
  3. Copy the Secret Key for use in the code


Arduino Code

Upload the following final working code to your ESP8266:


#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
#include <ESP8266WiFi.h>

const char DEVICE_LOGIN_NAME[] = ""; // Enter DEVICE ID
const char SSID[] = ""; // Enter WiFi SSID
const char PASS[] = ""; // Enter WiFi password
const char DEVICE_KEY[] = ""; // Enter Secret device password

// Define GPIO for 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

int toggleState_1 = 0, toggleState_2 = 0, toggleState_3 = 0, toggleState_4 = 0;

void onRelay1Change();
void onRelay2Change();
void onRelay3Change();
void onRelay4Change();

CloudSwitch relay1, relay2, relay3, relay4;

void initProperties() {
ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
ArduinoCloud.addProperty(relay1, READWRITE, ON_CHANGE, onRelay1Change);
ArduinoCloud.addProperty(relay2, READWRITE, ON_CHANGE, onRelay2Change);
ArduinoCloud.addProperty(relay3, READWRITE, ON_CHANGE, onRelay3Change);
ArduinoCloud.addProperty(relay4, READWRITE, ON_CHANGE, onRelay4Change);
}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

void setup() {
Serial.begin(9600);
initProperties();
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
pinMode(RelayPin1, OUTPUT); pinMode(RelayPin2, OUTPUT);
pinMode(RelayPin3, OUTPUT); pinMode(RelayPin4, OUTPUT);
pinMode(wifiLed, OUTPUT);

pinMode(SwitchPin1, INPUT_PULLUP);
pinMode(SwitchPin2, INPUT_PULLUP);
pinMode(SwitchPin3, INPUT_PULLUP);
pinMode(SwitchPin4, INPUT_PULLUP);

digitalWrite(RelayPin1, HIGH); digitalWrite(RelayPin2, HIGH);
digitalWrite(RelayPin3, HIGH); digitalWrite(RelayPin4, HIGH);
digitalWrite(wifiLed, HIGH);
}

void loop() {
ArduinoCloud.update();
manual_control();
digitalWrite(wifiLed, WiFi.status() != WL_CONNECTED ? HIGH : LOW);
}

void manual_control() {
if (digitalRead(SwitchPin1) == LOW) { toggleState_1 = !toggleState_1; digitalWrite(RelayPin1, !toggleState_1); relay1 = toggleState_1; }
if (digitalRead(SwitchPin2) == LOW) { toggleState_2 = !toggleState_2; digitalWrite(RelayPin2, !toggleState_2); relay2 = toggleState_2; }
if (digitalRead(SwitchPin3) == LOW) { toggleState_3 = !toggleState_3; digitalWrite(RelayPin3, !toggleState_3); relay3 = toggleState_3; }
if (digitalRead(SwitchPin4) == LOW) { toggleState_4 = !toggleState_4; digitalWrite(RelayPin4, !toggleState_4); relay4 = toggleState_4; }
}

void onRelay1Change() { digitalWrite(RelayPin1, relay1 ? LOW : HIGH); toggleState_1 = relay1; }
void onRelay2Change() { digitalWrite(RelayPin2, relay2 ? LOW : HIGH); toggleState_2 = relay2; }
void onRelay3Change() { digitalWrite(RelayPin3, relay3 ? LOW : HIGH); toggleState_3 = relay3; }
void onRelay4Change() { digitalWrite(RelayPin4, relay4 ? LOW : HIGH); toggleState_4 = relay4; }


How It Works

Cloud Dashboard: The Arduino IoT Cloud lets you toggle each relay remotely.

Manual Push Buttons: Pressing a button toggles its corresponding relay.

WiFi LED Indicator: If WiFi is disconnected, the LED turns ON; otherwise, it stays OFF.

State Synchronization: The relays remain updated whether controlled remotely or manually.


Future Improvements

  1. 📌 Add MQTT or Blynk for additional control options
  2. 📌 Use Alexa for voice-based relay switching
  3. 📌 Expand to more relays for home automation



All Code and PCB Gerber File and Documents

Here Is project Details And Documents GitHub Like

https://github.com/ElectroIoT/ESP8266-NodeMCU-4CH-Relay-Module