My first real IoT project was a semi-automatic greenhouse system built with the Virinchi Robotics Club. It monitored humidity and temperature, then watered plants and spun fans on its own. This guide distills everything I wish someone had told me before I started, so you can build your own ESP32 home automation setup here in Nepal.
Why the ESP32
If you are choosing your first microcontroller for home automation, the ESP32 is the obvious pick. It costs a few hundred rupees at electronics shops around New Road in Kathmandu, it has built-in WiFi and Bluetooth, plenty of GPIO pins, and it runs on 3.3V logic that plays nicely with common sensors. The Arduino Uno is a fine learning board, but the moment you want your device on the network, the ESP32 saves you an extra WiFi module and a headache.
For this guide you will need an ESP32 dev board, a DHT11 temperature and humidity sensor, a single-channel 5V relay module, a few jumper wires, a breadboard, and a reliable 5V 2A adapter. That last item matters more than you think, which I will explain below.
Wiring the DHT11
The DHT11 is the classic beginner sensor. It reports temperature and humidity over a single digital pin, which keeps wiring trivial. Connect its VCC to the ESP32's 3V3 pin, GND to GND, and the data pin to GPIO 15. Install the DHT sensor library by Adafruit in the Arduino IDE, select your ESP32 board, and flash this sketch:
#include <DHT.h>
#define DHT_PIN 15
#define DHT_TYPE DHT11
DHT dht(DHT_PIN, DHT_TYPE);
void setup() {
Serial.begin(115200);
dht.begin();
}
void loop() {
float humidity = dht.readHumidity();
float tempC = dht.readTemperature();
if (isnan(humidity) || isnan(tempC)) {
Serial.println("Sensor read failed, retrying...");
} else {
Serial.print("Temp: ");
Serial.print(tempC);
Serial.print(" C, Humidity: ");
Serial.print(humidity);
Serial.println(" %");
}
delay(2000);
}
Open the serial monitor at 115200 baud and you should see readings every two seconds. If you see "Sensor read failed" forever, check your wiring first and your pin number second. Nine times out of ten it is one of those two.
Switching real loads with a relay
Reading a sensor is satisfying, but automation means doing something. A relay module lets the ESP32 switch mains-powered devices like a fan or a water pump. Wire the relay's VCC to the ESP32's 5V pin (VIN), GND to GND, and the IN pin to GPIO 23. Then extend the sketch so the fan kicks in when the room gets hot:
#define RELAY_PIN 23
#define TEMP_LIMIT 30.0
void setup() {
Serial.begin(115200);
dht.begin();
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH); // relay off (active-low module)
}
void loop() {
float tempC = dht.readTemperature();
if (!isnan(tempC)) {
if (tempC > TEMP_LIMIT) {
digitalWrite(RELAY_PIN, LOW); // fan ON
} else {
digitalWrite(RELAY_PIN, HIGH); // fan OFF
}
}
delay(2000);
}
A word of caution: most cheap relay modules are active-low, meaning the relay engages when the pin is LOW. Test with an LED first. And please be careful with mains wiring. If you are switching 220V, keep high-voltage connections insulated and away from the breadboard, or ask someone experienced to check your work.
Lessons from load shedding
Here is the part no international tutorial mentions. In Nepal, power cuts and voltage fluctuations are part of life, and they will corrupt your project in confusing ways. Three things I learned the hard way:
- Use a proper power supply. A phone charger that sags under load will brown-out the ESP32 right when the relay clicks. A dedicated 5V 2A adapter fixed random reboots I chased for a week.
- Reconnect WiFi gracefully. If your router reboots, the ESP32 will not reconnect on its own unless you code for it. Add a reconnect loop in your main loop instead of only connecting in
setup(). - Store state in flash. If the power dies mid-cycle, your device should remember whether the pump was on. The ESP32
Preferenceslibrary makes this a five-line job.
Where to go next
Once the sensor and relay are talking, the natural next steps are a small web dashboard served directly from the ESP32, MQTT for talking to other devices, and Home Assistant integration when you are ready for a real platform. My greenhouse project grew exactly this way: sensor readings first, then automatic watering, then a dashboard I could check from my phone.
Start small, get one sensor and one relay working reliably, and build from there. The best automation project is the one that keeps running when you are not watching it.