Yarana Iot Guru
Published © MIT

🏠 Home Automation with ESP32: Building a 4-Channel Relay

Control up to 4 appliances using ESP32 via Web or App. Build your smart home easily β€” by YaranaIoT Guru

BeginnerFull instructions provided8 hours10
🏠 Home Automation with ESP32: Building a 4-Channel Relay

Things used in this project

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

πŸ’» ESP32 Web Control Code

C/C++
Here’s a simple web-based control code for ESP32.
#include <WiFi.h>
#include <WebServer.h>

const char* ssid = "YourWiFi";
const char* password = "YourPassword";

int relays[] = {23, 22, 21, 19};
WebServer server(80);

void handleRoot() {
  String html = "<h2>ESP32 Home Automation</h2>";
  for (int i = 0; i < 4; i++) {
    html += "Relay " + String(i+1) + " ";
    html += "<a href=\"/on" + String(i+1) + "\">ON</a> ";
    html += "<a href=\"/off" + String(i+1) + "\">OFF</a><br>";
  }
  server.send(200, "text/html", html);
}

void setup() {
  Serial.begin(115200);
  for (int i = 0; i < 4; i++) pinMode(relays[i], OUTPUT);
  
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500); Serial.print(".");
  }
  Serial.println("\nConnected! IP: " + WiFi.localIP().toString());
  
  server.on("/", handleRoot);
  for (int i = 0; i < 4; i++) {
    server.on("/on" + String(i+1), [i]() {
      digitalWrite(relays[i], HIGH);
      server.send(200, "text/html", "Relay " + String(i+1) + " ON <a href='/'>Back</a>");
    });
    server.on("/off" + String(i+1), [i]() {
      digitalWrite(relays[i], LOW);
      server.send(200, "text/html", "Relay " + String(i+1) + " OFF <a href='/'>Back</a>");
    });
  }
  server.begin();
}

void loop() {
  server.handleClient();
}

Credits

Yarana Iot Guru
20 projects β€’ 0 followers
Yarana Iot Guru Yarana IoT Guru: Arduino, ESP32, GSM, NodeMCU & more. Projects, Tutorials & App Development. Innovate with us!
Thanks to YaranaIoT Guru.

Comments