Smart Lighting with Alexa & Google Home
Build a complete voice-controlled smart lighting system using NodeMCU ESP8266, relay modules, and Home Assistant. Control your lights with Alexa and Google Assistant.
Table of Contents
1. Project Overview
In this project, we'll build a complete smart lighting system that integrates with both Amazon Alexa and Google Home. You'll be able to control your lights with voice commands like "Alexa, turn on the living room lights" or "Hey Google, dim the bedroom lights to 50%".
- WiFi-controlled relay module using NodeMCU ESP8266
- Home Assistant integration for centralized control
- Amazon Alexa voice control via HA-Bridge or Nabu Casa
- Google Home integration via Home Assistant Cloud
- Mobile app control through Home Assistant
- Automation routines (sunset/sunrise, schedules)
This system is expandable - start with one light and add more channels as needed. You can control lamps, ceiling lights, LED strips, or any AC appliance safely.
2. Required Components
Total cost: $25-40 for a 1-channel system, $40-60 for 4-channel. Much cheaper than commercial smart switches ($50+ per switch)!
3. Understanding Relay Modules
A relay is an electrically operated switch. It allows low-voltage DC (from NodeMCU) to control high-voltage AC (your lights) safely through electromagnetic isolation.
Relay Module Pinout
DC Side (Low Voltage) AC Side (High Voltage)
┌─────────────────┐ ┌──────────────────────┐
│ VCC ─── 5V │ │ COM ─── Live In │
│ GND ─── GND │ │ NO ─── Live Out │
│ IN1 ─── GPIO │ │ NC ─── (Unused) │
└─────────────────┘ └──────────────────────┘
Control Side Load Side
Relay Types
| Type | Active | Description |
|---|---|---|
| Active HIGH | 3.3V/5V | Relay turns ON when GPIO is HIGH |
| Active LOW | GND | Relay turns ON when GPIO is LOW (more common) |
Most relay modules are Active LOW, meaning they trigger when the input pin is connected to GND. This is opposite to what you might expect!
4. Hardware Setup
Low-voltage DC wiring:
- Relay VCC → NodeMCU VIN (5V) or external 5V
- Relay GND → NodeMCU GND
- Relay IN1 → NodeMCU D1 (GPIO 5)
NodeMCU ESP8266 Pinout:
┌─────────────────┐
│ VIN GND │ VIN = 5V power in
│ D0 D1 │ D1 = GPIO 5 (relay control)
│ D2 D3 │ D0 = GPIO 16
│ D4 D5 │ D4 = GPIO 2 (built-in LED)
│ D6 D7 │
│ D8 RX │
│ TX RST │
└─────────────────┘
Connect your light bulb through the relay:
- AC Live (Brown/Black) → Relay COM
- Relay NO (Normally Open) → Light Bulb Live
- AC Neutral (Blue/White) → Light Bulb Neutral
- AC Ground (Green/Yellow) → Light Bulb Ground
- ⚠️ AC mains electricity can KILL you
- Turn off power at the circuit breaker before wiring
- Use a proper electrical enclosure
- Don't touch exposed wires
- If unsure, consult a licensed electrician
- Never work on live circuits
5. NodeMCU Programming
Option 1: Arduino IDE with MQTT
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// WiFi credentials
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
// MQTT Broker
const char* mqtt_server = "192.168.1.100"; // Your broker IP
const int mqtt_port = 1883;
// Relay pin
#define RELAY_PIN D1 // GPIO 5
// MQTT Topics
const char* lightTopic = "home/livingroom/light";
const char* lightStateTopic = "home/livingroom/light/state";
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
delay(10);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
void reconnect() {
while (!client.connected()) {
String clientId = "NodeMCU-" + String(random(0xffff), HEX);
if (client.connect(clientId.c_str())) {
client.subscribe(lightTopic);
} else {
delay(5000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
String message;
for (int i = 0; i < length; i++) {
message += (char)payload[i];
}
if (message == "ON") {
digitalWrite(RELAY_PIN, LOW); // Active LOW
client.publish(lightStateTopic, "ON");
} else if (message == "OFF") {
digitalWrite(RELAY_PIN, HIGH); // Active LOW
client.publish(lightStateTopic, "OFF");
}
}
void setup() {
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH); // Start OFF (Active LOW)
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) reconnect();
client.loop();
}
Option 2: ESPHome (Recommended for Home Assistant)
ESPHome is the easiest way to integrate with Home Assistant:
# living_room_light.yaml
esphome:
name: living_room_light
platform: ESP8266
board: nodemcuv2
wifi:
ssid: "YOUR_WIFI_SSID"
password: "YOUR_WIFI_PASSWORD"
# Enable Home Assistant API
api:
password: "your_api_password"
ota:
password: "your_ota_password"
# Relay configuration
switch:
- platform: gpio
name: "Living Room Light"
pin: D1
id: living_room_relay
restore_mode: ALWAYS_OFF
# Optional: Status LED
status_led:
pin:
number: D4
inverted: true
Upload to NodeMCU:
# Install ESPHome
pip install esphome
# Compile and upload
esphome living_room_light.yaml run
6. Home Assistant Setup
Install Home Assistant
Choose one of these installation methods:
- Home Assistant OS: Complete OS for Raspberry Pi or VM
- Home Assistant Container: Docker installation
- Home Assistant Supervised: On existing Linux
# Docker installation example
docker run -d \
--name homeassistant \
--privileged \
--restart=unless-stopped \
-e TZ=MY_TIME_ZONE \
-v /path/to/config:/config \
--network=host \
ghcr.io/home-assistant/home-assistant:stable
Add ESPHome Integration
- Open Home Assistant UI
- Go to Settings → Devices & Services
- Click Add Integration
- Search for ESPHome
- Your NodeMCU should appear automatically
- Click Submit and enter API password
Configure MQTT Light (Alternative)
Add to your configuration.yaml:
mqtt:
switch:
- name: "Living Room Light"
state_topic: "home/livingroom/light/state"
command_topic: "home/livingroom/light"
payload_on: "ON"
payload_off: "OFF"
qos: 1
retain: true
7. Alexa Integration
Option 1: Home Assistant Cloud (Nabu Casa) - Easiest
- In Home Assistant, go to Settings → Home Assistant Cloud
- Click Start Free Trial ($6.99/month after 1 month free)
- Create account and log in
- Enable Alexa integration
- Say: "Alexa, discover devices"
- Your lights will appear in Alexa app
Option 2: HA-Bridge (Free, More Complex)
HA-Bridge emulates Philips Hue bridge for Alexa:
# Docker installation
docker run -d \
--name ha-bridge \
--net=host \
-v /path/to/config:/config \
bwssytems/ha-bridge
- Access HA-Bridge at
http://your-ip:80 - Add new device with MQTT commands
- Set On/Off URLs to publish MQTT messages
- In Alexa app: Devices → + → Add Device
- Say: "Alexa, discover devices"
Option 3: Home Assistant Skill (Legacy)
Use the official Home Assistant skill in Alexa app (requires port forwarding or Duck DNS).
8. Google Home Integration
Home Assistant Cloud (Recommended)
- In Home Assistant Cloud, enable Google Assistant
- Link your Home Assistant account in Google Home app
- Say: "Hey Google, sync my devices"
- Control lights with: "Hey Google, turn on living room light"
Manual Configuration
Expose specific entities to Google:
# configuration.yaml
google_assistant:
project_id: YOUR_PROJECT_ID
service_account:
key_file: /config/google_credentials.json
expose_by_default: false
exposed_domains:
- switch
- light
entity_config:
switch.living_room_light:
expose: true
room: Living Room
aliases:
- Main Light
- Ceiling Light
9. Voice Commands & Routines
Basic Voice Commands
| Alexa | Google Home | Action |
|---|---|---|
| "Turn on living room light" | "Turn on living room light" | Switch ON |
| "Turn off living room light" | "Turn off living room light" | Switch OFF |
| "Dim living room light to 50%" | "Set living room light to 50%" | Dim (if supported) |
| "Brighten living room light" | "Brighten living room light" | Increase brightness |
Creating Routines
Alexa Routine - "Good Morning":
- Open Alexa app → More → Routines
- + → When: Voice → "Good morning"
- Add action: Smart Home → Turn on living room light
- Add action: Smart Home → Turn on kitchen light
- Add action: Music → Play morning news
Google Routine - "Good Night":
- Open Google Home app → Routines
- + → Starter: "Hey Google, good night"
- Add action: Turn off all lights
- Add action: Set thermostat to 68°F
- Add action: Play sleep sounds
Home Assistant Automations
# Automations in configuration.yaml
automation:
- alias: "Sunset - Turn on lights"
trigger:
platform: sun
event: sunset
action:
service: switch.turn_on
target:
entity_id: switch.living_room_light
- alias: "Motion - Turn on hallway light"
trigger:
platform: state
entity_id: binary_sensor.hallway_motion
to: "on"
action:
service: switch.turn_on
target:
entity_id: switch.hallway_light
data:
duration: 60 # Turn off after 60 seconds
10. Safety Considerations
- Always turn off power at breaker before working with AC
- Use a voltage tester to confirm power is off
- Never work on live circuits
- Use proper wire gauge for your load (14-16 AWG for lights)
- Ensure all connections are tight and secure
- Use wire nuts or Wago connectors, not electrical tape
- Install in a proper electrical enclosure
- Don't exceed relay current rating (typically 10A)
Best Practices
- Use LED bulbs (lower power, less heat)
- Label all wires before disconnecting
- Take photos of original wiring
- Test with low voltage first
- Keep DC and AC wiring separated
- Use strain relief on all cables
- Ground metal enclosures
11. Troubleshooting
Problem: NodeMCU Won't Connect to WiFi
- Check SSID and password (case-sensitive)
- Ensure 2.4GHz network (ESP8266 doesn't support 5GHz)
- Move closer to router
- Check MAC filtering on router
- Try static IP configuration
Problem: Relay Clicks But Light Doesn't Turn On
- Check AC wiring connections
- Verify light bulb works
- Test relay with multimeter (continuity between COM and NO)
- Ensure relay is rated for your voltage/current
Problem: Home Assistant Can't Find Device
- Ensure NodeMCU and HA are on same network
- Check API password matches
- Verify mDNS is working
- Try static IP for NodeMCU
- Check firewall settings
Problem: Alexa/Google Can't Discover Devices
- Ensure entities are exposed to cloud
- Check Home Assistant Cloud status
- Try "Alexa/Google, sync my devices"
- Verify entity has unique name
- Check entity is not disabled
Next Steps
Expand your smart home system:
- Add more lights and rooms
- Integrate motion sensors for automation
- Add dimmer support with PWM
- Create scenes (Movie Time, Dinner Party)
- Integrate with smart switches
- Add energy monitoring
Related Articles:
Smart Home Temperature Monitoring with ESP32 |
Smart Door Lock with Fingerprint & RFID
Useful Tools:
MQTT Message Generator |
MQTT Connection Generator