Yarana Iot Guru
Published © MIT

My Biggest Home Automation Project Using ESP32 | IoT Project

“Build a complete Home Automation system using ESP32, Ubidots, and LCSC components! Control lights, fans, and plugs remotely from your smart

BeginnerFull instructions provided8 hours75
My Biggest Home Automation Project Using ESP32 | IoT Project

Things used in this project

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Complete Arduino Code

C/C++
/*******************************************************
   Home Automation using ESP32 + Ubidots Cloud
   Created by Yarana IoT Guru
*******************************************************/

#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>

#define WIFI_SSID "YourWiFiName"
#define WIFI_PASS "YourWiFiPassword"
#define UBIDOTS_TOKEN "Your_Ubidots_Token"

#define RELAY1 23
#define RELAY2 22
#define RELAY3 21
#define RELAY4 19

String deviceLabel = "home_automation";

void setup() {
  Serial.begin(115200);
  pinMode(RELAY1, OUTPUT);
  pinMode(RELAY2, OUTPUT);
  pinMode(RELAY3, OUTPUT);
  pinMode(RELAY4, OUTPUT);

  digitalWrite(RELAY1, HIGH);
  digitalWrite(RELAY2, HIGH);
  digitalWrite(RELAY3, HIGH);
  digitalWrite(RELAY4, HIGH);

  WiFi.begin(WIFI_SSID, WIFI_PASS);
  Serial.print("Connecting to WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWiFi Connected!");
}

void loop() {
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    String url = "http://industrial.api.ubidots.com/api/v1.6/devices/" + deviceLabel + "/?token=" + UBIDOTS_TOKEN;
    http.begin(url);
    int httpResponseCode = http.GET();

    if (httpResponseCode > 0) {
      String payload = http.getString();
      Serial.println(payload);
      StaticJsonDocument<512> doc;
      deserializeJson(doc, payload);

      int r1 = doc["relay1"]["value"];
      int r2 = doc["relay2"]["value"];
      int r3 = doc["relay3"]["value"];
      int r4 = doc["relay4"]["value"];

      digitalWrite(RELAY1, r1 ? LOW : HIGH);
      digitalWrite(RELAY2, r2 ? LOW : HIGH);
      digitalWrite(RELAY3, r3 ? LOW : HIGH);
      digitalWrite(RELAY4, r4 ? LOW : HIGH);
    }

    http.end();
  }
  delay(2000);
}

Credits

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

Comments