Wireless Notes
Complete WiFi home automation project using ESP32, MQTT protocol, relay modules, and mobile app control with step-by-step circuit, code, and wireless communication concepts demonstrated.
Build a complete WiFi-based home automation system using ESP32 microcontrollers, MQTT messaging protocol, relay modules for appliance control, and a mobile app dashboard for remote monitoring and control.
System Architecture
| Component | Role | Technology |
|---|---|---|
| ESP32 nodes | Control appliances, read sensors | WiFi 802.11 b/g/n |
| MQTT Broker | Message routing between all components | Mosquitto on Raspberry Pi |
| Relay modules | Switch AC appliances on/off | 5V 4-channel optoisolated |
| Sensors | Monitor temperature, motion, light | DHT22, PIR, LDR |
| Mobile App | User interface for control | MIT App Inventor / Flutter |
| Power monitoring | Track energy consumption | PZEM-004T module |
Communication Flow
| [Mobile App] ←──MQTT/WiFi── | [Broker (RPi)] ←──MQTT/WiFi──→ [ESP32 Node 1: Living Room] |
| ↕ [ESP32 Node 2 | Bedroom] |
| [ESP32 Node 3 | Kitchen] |
Hardware Design
ESP32 Relay Interface
Each ESP32 controls a 4-channel relay module that can switch AC appliances (lights, fans, water heaters). The relay module provides optical isolation (optocoupler) between the ESP32's 3.3V logic and the 230V AC mains — essential for safety.
| Connection | From | To | Purpose |
|---|---|---|---|
| IN1-IN4 | ESP32 GPIO 25-28 | Relay module input | Control signals |
| VCC | ESP32 5V (USB) | Relay VCC | Relay coil power |
| GND | ESP32 GND | Relay GND | Common ground |
| COM/NO | Relay output | AC load | Switching mains power |
Safety Warning: Working with mains voltage (230V AC) is dangerous. Use proper insulation, fuse protection, and consider using an electrician for the AC wiring portion. The low-voltage ESP32 and relay control side is safe to work with.
Sensor Integration
| Sensor | Measurement | Interface | Update Rate |
|---|---|---|---|
| DHT22 | Temperature + Humidity | Digital (single wire) | Every 30 seconds |
| PIR (HC-SR501) | Motion detection | Digital (interrupt) | Instant on motion |
| LDR | Ambient light level | Analog (ADC) | Every 10 seconds |
| PZEM-004T | Voltage, Current, Power, Energy | UART/Modbus | Every 5 seconds |
WiFi Communication Details
ESP32 WiFi Capabilities
The ESP32 integrates a complete WiFi 802.11 b/g/n stack:
| Feature | Specification |
|---|---|
| Standards | 802.11 b/g/n (2.4 GHz only) |
| Max data rate | 150 Mbps (802.11n, 20 MHz) |
| TX power | Up to 20.5 dBm |
| Sensitivity | -97 dBm (1 Mbps) |
| Security | WPA/WPA2/WPA3 |
| Range (indoor) | 15-30 meters through walls |
| Simultaneous connections | Station + AP mode |
WiFi Provisioning
Hardcoding WiFi credentials is impractical for a deployable product. We implement WiFi provisioning:
- On first boot (or credential failure), ESP32 starts as an Access Point ("HomeAuto_Setup")
- User connects to this AP from their phone
- A captive portal webpage lets user enter home WiFi SSID and password
- ESP32 saves credentials to flash memory and restarts in Station mode
- Subsequent boots connect automatically to the saved network
This approach means you never need to reprogram the device to change WiFi networks.
Connection Reliability
WiFi connections can drop due to router reboots, interference, or signal issues. Our firmware includes:
- Auto-reconnection — Detects WiFi loss and retries every 5 seconds
- MQTT reconnection — Re-establishes broker connection after WiFi recovery
- Offline operation — Physical switches still work even without WiFi
- Watchdog timer — Automatic ESP32 restart if firmware hangs
MQTT Implementation
Topic Structure
A well-designed topic hierarchy enables scalable device management:
| home/living-room/light1/command | "ON" or "OFF" |
| home/living-room/light1/status | "ON" or "OFF" (confirmed state) |
| home/living-room/fan/speed | "0", "1", "2", "3" |
| home/living-room/temperature | "28.5" |
| home/bedroom/light1/command | "ON" or "OFF" |
| home/kitchen/gas-alert | "SAFE" or "DANGER" |
| home/energy/power | "1250" (watts) |
| home/energy/daily-kwh | "8.5" |
QoS Selection
| Message Type | QoS | Reasoning |
|---|---|---|
| Sensor readings | 0 | Next reading comes in seconds; lost one is harmless |
| Control commands | 1 | Must reach device; duplicate execution is acceptable |
| Gas/safety alerts | 2 | Exactly-once delivery critical for safety |
| Status confirmations | 1 | Dashboard needs reliable state sync |
Automation Rules
Beyond manual control, the system implements automation rules processed on the Raspberry Pi:
| Rule | Trigger | Action |
|---|---|---|
| Auto-lights | PIR motion + LDR < threshold | Turn on room lights |
| Night mode | Time > 11 PM | Dim all lights to 30% |
| Energy saver | No motion for 15 min | Turn off room appliances |
| Morning routine | Time = 6:30 AM | Turn on geyser, kitchen light |
| Safety | Gas sensor > threshold | Cut gas valve, sound alarm, send notification |
| Temperature | Temp > 30°C | Turn on fan speed 3 |
Mobile App Design
The mobile app provides:
- Room-by-room control — Toggle switches for each appliance
- Real-time status — LED indicators showing current device state
- Sensor dashboard — Temperature, humidity, power consumption graphs
- Scheduling — Set times for automatic on/off
- Notifications — Push alerts for motion detection, gas leaks, power outages
- Voice control — Integration with Google Assistant via IFTTT
Power Monitoring
The PZEM-004T module provides non-invasive power monitoring:
- Real-time power consumption (watts)
- Daily/monthly energy usage (kWh)
- Voltage and current measurements
- Power factor calculation
- Cost estimation based on per-unit rate
This data helps users understand consumption patterns and identify energy-wasting appliances.
Wireless Concepts Demonstrated
| Concept | Implementation |
|---|---|
| WiFi Station mode | ESP32 connects to home router |
| WiFi AP mode | Provisioning portal for initial setup |
| TCP/IP stack | MQTT runs over TCP (port 1883) |
| DHCP | Automatic IP assignment from router |
| mDNS | Access ESP32 by name (homeauto.local) |
| Publish-Subscribe | Decoupled communication via MQTT |
| QoS levels | Appropriate reliability per message type |
| Retained messages | Dashboard gets current state on connection |
Troubleshooting
| Issue | Cause | Solution |
|---|---|---|
| ESP32 keeps disconnecting | WiFi signal weak | Add mesh router or move ESP32 closer |
| Relay clicks but device stays off | Wiring issue at NO/COM | Check AC connections with multimeter |
| App shows stale data | MQTT not using retained messages | Enable retain flag on status publishes |
| High latency (> 2 seconds) | Cloud broker (not local) | Use local Mosquitto on RPi |
| ESP32 resets randomly | Power supply insufficient | Use dedicated 5V 2A supply per node |
Key Takeaways
- WiFi-based home automation leverages existing router infrastructure — no additional hub or proprietary gateway hardware needed
- ESP32 provides a complete WiFi stack with station mode, AP mode, and simultaneous operation for provisioning
- MQTT publish-subscribe architecture decouples devices from controllers, enabling scalable multi-room automation
- Physical switches must work independently of WiFi — always implement local control as fallback
- Auto-reconnection logic is essential for reliability since WiFi connections will inevitably drop periodically
- Power monitoring transforms the system from simple control to intelligent energy management with consumption insights
- The same architecture (ESP32 + MQTT + broker) scales from a single-room demo to a complete multi-floor home automation system
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for WiFi Based Home Automation Project ESP32 MQTT App Control.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Wireless Communications topic.
Search Terms
wireless-communications, wireless communications, wireless, communications, projects, wifi, based, home
Related Wireless Communications Topics