Header Ads Widget

Building a Smart Home with ESP32 using Arduino IoT Cloud: Control Appliances via IR Remote, Manual Switches, and Alexa

 Introduction:

The concept of a smart home has transformed from a futuristic idea to a reality that's easily achievable through the power of Internet of Things (IoT) technology. In this blog post, we're going to delve into the exciting realm of home automation using the ESP32 microcontroller and the Arduino IoT Cloud platform. We'll guide you through the process of controlling four home appliances via an IR remote, manual switches, and even the convenience of voice commands using Amazon Alexa.



Prerequisites:

Before diving into this project, make sure you have the following:

ESP32 Board: A versatile microcontroller with built-in WiFi and Bluetooth capabilities.

Arduino IoT Cloud Account: Create an account on the Arduino IoT Cloud platform for seamless device management.

IR Receiver and Transmitter: To enable IR remote control.

Relays: Required to control the appliances.

Manual Switches: For manual control alongside digital methods.

Amazon Alexa Account: Set up an Alexa developer account.

Project Overview:

The objective of this project is to create a smart home setup that allows you to control four different home appliances remotely, manually, and even through voice commands via Alexa. The ESP32 will serve as the central hub, connecting all these control methods through the Arduino IoT Cloud platform.

 circuit :

Code:


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

const char THING_ID[]           = "25c8e65d-f0ce-4071-aebb-d77cefeb5e9f"; //Enter THING ID
const char DEVICE_LOGIN_NAME[]  = "6d56b481-3b35-4d29-a6cc-60f1b843e12a"; //Enter DEVICE ID

const char SSID[]               = "mewt";    //Enter WiFi SSID (name)
const char PASS[]               = "87654321";    //Enter WiFi password
const char DEVICE_KEY[]         = "OZBRYQMBMTFPGGNOYSGK";    //Enter Secret device password (Secret Key)


#define IR_RECV_PIN         35 //D35 pin connected with IR Receiver IC















// define the GPIO connected with Relays and switches
#define relay1 23  //D23
#define relay2 22  //D22
#define relay3 21  //D21
#define relay4 19  //D19

#define manual_switch1 13  //D13
#define manual_switch2 12  //D12
#define manual_switch3 14  //D14
#define manual_switch4 27  //D27

#define wifiLed    2   //D2




IRrecv irrecv(IR_RECV_PIN);
decode_results results;

int t_state1 = 0; //Define integer to remember the toggle state for relay 1
int t_state2 = 0; //Define integer to remember the toggle state for relay 2
int t_state3 = 0; //Define integer to remember the toggle state for relay 3
int t_state4 = 0; //Define integer to remember the toggle state for relay 4

// Switch State
bool s_state1 = LOW;
bool s_state2 = LOW;
bool s_state3 = LOW;
bool s_state4 = LOW;



void onSwitch1Change();
void onSwitch2Change();
void onSwitch3Change();
void onSwitch4Change();


CloudSwitch switch1;
CloudSwitch switch2;
CloudSwitch switch3;
CloudSwitch switch4;

void ir_remote_control(){
  if (irrecv.decode(&results)) {
    
      switch(results.value){
          case 0xC00001:  relayOnOff(1); switch1 = t_state1; break; //update the HEX-code
          case 0xC00002:  relayOnOff(2); switch2 = t_state2; break; //update the HEX-code
          case 0xC00003:  relayOnOff(3); switch3 = t_state3; break; //update the HEX-code
          case 0xC00004:  relayOnOff(4); switch4 = t_state4; break; //update the HEX-code
          default : break;         
        }   
        //Serial.println(results.value, HEX);    
        irrecv.resume();   
  } 
}

void initProperties(){

  ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
  ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
  ArduinoCloud.setThingId(THING_ID);
  ArduinoCloud.addProperty(switch1, READWRITE, ON_CHANGE, onSwitch1Change);
  ArduinoCloud.addProperty(switch2, READWRITE, ON_CHANGE, onSwitch2Change);
  ArduinoCloud.addProperty(switch3, READWRITE, ON_CHANGE, onSwitch3Change);
  ArduinoCloud.addProperty(switch4, READWRITE, ON_CHANGE, onSwitch4Change);
  
}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);





void relayOnOff(int relay) {

  switch (relay) {
    case 1:
      if (t_state1 == 0) {
        digitalWrite(relay1, LOW); // turn on relay 1
        t_state1 = 1;
        Serial.println("Device1 ON");
      }
      else {
        digitalWrite(relay1, HIGH); // turn off relay 1
        t_state1 = 0;
        Serial.println("Device1 OFF");
      }
      delay(100);
      break;
    case 2:
      if (t_state2 == 0) {
        digitalWrite(relay2, LOW); // turn on relay 2
        t_state2 = 1;
        Serial.println("Device2 ON");
      }
      else {
        digitalWrite(relay2, HIGH); // turn off relay 2
        t_state2 = 0;
        Serial.println("Device2 OFF");
      }
      delay(100);
      break;
    case 3:
      if (t_state3 == 0) {
        digitalWrite(relay3, LOW); // turn on relay 3
        t_state3 = 1;
        Serial.println("Device3 ON");
      } else {
        digitalWrite(relay3, HIGH); // turn off relay 3
        t_state3 = 0;
        Serial.println("Device3 OFF");
      }
      delay(100);
      break;
    case 4:
      if (t_state4 == 0) {
        digitalWrite(relay4, LOW); // turn on relay 4
        t_state4 = 1;
        Serial.println("Device4 ON");
      }
      else {
        digitalWrite(relay4, HIGH); // turn off relay 4
        t_state4 = 0;
        Serial.println("Device4 OFF");
      }
      delay(100);
      break;
    default : break;
  }
}

void manual_control()
{
  if (digitalRead(manual_switch1) == LOW && s_state1 == LOW) {
    digitalWrite(relay1, LOW);
    t_state1 = 1;
    s_state1 = HIGH;
    switch1 = t_state1;
    Serial.println("Switch-1 on");
  }
  if (digitalRead(manual_switch1) == HIGH && s_state1 == HIGH) {
    digitalWrite(relay1, HIGH);
    t_state1 = 0;
    s_state1 = LOW;
    switch1 = t_state1;
    Serial.println("Switch-1 off");
  }
  if (digitalRead(manual_switch2) == LOW && s_state2 == LOW) {
    digitalWrite(relay2, LOW);
    t_state2 = 1;
    s_state2 = HIGH;
    switch2 = t_state2;
    Serial.println("Switch-2 on");
  }
  if (digitalRead(manual_switch2) == HIGH && s_state2 == HIGH) {
    digitalWrite(relay2, HIGH);
    t_state2 = 0;
    s_state2 = LOW;
    switch2 = t_state2;
    Serial.println("Switch-2 off");
  }
  if (digitalRead(manual_switch3) == LOW && s_state3 == LOW) {
    digitalWrite(relay3, LOW);
    t_state3 = 1;
    s_state3 = HIGH;
    switch3 = t_state3;
    Serial.println("Switch-3 on");
  }
  if (digitalRead(manual_switch3) == HIGH && s_state3 == HIGH) {
    digitalWrite(relay3, HIGH);
    t_state3 = 0;
    s_state3 = LOW;
    switch3 = t_state3;
    Serial.println("Switch-3 off");
  }
  if (digitalRead(manual_switch4) == LOW && s_state4 == LOW) {
    digitalWrite(relay4, LOW);
    t_state4 = 1;
    s_state4 = HIGH;
    switch4 = t_state4;
    Serial.println("Switch-4 on");
  }
  if (digitalRead(manual_switch4) == HIGH && s_state4 == HIGH) {
    digitalWrite(relay4, HIGH);
    t_state4 = 0;
    s_state4 = LOW;
    switch4 = t_state4;
    Serial.println("Switch-4 off");
  }
}  

void doThisOnConnect(){
  /* add your custom code here */
  Serial.println("Board successfully connected to Arduino IoT Cloud");
  digitalWrite(wifiLed, HIGH); //Turn off WiFi LED
}
void doThisOnSync(){
  /* add your custom code here */
  Serial.println("Thing Properties synchronised");
}

void doThisOnDisconnect(){
  /* add your custom code here */
  Serial.println("Board disconnected from Arduino IoT Cloud");
  digitalWrite(wifiLed, LOW); //Turn off WiFi LED
}

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500);

  // Defined in thingProperties.h
  initProperties();
  irrecv.enableIRIn(); // Start the receiver
  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  ArduinoCloud.addCallback(ArduinoIoTCloudEvent::CONNECT, doThisOnConnect);
  ArduinoCloud.addCallback(ArduinoIoTCloudEvent::SYNC, doThisOnSync);
  ArduinoCloud.addCallback(ArduinoIoTCloudEvent::DISCONNECT, doThisOnDisconnect);

  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();

  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);

  pinMode(wifiLed, OUTPUT);

  pinMode(manual_switch1, INPUT_PULLUP);
  pinMode(manual_switch2, INPUT_PULLUP);
  pinMode(manual_switch3, INPUT_PULLUP);
  pinMode(manual_switch4, INPUT_PULLUP);

  //During Starting all Relays should TURN OFF
  digitalWrite(relay1, HIGH);
  digitalWrite(relay2, HIGH);
  digitalWrite(relay3, HIGH);
  digitalWrite(relay4, HIGH);
}

void loop() {

  ArduinoCloud.update();
  
  manual_control();     //Manual Control
  ir_remote_control();  //IR Remote Control

}

void onSwitch1Change() {
  //Control the device
  if (switch1 == 1)
  {
    digitalWrite(relay1, LOW);
    Serial.println("Device1 ON");
    t_state1 = 1;
  }
  else
  {
    digitalWrite(relay1, HIGH);
    Serial.println("Device1 OFF");
    t_state1 = 0;
  }
}

void onSwitch2Change() {
  if (switch2 == 1)
  {
    digitalWrite(relay2, LOW);
    Serial.println("Device2 ON");
    t_state2 = 1;
  }
  else
  {
    digitalWrite(relay2, HIGH);
    Serial.println("Device2 OFF");
    t_state2 = 0;
  }
}

void onSwitch3Change() {
  if (switch3 == 1)
  {
    digitalWrite(relay3, LOW);
    Serial.println("Device3 ON");
    t_state3 = 1;
  }
  else
  {
    digitalWrite(relay3, HIGH);
    Serial.println("Device3 OFF");
    t_state3 = 0;
  }
}

void onSwitch4Change() {
  if (switch4 == 1)
  {
    digitalWrite(relay4, LOW);
    Serial.println("Device4 ON");
    t_state4 = 1;
  }
  else
  {
    digitalWrite(relay4, HIGH);
    Serial.println("Device4 OFF");
    t_state4 = 0;
  }
}




Conclusion:

Creating a smart home using the ESP32 and the Arduino IoT Cloud platform is a fascinating journey into the world of IoT and home automation. By combining IR remote control, manual switches, and the convenience of Alexa voice commands, you're not just building a smart home – you're creating an intelligent living space that adapts to your preferences and lifestyle. As technology continues to evolve, your skills in building and customizing such systems will prove invaluable. So, gather your components, unleash your creativity, and embark on the exciting path of transforming your house into a smart home that's controlled at your fingertips.





Post a Comment

0 Comments