Yarana Iot Guru
Published © MIT

Building a Direct ESP32-to-Phone Home Automation System

Control home appliances directly from your phone using ESP32 — no cloud needed! DIY smart home made simple by YaranaIoT Guru

BeginnerFull instructions provided8 hours308
Building a Direct ESP32-to-Phone Home Automation System

Things used in this project

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

💻 Code Example (ESP32 Web Server)

C/C++
#include <WiFi.h>
#include <WebServer.h>

const char* ssid = "ESP32_Hotspot";
const char* password = "12345678";  // For AP mode

WebServer server(80);
int relayPins[4] = {13, 12, 14, 27};
bool relayState[4] = {LOW, LOW, LOW, LOW};

String buildHTML() {
  String html = "<html><head><meta charset='UTF-8'><meta name='viewport' content='width=device-width, initial-scale=1'>";
  html += "<title>ESP32 Direct Home Automation</title>";
  html += "<style>body{text-align:center;font-family:Arial;}button{padding:10px 20px;margin:5px;}</style></head><body>";
  html += "<h2>🏠 Direct ESP32-to-Phone Control</h2><hr>";
  for (int i = 0; i < 4; i++) {
    html += "<p>Relay " + String(i+1) + ": ";
    html += relayState[i] ? "<b style='color:green;'>ON</b>" : "<b style='color:red;'>OFF</b>";
    html += "</p><a href='/toggle?ch=" + String(i) + "'><button>Toggle</button></a><hr>";
  }
  html += "<p><small>By YaranaIoT Guru</small></p></body></html>";
  return html;
}

void handleRoot() { server.send(200, "text/html", buildHTML()); }

void handleToggle() {
  if(server.hasArg("ch")) {
    int ch = server.arg("ch").toInt();
    if(ch>=0 && ch<4){
      relayState[ch] = !relayState[ch];
      digitalWrite(relayPins[ch], relayState[ch]);
    }
  }
  server.sendHeader("Location", "/");
  server.send(303);
}

void setup() {
  Serial.begin(115200);
  for(int i=0;i<4;i++){ pinMode(relayPins[i], OUTPUT); digitalWrite(relayPins[i], LOW); }

  // Start ESP32 as Access Point
  WiFi.softAP(ssid,password);
  Serial.println("AP Started. Connect to Wi-Fi 'ESP32_Hotspot'");
  Serial.println("IP address: " + WiFi.softAPIP().toString());

  server.on("/", handleRoot);
  server.on("/toggle", handleToggle);
  server.begin();
  Serial.println("Web server started.");
}

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

Credits

Yarana Iot Guru
48 projects • 25 followers
Yarana Iot GuruYarana IoT Guru: Arduino,ESP32, GSM, NodeMCU & more.Projects, Tutorials & App Development. Innovate with us!
Thanks to YaranaIoT Guru.

Comments