Yarana Iot Guru
Published © MIT

๐Ÿ  Home Automation Made Easy: Create Your Own IoT Cloud

Build your own IoT cloud to control home appliances from anywhere โ€” smart, wireless, and easy DIY by YaranaIoT Guru ๐Ÿ’ก

BeginnerShowcase (no instructions)8 hours84
๐Ÿ  Home Automation Made Easy: Create Your Own IoT Cloud

Things used in this project

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

๐Ÿ“„ Arduino Example Code โ€” IoT Cloud Control (ESP32 + WebServer

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

const char* ssid = "YourWiFiName";
const char* password = "YourWiFiPassword";

AsyncWebServer server(80);

#define RELAY1 2
#define RELAY2 4

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

  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);
  digitalWrite(RELAY1, LOW);
  digitalWrite(RELAY2, LOW);

  WiFi.begin(ssid, password);
  Serial.println("Connecting to WiFi...");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nConnected! IP Address:");
  Serial.println(WiFi.localIP());

  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(200, "text/html",
      "<h1>YaranaIoT Guru - IoT Cloud</h1>"
      "<p><a href='/relay1/on'>Relay 1 ON</a></p>"
      "<p><a href='/relay1/off'>Relay 1 OFF</a></p>"
      "<p><a href='/relay2/on'>Relay 2 ON</a></p>"
      "<p><a href='/relay2/off'>Relay 2 OFF</a></p>");
  });

  server.on("/relay1/on", HTTP_GET, [](AsyncWebServerRequest *request){
    digitalWrite(RELAY1, HIGH);
    request->send(200, "text/html", "Relay 1 ON");
  });

  server.on("/relay1/off", HTTP_GET, [](AsyncWebServerRequest *request){
    digitalWrite(RELAY1, LOW);
    request->send(200, "text/html", "Relay 1 OFF");
  });

  server.on("/relay2/on", HTTP_GET, [](AsyncWebServerRequest *request){
    digitalWrite(RELAY2, HIGH);
    request->send(200, "text/html", "Relay 2 ON");
  });

  server.on("/relay2/off", HTTP_GET, [](AsyncWebServerRequest *request){
    digitalWrite(RELAY2, LOW);
    request->send(200, "text/html", "Relay 2 OFF");
  });

  server.begin();
}

void loop() {
  // Nothing needed, AsyncWebServer handles requests
}

Credits

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

Comments