---
# System prepended metadata

title: Home automation ESP 32

---

# Home automation ESP 32

Project title: Home automation esp32

Tarek Hasna th222yb
# Objective
Using Arduino lot cloud in combination with the ESP32 board, we can make a smart home system that allows us to control most of our home appliances from a single place. If the ESP32 is connected to internet, then we can use the Arduino lot Cloud remote app which lets us control our home appliances from anywhere around the globe. But if the ESP32 board is not connected to the internet, we may only use the board at the spot with an IR remote.

This project will make unnecessary steps that we spend lots of time on every day just disappear. Things like walking from a room to another just to press a light switch and turn off a light bulb, which takes time that I could spend on more useful things. This is the main reason that made me choose this project.

This project took me approximately 7 days to finish, and almost 4 of the 7 days were spent on getting the hardware part done. The time it would take others to do the same project would depend on how much knowledge they have, in topics like hardware.  Because the hardware part was the hardest, so you will need some decent knowledge in hardware, and secondly, electricity wiring. 

I chose this project because I wanted to help my old mother who lives with my old enough father in Syria in an old house, so it is not easy for my mother to walk between rooms to turn on/off  the lights and this project could be helpful for my parents. 
# The materials used in this project
1.	Relays 4 channel (SPDT), Price: 99 sek
Link: https://hitechchain.se/arduinokompatibel/4-channel-relay-module-with-light-coupling-5v-12v-24v
Description: Used to control high voltages so that not less or more power is let through to the circuit.
![](https://i.imgur.com/NCl9J3z.png)

2.	Push buttons (3 no), Price: 49sek
Link: https://www.kopplat.se/Produkt/11794/MonkMakes-Squid-tryckknapp-momentan-ON-OFF
Description: Lets an electric signal pass through from one side to another.
![](https://i.imgur.com/HYKyVtU.jpg)
3.	Phone charger 5V DC supply, Price 119 sek, teknikdelar.

Link: https://www.teknikdelar.se/iphone-laddare-med-lightning-kabel-1-m-vit?dfw_tracker=35448-400010206&shopping_id_click=400010206&gclid=Cj0KCQjwxb2XBhDBARIsAOjDZ341zjldx_JIjknUh7MLBfs9wkKVL0winn_ghaFSOI7J4Yed666Yn7YaAqplEALw_wcB
Description: The main power supply for the circuit.
![](https://i.imgur.com/innZcJI.jpg)
4.	DHT 11 Temperature and humidity sensor, Price: 10sek, Aliexpress.
Link:https://www.aliexpress.com/item/32848531680.html?UTABTest=aliabtest300081_422161&_randl_currency=SEK&_randl_shipto=SE&src=google&aff_fcid=7d99684deb8a4da78e69558ce41b8850-1658344859289-09215-UneMJZVf&aff_fsk=UneMJZVf&aff_platform=aaf&sk=UneMJZVf&aff_trace_key=7d99684deb8a4da78e69558ce41b8850-1658344859289-09215-UneMJZVf&terminal_id=942186c9432d494a87066ccfb8de659b&OLP=1083300108_f_group0&o_s_id=1083300108&afSmartRedirect=y
Description: Used to sense the humidity and temperature on the spot.
![](https://i.imgur.com/bsQOA6J.png)

5.	Electricity wire, Price: 50sek, Aliexpress.
Link:
https://www.aliexpress.com/item/1005004100175176.html?spm=a2g0o.productlist.0.0.69782259iIzmRA&algo_pvid=292950bb-f188-4d5f-8e18-252b4623d337&aem_p4p_detail=202207201223455479251848430360004520011&algo_exp_id=292950bb-f188-4d5f-8e18-252b4623d337-18&pdp_ext_f=%7B%22sku_id%22%3A%2212000028031787896%22%7D&pdp_npi=2%40dis%21SEK%21%2150.0%21%21%21%21%21%400b0a01f816583450256682666e6d97%2112000028031787896%21sea
Description: Used to connect different components together.
![](https://i.imgur.com/zp32BcW.png)
6.	Breadboard, Price: 13sek, Aliexpress.
Link: https://www.aliexpress.com/item/4000460671949.html?spm=a2g0o.productlist.0.0.3da968e8AG74hP&algo_pvid=4c35a8bd-1822-4583-acaa-80511282fe27&aem_p4p_detail=202207201227098784211915687110004545634&algo_exp_id=4c35a8bd-1822-4583-acaa-80511282fe27-48&pdp_ext_f=%7B%22sku_id%22%3A%2210000001866289737%22%7D&pdp_npi=2%40dis%21SEK%21%2112.79%21%21%21%21%21%400b0a0ae216583452297803355ef91d%2110000001866289737%21sea
Description: Used to build circuits in demonstration purposes, and test early versions of a project.
![](https://i.imgur.com/C34isom.png)
7.	Barlast lamps (3), Price48 se 
      Link: https://www.ikea.com/se/sv/p/barlast-bordslampa-svart-vit-00504557/
Description: the lamps that used in the project.
![](https://i.imgur.com/NPY4uY5.png)
8.	Male to female cable to Dupont wire, Price: 12sek, Aliexpress.
Link:https://www.aliexpress.com/item/1005003912741292.html?spm=a2g0o.productlist.0.0.cdf638c0bEqmAr&algo_pvid=983463f1-7438-4383-ad0c-7ce4b8b4edd4&aem_p4p_detail=202207201238542202496326553950004921248&algo_exp_id=983463f1-7438-4383-ad0c-7ce4b8b4edd4-1&pdp_ext_f=%7B%22sku_id%22%3A%2212000027450941802%22%7D&pdp_npi=2%40dis%21SEK%21%2111.74%21%21%21%21%21%402100bddd16583459342963744e6b58%2112000027450941802%21sea
Description: Connectors that are used most commonly with breadboards.
![](https://i.imgur.com/EY10uKL.png)
9.	Male to male cable to Dupont wire, Price: 12sek, Aliexpress
Link:https://www.aliexpress.com/item/1005003912741292.html?spm=a2g0o.productlist.0.0.cdf638c0bEqmAr&algo_pvid=983463f1-7438-4383-ad0c-7ce4b8b4edd4&aem_p4p_detail=202207201238542202496326553950004921248&algo_exp_id=983463f1-7438-4383-ad0c-7ce4b8b4edd4-1&pdp_ext_f=%7B%22sku_id%22%3A%2212000027450941802%22%7D&pdp_npi=2%40dis%21SEK%21%2111.74%21%21%21%21%21%402100bddd16583459342963744e6b58%2112000027450941802%21sea
Description: Connectors that are used most commonly with breadboards
![](https://i.imgur.com/I9ANTep.png)
10.	ESP32 board, Price: 109sek, Amazon
Link:
https://www.amazon.se/XTVTX-Utvecklingskort-Bluetooth-klockfrekvens-ESP32-modul/dp/B09LCDJY8Z/ref=asc_df_B09LCDJY8Z/?tag=shpngadsglede-21&linkCode=df0&hvadid=476456774343&hvpos=&hvnetw=g&hvrand=8852945577715355607&hvpone=&hvptwo=&hvqmt=&hvdev=c&hvdvcmdl=&hvlocint=&hvlocphy=1012284&hvtargid=pla-1606679765740&psc=1

Description: The motherboard of the entire project. All the processes are done there. 
![](https://i.imgur.com/MEfrlQx.png)
11.	Connectors, Price: 45 sek, kjell&company
Link:
https://www.kjell.com/se/produkter/el-verktyg/el-produkter/svagstrom/kontaktdon-med-snapplas/kontaktpar-med-snapplas-2-poligt-p37400
Description: To make the wires of lights fit for electrical plug.
11.	Connectors, Price: 45 sek, kjell&company
Link:
https://www.kjell.com/se/produkter/el-verktyg/el-produkter/svagstrom/kontaktdon-med-snapplas/kontaktpar-med-snapplas-2-poligt-p37400
Description: To make the wires of lights fit for electrical plug.
![](https://i.imgur.com/sWMlgPS.png)
12.	Usb Cable price 20 sek, Teknikdelar.
Link:
https://www.teknikdelar.se/goobay-usb-2-0-kabel-micro-usb-till-usb-a-1-8m-vit?dfw_tracker=35448-95143&shopping_id_click=95143&gclid=Cj0KCQjwxb2XBhDBARIsAOjDZ37GwV98tNslb0fpsLAWMpQL-pu5uGI_orbblHWOxV9RvJdTtADt7JsaAk4kEALw_wcB
Description: to connect esp32 to computer.
![](https://i.imgur.com/sRlXK68.png)
# Computer setup 
The IDE I choose to work with is Arduino IDE. First creating an account  on Arduino, then logging in to the account, pressing on Things icon, then create things, and setting up a new device, clicking on setup 3rd party device, selecting ESP32, and the model is DOIT ESP32 devkit V1, then I named ESP32 home automation.
After the setting up a new device I  downloaded the device name and the secret key, after this step the device was be added, the next step was creating 4 variables, as I will control 3 relays and temperature, by pressing on add variable, I named switch 1, typed Alexa compatible, switch, the variable permeation (read&write), the variable update (on change), the same steps for switch 2 and 3, the variable 4 named temperature and humidity, typed Alexa compatible, temperature sensor C, the variable permeation (read only), the variable update (periodically every 10 seconds).
Creating Dashboard:
By pressing on add  switch, named switch 1, then I need to link variable to which I had created (switch 1), the same for 2, 3 and temperature.
	
   The code is uploaded to the ESP32 through a micro-usb cable. There were also some steps I had to apply to my computer in order for the project to work. First with the Arduino app open, we go to File> Preferences. Then we look for the “Additional Board Manager URLs” field, and enter the following link to it: (https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json). After that we press “ok”. The following step is to go to Tools > Board > Boards Manager… . Then in the search bar we will look for “ESP32”, and install the package called “ESP32 by Espressif Systems“. And like that the ESP32 should be ready to go.
# Putting everything together
![](https://i.imgur.com/97pgfAW.jpg)
The circuit diagram above is connected as followed:

DHT11 temperature & humidity sensor :
THE Pin (-)  on the right side connected to GND on Breadboard which is connected to GND on esp32.
The middle pin (data) is connected to G16 on esp32.
The left pin (+) on the lift side is connected to V5 on Breadboard which is connected to V5 on esp32.

Switch button 1 :
The right pin is connected to GND on Breadboard.
The lift pin is connected to port number 45 on Breadboard, port number 45 is connected to G14 on esp32.
Switch button 2 :
The right pin is connected to GND on Breadboard.
The lift pin is connected to port number 45 on Breadboard, port number 46 is connected to G12 on esp32.
Switch button 3 :
The right pin is connected to GND on Breadboard.
The lift pin is connected to port number 45 on Breadboard, port number 47 is connected to G13 on esp32.

Relays 4 channel:
The VCC port on Relays is connected to V5 on Breadboard.
The GND port on Relays is connected to GND on Breadboard.
The por IN1 on Relays is connected to pin 23 on esp32.
( Note : in the diagram I have used relay one channel, but in my project, I have used Relays 4 channel)
 
Power Supply:
The power supply is connected to GND and V5 on breadboard.

# Platform 
The Arduino IDE was so flexible and had lots of packages available, which made using it easy and not frustrating as I was expecting. I choose Arduino IDE from among all of the available ones because it was the one with the most tutorials available and its high reviews in addition to it is free. 

# The code 
After the computer setup part, I started by installed two library ArduinoIoTCloud  to the sketch.

Some of the code that’s used:
WiFi Conaction:
const char SSID[]               = "COMHEM_8C7EBE";    //Enter WiFi SSID (name)
const char PASS[]               = "53meyc4y";    //Enter WiFi password
const char DEVICE_KEY[]         = "FUGDRNJNSWC5HIAPJMBW";    //Enter Secret device password (Secret Key)
#define wifiLed    

Temperature & humidity :
#define DHTPIN              16 //G16   
#define DHTTYPE DHT11     // DHT 11
Define Relay and Switch: 
#define RelayPin1 23  //G23
#define RelayPin2 22  //G22
#define RelayPin3 21  //G21
#define SwitchPin1 13  //G13
#define SwitchPin2 12  //G12
#define SwitchPin3 14  //G14

int toggleState_1 = 0; //Define integer to remember the toggle state for relay 1

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

void onSwitch1Change();
CloudSwitch switch1;

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(temperature, READ, 8 * SECONDS, NULL); //Update temperature value after every 10 seconds
}
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);



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;
    

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(wifiLed, OUTPUT);

  pinMode(SwitchPin1, INPUT_PULLUP);
 

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

Main Loop:
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;
  }
}


# Transmitting the data/connectivity
Regarding the transmitting so the data is sent so far, the device is plugged in and running, and it uses WiFi protocol to get in the connection to the Network, also to get the transport well going so I used the Webhook protocol just to get my own HTTP link belonging to the project via port 80. All updates happen directly to that link who synching it at once to Arduino Cloud with good latency about 10 sec but that depends on the traffic as well, so it maybe sees delay sometimes ( when it's plugged and running well )
The reason behind choosing WebHook protocol is that it is basic and simple in addition to it is just an http, unlike MQTT protocol, which is complicated.
# Presenting the data
![](https://i.imgur.com/Agieauu.png)
The data will be saved while the esp32 connected to WiFi, Automation triggers of data used is Amazon Alexa app, by this app and Arduino cloud I can control the project from any place while esp32 was connected to WiFi. 
# Finalizing the project
![](https://i.imgur.com/86oxeeG.jpg)
![](https://i.imgur.com/fBRKccH.jpg)
![](https://i.imgur.com/9briYbe.jpg)
![](https://i.imgur.com/DcGYPut.jpg)
![](https://i.imgur.com/QsPyEyw.jpg)
![](https://i.imgur.com/GhEJ12V.jpg)
![](https://i.imgur.com/DWnpGE3.jpg)
I think that the project could have been done in a better way, but I lack the experience to complete this, for example, the packaging could have been more beautiful.
The project went well, but there were some difficulties, it was supposed to add IR receiver, but it did not work
Now the project is working well and can be controlled through Alexa app, Arduino cloud from anywhere while the esp32 is connected to WiFi, and by push buttons.
The project needs to be developed in order to be connected to the lighting of the house.









