In this smart home IoT project, I have explained how to make an Arduino Cloud ESP32 Alexa home automation system to control appliances with the internet from anywhere in the world. With this internet of things project, you can control 4 relays from Arduino IoT Cloud dashboard, Alexa, IR remote, and manual switches. You can also monitor the real-time room temperature in the Arduino cloud dashboard and Amazon Alexa app. If there is no internet available still, you can control the appliances from the IR remote and switches.

I have used all the FREE tools and you don’t need any Alexa devices or Amazon Echo Dot for this voice control smart home IoT project.

Smart Home IoT Project using Arduino Cloud

So if you follow all the steps, you can easily make this Home Automation System with Arduino IoT Cloud and ESP32 for your home.

Required Components for the ESP32 IoT projects

Required Components for the ESP32 IoT
  1. ESP32 DevKIT V1 Amazon
  2. 4-channel 5V SPDT Relay Module Amazon
  3. DHT11 Sensor Amazon
  4. TSOP1838 IR Receiver (with metallic case) Amazon
  5. Pushbuttons Amazon
  6. Any IR Remote
  7. Amazon Echo Dot (optional) Amazon

Circuit of the Arduino Cloud ESP32 projects

Circuit of ESP32 IoT projects

The circuit is very simple, I have used D23, D22, D21 & D19 GPIO to control the 4-channel relay module.

And the GPIO D13, D12, D14 & D27 connected with pushbuttons to control the relay module manually.

I have used the INPUT_PULLUP function in Arduino IDE instead of using the pull-up resistors with each push button.

As per the source code, when the control pins of the relay module receive a LOW signal the relay will turn on and the relay will turn off for the HIGH signal in the control pin.

IR remote receiver (TSOP1838) connected with D35. And the DHT11 sensor connected with RX2 (GPIO16).

If you want to use the latched switches instead of pushbuttons, then just connect the switches instead of the pushbuttons across GPIO pins and GND.

I have used a 5V 5Amp mobile charger to supply the circuit.

Please take proper safety precautions while connecting the AC appliances.

Tutorial video on Arduino Cloud ESP32 IoT project

In the Arduino Cloud tutorial video, I have covered the following steps in detail.

  • Create new things in Arduino IoT Cloud.
  • How to set up Arduino IoT Cloud Dashboard.
  • How to set up Arduino IoT Cloud for ESP32.
  • Programming the ESP32 with Arduino IDE
  • Connect Arduino IoT Cloud with Amazon Alexa App.
  • Control relays with IR remote

Arduino IoT Cloud FREE account setup

For this IoT project, I have used the Arduino Cloud Free plan.

First, you have to add ESP32 devices in Arduino IoT Cloud.

Then you have to add 4 Alexa Compatible Switch variables and 1 Temperature sensor variable to the Arduino Cloud Thing.

Please click on the following link for more details on Arduino IoT Cloud setup.

Getting Started with Arduino IoT Cloud

Important points for the ESP32 internet of things project:

  1. You can control maximum 5 relays or sensors with the Arduino IoT cloud FREE plan.
  2. The IR receiver sensor must have a metallic casing. Otherwise, you may face issues while trying to get the Hex code.
  3. You don’t need an Amazon Echo Dot or any other Alexa devices for this project. But if you have you can use it.
  4. After compiling the code, you have to press and hold the BOOT button of ESP32 until the code starts uploading.
  5. You can also use Arduino Web Editor to program the ESP32.
  6. You will get real-time feedback if the ESP32 connected to the internet.
  7. Use a stable 5V 5A DC power supply.

Program ESP32 with Arduino IDE

In the Tutorial video, I have explained all the steps to program the NodeMCU using Arduino IDE.

  1. Update the Preferences –> Aditional boards Manager URLs: https://dl.espressif.com/dl/package_esp32_index.json, http://arduino.esp8266.com/stable/package_esp8266com_index.json
  2. Then install the ESP32 board from the Board manager or Click Here to download the ESP32 board.
  3. Download the required libraries from the following links:


Go to Sketch -> Include Libraries -> Manage Libraries in Arduino IDE.

ArduinoIotCloud library dependencies

When you try to install the ArduinoIoTCloud library, it will ask you to install all the dependencies. Then Click on Install All.

Code for Arduino IoT Cloud ESP32 home automation

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

const char THING_ID[]           = ""; //Enter THING ID
const char DEVICE_LOGIN_NAME[]  = ""; //Enter DEVICE ID

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

#define DHTPIN              16 //RX2  pin connected with DHT
#define IR_RECV_PIN         35 //D35 pin connected with IR Receiver IC

// define the GPIO connected with Relays and switches
#define RelayPin1 23  //D23
#define RelayPin2 22  //D22
#define RelayPin3 21  //D21
#define RelayPin4 19  //D19

#define SwitchPin1 13  //D13
#define SwitchPin2 12  //D12
#define SwitchPin3 14  //D14
#define SwitchPin4 27  //D27

#define wifiLed    2   //D2

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301


DHT dht(DHTPIN, DHTTYPE);
IRrecv irrecv(IR_RECV_PIN);
decode_results results;

int toggleState_1 = 0; //Define integer to remember the toggle state for relay 1
int toggleState_2 = 0; //Define integer to remember the toggle state for relay 2
int toggleState_3 = 0; //Define integer to remember the toggle state for relay 3
int toggleState_4 = 0; //Define integer to remember the toggle state for relay 4

float temperature1 = 0;
float humidity1   = 0;
int   reconnectFlag = 0;

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

CloudSwitch switch1;
CloudSwitch switch2;
CloudSwitch switch3;
CloudSwitch switch4;
CloudTemperatureSensor temperature;

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);
  ArduinoCloud.addProperty(temperature, READ, 8 * SECONDS, NULL); //Update temperature value after every 8 seconds
}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

void readSensor(){
     
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
  
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  else {
    humidity1 = h;
    temperature = t;
   // Serial.println(tempareture);
  }  
}

void sendSensor()
{
  readSensor();
}

void ir_remote_control(){
  if (irrecv.decode(&results)) {
    
      switch(results.value){
          case 0x80BF49B6:  relayOnOff(1); switch1 = toggleState_1; break; //update the HEX-code
          case 0x80BFC936:  relayOnOff(2); switch2 = toggleState_2; break; //update the HEX-code
          case 0x80BF33CC:  relayOnOff(3); switch3 = toggleState_3; break; //update the HEX-code
          case 0x80BF718E:  relayOnOff(4); switch4 = toggleState_4; break; //update the HEX-code
          default : break;         
        }   
        //Serial.println(results.value, HEX);    
        irrecv.resume();   
  } 
}

void relayOnOff(int relay) {

  switch (relay) {
    case 1:
      if (toggleState_1 == 0) {
        digitalWrite(RelayPin1, LOW); // turn on relay 1
        toggleState_1 = 1;
        Serial.println("Device1 ON");
      }
      else {
        digitalWrite(RelayPin1, HIGH); // turn off relay 1
        toggleState_1 = 0;
        Serial.println("Device1 OFF");
      }
      delay(100);
      break;
    case 2:
      if (toggleState_2 == 0) {
        digitalWrite(RelayPin2, LOW); // turn on relay 2
        toggleState_2 = 1;
        Serial.println("Device2 ON");
      }
      else {
        digitalWrite(RelayPin2, HIGH); // turn off relay 2
        toggleState_2 = 0;
        Serial.println("Device2 OFF");
      }
      delay(100);
      break;
    case 3:
      if (toggleState_3 == 0) {
        digitalWrite(RelayPin3, LOW); // turn on relay 3
        toggleState_3 = 1;
        Serial.println("Device3 ON");
      } else {
        digitalWrite(RelayPin3, HIGH); // turn off relay 3
        toggleState_3 = 0;
        Serial.println("Device3 OFF");
      }
      delay(100);
      break;
    case 4:
      if (toggleState_4 == 0) {
        digitalWrite(RelayPin4, LOW); // turn on relay 4
        toggleState_4 = 1;
        Serial.println("Device4 ON");
      }
      else {
        digitalWrite(RelayPin4, HIGH); // turn off relay 4
        toggleState_4 = 0;
        Serial.println("Device4 OFF");
      }
      delay(100);
      break;
    default : break;
  }
}

void manual_control() {
  //Manual Switch Control
  if (digitalRead(SwitchPin1) == LOW) {
    delay(200);
    relayOnOff(1);
    switch1 = toggleState_1;
  }
  else if (digitalRead(SwitchPin2) == LOW) {
    delay(200);
    relayOnOff(2);
    switch2 = toggleState_2;
  }
  else if (digitalRead(SwitchPin3) == LOW) {
    delay(200);
    relayOnOff(3);
    switch3 = toggleState_3;
  }
  else if (digitalRead(SwitchPin4) == LOW) {
    delay(200);
    relayOnOff(4);
    switch4 = toggleState_4;
  }
}

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();
  dht.begin();
  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(RelayPin1, OUTPUT);
  pinMode(RelayPin2, OUTPUT);
  pinMode(RelayPin3, OUTPUT);
  pinMode(RelayPin4, OUTPUT);

  pinMode(wifiLed, OUTPUT);

  pinMode(SwitchPin1, INPUT_PULLUP);
  pinMode(SwitchPin2, INPUT_PULLUP);
  pinMode(SwitchPin3, INPUT_PULLUP);
  pinMode(SwitchPin4, INPUT_PULLUP);

  //During Starting all Relays should TURN OFF
  digitalWrite(RelayPin1, HIGH);
  digitalWrite(RelayPin2, HIGH);
  digitalWrite(RelayPin3, HIGH);
  digitalWrite(RelayPin4, HIGH);
}

void loop() {

  ArduinoCloud.update();
  
  manual_control();     //Manual Control
  ir_remote_control();  //IR Remote Control
  sendSensor();         //Get Sensor Data
}

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

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

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

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

After uploading the code to ESP32, please refer to the following articles for connecting the Arduino IoT Cloud Account with Amazon Alexa App.

After doing all these steps, now you control the appliances with Alexa.

ESP32 Alexa control Relay using Arduino Cloud

If the ESP32 is connected with WiFi, then you can ask Alexa, to turn on the light [“Alexa, Turn ON Room Light”]. Thus, you can control the appliances like lights, fans, etc with voice commands using Amazon Alexa App from anywhere in the world.

You can also monitor the real-time feedbacks and room temperature in the Alexa app.

Control Relays with Arduino IoT Cloud Remote App

Control Relays with Arduino IoT Cloud

You can also control the relays and monitor the room temperature from Arduino IoT Cloud Remote App.

Just download and install the Arduino IoT Cloud Remote app from Google Play Store or App Store, then log in to your Arduino Cloud account and select the dashboard.

Sometimes you have to refresh the dashboard multiple times if you face any issue.

Control Relays manually with Push Buttons

You can always control the appliances manually with push buttons. and if the ESP32 is not connected with the Wi-Fi, still you can control the appliances with push buttons.

IR Remote control relays with ESP32

IR Remote control relays with ESP32

You can also control the appliances with any IR remote.

First, you have to get the HEX codes of the remote buttons, then update the HEX code in the code. That’s it. Now you can control the appliances with the IR remote.

PCB for the ESP32 IoT Projects

PCB for the ESP32 IoT Projects

You can also use this PCB for this ESP32 IoT project. Just download the Gerber file and order the PCB from any PCB manufacturer.

I hope you like this Smart house IoT projects idea with the Arduino Cloud ESP32.

Click Here for more such ESP32 projects.

Please do share your feedback on this IoT project. Thank you for your time.

This Post Has 3 Comments

  1. vitor hugo s santos

    bom dia amigo!qual o programa que vc usa para as pcb.

  2. ally

    Nice article. I there a way to update the widget status when you use the manual switch. I need to the cloud to update automatically when the control is by the switch. thx

Leave a Reply