100% found this document useful (1 vote)
497 views19 pages

Smart Home With Google Assistant & Alexa Using NodeMCU ESP8266 - IotCircuitHub

This document describes a smart home project using a NodeMCU ESP8266 microcontroller to control appliances via Google Assistant, Amazon Alexa, and manual switches. The circuit connects GPIO pins on the NodeMCU to a 4-channel relay module and manual switches. The NodeMCU code uses Sinric Pro to interface with Google Assistant and Alexa for voice control of 3 devices. When run, the NodeMCU can control appliances in real-time from the Google Home and Alexa apps or manually via on-board switches.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
497 views19 pages

Smart Home With Google Assistant & Alexa Using NodeMCU ESP8266 - IotCircuitHub

This document describes a smart home project using a NodeMCU ESP8266 microcontroller to control appliances via Google Assistant, Amazon Alexa, and manual switches. The circuit connects GPIO pins on the NodeMCU to a 4-channel relay module and manual switches. The NodeMCU code uses Sinric Pro to interface with Google Assistant and Alexa for voice control of 3 devices. When run, the NodeMCU can control appliances in real-time from the Google Home and Alexa apps or manually via on-board switches.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

8/3/23, 3:18 PM Smart Home with Google Assistant & Alexa using NodeMCU ESP8266 - IotCircuitHub

Smart Home with Google Assistant & Alexa


using NodeMCU ESP8266
SUBHAJIT / APRIL 21, 2021 / ESP8266 PROJECTS / 21 COMMENTS

In this IoT project, I have explained how to make Smart Home with Google
Assistant and Alexa using NodeMCU ESP8266 and Sinric Pro. With this
NodeMCU ESP8266 project, you can control 3 home appliances with
Google Assistant, Alexa, and manual switches. You can also control the
relays from Google Home and Amazon Alexa App from anywhere in the
world. You can control the relay module from the manual switches if there
is no internet available.

And you don’t need any Google Nest or Amazon Echo Dot devices for
this voice control home automation project.

With this home automation project, you can control & monitor the real-
time feedback of the relays in the Google Home and Alexa App from
anywhere in the world. If the WiFi is available, the NodeMCU will
automatically connect with the Wi-Fi.

https://iotcircuithub.com/smart-home-with-google-assistant-alexa/ 1/19
8/3/23, 3:18 PM Smart Home with Google Assistant & Alexa using NodeMCU ESP8266 - IotCircuitHub

For this project, I have used all the FREE tools. So if you follow all steps,
you can easily make this Smart Home System with Google Home and
Amazon Alexa to control the appliances with voice commands.

Table of Contents

https://iotcircuithub.com/smart-home-with-google-assistant-alexa/ 2/19
8/3/23, 3:18 PM Smart Home with Google Assistant & Alexa using NodeMCU ESP8266 - IotCircuitHub

Circuit of the NodeMCU Home


Automation

The circuit is very simple, I have used D1, D2, D5 & D6 GPIO to control
the 4-channel relay module.

And the GPIO SD3, D3, D7 & RX are connected with manual switches 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 switch.

https://iotcircuithub.com/smart-home-with-google-assistant-alexa/ 3/19
8/3/23, 3:18 PM Smart Home with Google Assistant & Alexa using NodeMCU ESP8266 - IotCircuitHub

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

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

**The Boot will fail if SD3 and D3 are grounded during the Boot
process. So manual switch-S1 and switch-S2 must be OFF during
NodeMCU Boot.

Now, if you want to use pushbuttons then just connect the pushbuttons
across the GPIO pins and GND pin instead of switches.

Required Components for the NodeMCU


projects

1. NodeMCU
2. 4-channel 5V SPDT Relay Module
3. Manual Switches or Pushbuttons
4. Amazon Echo Dot (optional)
5. Google Nest Mini (optional)

Affiliate Purchase Links:

https://iotcircuithub.com/smart-home-with-google-assistant-alexa/ 4/19
8/3/23, 3:18 PM Smart Home with Google Assistant & Alexa using NodeMCU ESP8266 - IotCircuitHub

Tutorial video on Smart Home with


Google & Alexa

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

Control 3 relays with Google Assistant, Alexa, and switches.

https://iotcircuithub.com/smart-home-with-google-assistant-alexa/ 5/19
8/3/23, 3:18 PM Smart Home with Google Assistant & Alexa using NodeMCU ESP8266 - IotCircuitHub

Create an account and add devices in Sinric Pro


Programming the NodeMCU with Arduino IDE
Connect Sinric Pro and add IoT devices with Amazon Alexa App.
Connect Sinric Pro and add IoT devices with Google Home App.

Sinric Pro FREE account setup


For this smart house project, I have used the Sinric Pro Free account. First,
you have to add 3 devices to the Sinric Pro account.

I have already explained, how to set up and add devices to Sinric Pro in
the following article.

Sinric Pro Account Setup

Program NodeMCU 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 ESP8266 board from the Board manager or Click
Here to download the ESP8266 board.
3. Download the required libraries from the following links:

https://iotcircuithub.com/smart-home-with-google-assistant-alexa/ 6/19
8/3/23, 3:18 PM Smart Home with Google Assistant & Alexa using NodeMCU ESP8266 - IotCircuitHub

Sinric Pro by Boris Jaeger (Download Sinric Pro examples for


ESP8266 & ESP32)
WebSockets by Markus Sattler (minimum Version 2.3.5)
ArduinoJson by Benoit Blanchon (minimum Version 6.12.0)

Click Here to visit the GitHub page of Sinric Pro for more details.

**Please download the latest version of the libraries from the given links.
Then install the libraries at Arduino IDE – Sketch – Include Library – Add
Zip Library.

Code for NodeMCU home automation


system
Download the Code

Code_NodeMCU_Google_Alexa_SinricPro_4Relay_Switch.ino

// Uncomment the following line to enable serial debug output


//#define ENABLE_DEBUG

#ifdef ENABLE_DEBUG
#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG
#endif

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include "SinricPro.h"
#include "SinricProSwitch.h"

#include <map>

#define WIFI_SSID "YOUR-WIFI-NAME"


#define WIFI_PASS "YOUR-WIFI-PASSWORD"
#define APP_KEY "YOUR-APP-KEY" // Should look like "de0
#define APP_SECRET "YOUR-APP-SECRET" // Should look like "5f3

//Enter the device IDs here


#define device_ID_1 "SWITCH_ID_NO_1_HERE"
#define device_ID_2 "SWITCH_ID_NO_2_HERE"
#define device_ID_3 "SWITCH_ID_NO_3_HERE"
#define device_ID_4 "SWITCH_ID_NO_4_HERE"

// define the GPIO connected with Relays and switches

https://iotcircuithub.com/smart-home-with-google-assistant-alexa/ 7/19
8/3/23, 3:18 PM Smart Home with Google Assistant & Alexa using NodeMCU ESP8266 - IotCircuitHub

#define RelayPin1 5 //D1


#define RelayPin2 4 //D2
#define RelayPin3 14 //D5
#define RelayPin4 12 //D6

#define SwitchPin1 10 //SD3


#define SwitchPin2 0 //D3
#define SwitchPin3 13 //D7
#define SwitchPin4 3 //RX

#define wifiLed 16 //D0

// comment the following line if you use a toggle switches instead of


//#define TACTILE_BUTTON 1

#define BAUD_RATE 9600

#define DEBOUNCE_TIME 250

typedef struct { // struct for the std::map below


int relayPIN;
int flipSwitchPIN;
} deviceConfig_t;

// this is the main configuration


// please put in your deviceId, the PIN for Relay and PIN for flipSwit
// this can be up to N devices...depending on how much pin's available
// right now we have 4 devicesIds going to 4 relays and 4 flip switche
std::map<String, deviceConfig_t> devices = {
//{deviceId, {relayPIN, flipSwitchPIN}}
{device_ID_1, { RelayPin1, SwitchPin1 }},
{device_ID_2, { RelayPin2, SwitchPin2 }},
{device_ID_3, { RelayPin3, SwitchPin3 }},
{device_ID_4, { RelayPin4, SwitchPin4 }}
};

typedef struct { // struct for the std::map below


String deviceId;
bool lastFlipSwitchState;
unsigned long lastFlipSwitchChange;
} flipSwitchConfig_t;

std::map<int, flipSwitchConfig_t> flipSwitches; // this map is used


// it will be setup

void setupRelays() {
for (auto &device : devices) { // for each device (relay,
int relayPIN = device.second.relayPIN; // get the relay pin
pinMode(relayPIN, OUTPUT); // set relay pin to OUTPUT
digitalWrite(relayPIN, HIGH);
}
}

https://iotcircuithub.com/smart-home-with-google-assistant-alexa/ 8/19
8/3/23, 3:18 PM Smart Home with Google Assistant & Alexa using NodeMCU ESP8266 - IotCircuitHub

void setupFlipSwitches() {
for (auto &device : devices) { // for each devi
flipSwitchConfig_t flipSwitchConfig; // create a new

flipSwitchConfig.deviceId = device.first; // set the devic


flipSwitchConfig.lastFlipSwitchChange = 0; // set debounce
flipSwitchConfig.lastFlipSwitchState = true; // set lastFlipSw

int flipSwitchPIN = device.second.flipSwitchPIN; // get the flipS

flipSwitches[flipSwitchPIN] = flipSwitchConfig; // save the flip


pinMode(flipSwitchPIN, INPUT_PULLUP); // set the
}
}

bool onPowerState(String deviceId, bool &state)


{
Serial.printf("%s: %s\r\n", deviceId.c_str(), state ? "on" : "off");
int relayPIN = devices[deviceId].relayPIN; // get the relay pin for
digitalWrite(relayPIN, !state); // set the new relay sta
return true;
}

void handleFlipSwitches() {
unsigned long actualMillis = millis();
for (auto &flipSwitch : flipSwitches) {
unsigned long lastFlipSwitchChange = flipSwitch.second.lastFlipSwi

if (actualMillis - lastFlipSwitchChange > DEBOUNCE_TIME) {

int flipSwitchPIN = flipSwitch.first;


bool lastFlipSwitchState = flipSwitch.second.lastFlipSwitchState
bool flipSwitchState = digitalRead(flipSwitchPIN);
if (flipSwitchState != lastFlipSwitchState) {
#ifdef TACTILE_BUTTON
if (flipSwitchState) {
#endif
flipSwitch.second.lastFlipSwitchChange = actualMillis;
String deviceId = flipSwitch.second.deviceId;
int relayPIN = devices[deviceId].relayPIN;
bool newRelayState = !digitalRead(relayPIN);
digitalWrite(relayPIN, newRelayState);

SinricProSwitch &mySwitch = SinricPro[deviceId];


mySwitch.sendPowerStateEvent(!newRelayState);
#ifdef TACTILE_BUTTON
}
#endif
flipSwitch.second.lastFlipSwitchState = flipSwitchState;
}
}
}
}

https://iotcircuithub.com/smart-home-with-google-assistant-alexa/ 9/19
8/3/23, 3:18 PM Smart Home with Google Assistant & Alexa using NodeMCU ESP8266 - IotCircuitHub

void setupWiFi()
{
Serial.printf("\r\n[Wifi]: Connecting");
WiFi.begin(WIFI_SSID, WIFI_PASS);

while (WiFi.status() != WL_CONNECTED)


{
Serial.printf(".");
delay(250);
}
digitalWrite(wifiLed, LOW);
Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.loc
}

void setupSinricPro()
{
for (auto &device : devices)
{
const char *deviceId = device.first.c_str();
SinricProSwitch &mySwitch = SinricPro[deviceId];
mySwitch.onPowerState(onPowerState);
}

SinricPro.begin(APP_KEY, APP_SECRET);
SinricPro.restoreDeviceStates(true);
}

void setup()
{
Serial.begin(BAUD_RATE);

pinMode(wifiLed, OUTPUT);
digitalWrite(wifiLed, HIGH);

setupRelays();
setupFlipSwitches();
setupWiFi();
setupSinricPro();
}

void loop()
{
SinricPro.handle();
handleFlipSwitches();
}

Enter the APP KEY and APP SECRET with Wi-Fi name and Wi-Fi password
in the code.

https://iotcircuithub.com/smart-home-with-google-assistant-alexa/ 10/19
8/3/23, 3:18 PM Smart Home with Google Assistant & Alexa using NodeMCU ESP8266 - IotCircuitHub

You can get the APP KEY and APP SECRET under the Credentials menu
in Sinric Pro

#define WIFI_SSID "YOUR-WIFI-NAME"


#define WIFI_PASS "YOUR-WIFI-PASSWORD"
#define APP_KEY "YOUR-APP-KEY"
#define APP_SECRET "YOUR-APP-SECRET"

Also, enter the device id in the code. You will find the Device ID from the
Devices menu.

//Enter the device IDs here


#define device_ID_1 "SWITCH_ID_NO_1_HERE"
#define device_ID_2 "SWITCH_ID_NO_2_HERE"
#define device_ID_3 "SWITCH_ID_NO_3_HERE"
#define device_ID_4 "SWITCH_ID_NO_4_HERE"

**When you add a device in Sinric Pro, a unique ID is assigned to that


device. If you create 3 devices, then there will be 3 unique device IDs.

As I have used the free Sinric pro account, so I have entered the 3-device
IDs. (Sinric Pro gives 3 devices for FREE)

Uncomment the following line if you use pushbuttons instead of toggle


switches.

//#define TACTILE_BUTTON 1

After uploading the code to NodeMCU, please refer to the following


articles for connecting the Sinric Pro Account with Amazon Alexa and
Google Home App.

Connect Sinric Pro with Alexa App


Connect Sinric Pro with Google Home App

https://iotcircuithub.com/smart-home-with-google-assistant-alexa/ 11/19
8/3/23, 3:18 PM Smart Home with Google Assistant & Alexa using NodeMCU ESP8266 - IotCircuitHub

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

NodeMCU control Relays with Alexa App

If the NodeMCU 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 light, fan, etc with voice commands using Amazon Alexa
App, and also monitor the current status of the switches from anywhere in
the world from the Alexa App.

NodeMCU control Relays with Google


Assistant

https://iotcircuithub.com/smart-home-with-google-assistant-alexa/ 12/19
8/3/23, 3:18 PM Smart Home with Google Assistant & Alexa using NodeMCU ESP8266 - IotCircuitHub

You can also ask Google Assistant, to turn on the light [“Hey Google,
Turn ON the Room Light“]. Thus, you can control the appliances like
light, fan, etc with voice commands using Google Assistant, and also
monitor the current status of the switches from anywhere in the world
from the Google Home App.

Control relays manually with Switches

Control relays with Switches No Wi-Fi Control with Switches

You can always control the appliances manually with switches or push
buttons. and if the NodeMCU is connected with the Wi-Fi, then you can
monitor the real-time status in Google Home and Alexa App.

PCB for the NodeMCU Smart Home

https://iotcircuithub.com/smart-home-with-google-assistant-alexa/ 13/19
8/3/23, 3:18 PM Smart Home with Google Assistant & Alexa using NodeMCU ESP8266 - IotCircuitHub

Download PCB Gerber File

If you want, you can also use this PCB to make the circuit compact and
give the project a professional look.

I hope you like this Smart house IoT projects idea with the NodeMCU
ESP8266.

Click Here for more such ESP8266 projects.

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

https://iotcircuithub.com/smart-home-with-google-assistant-alexa/ 14/19
8/3/23, 3:18 PM Smart Home with Google Assistant & Alexa using NodeMCU ESP8266 - IotCircuitHub

# Alexa # Google # Intermediate # NodeMCU # Sinric Pro

# Smart Home

PREVIOUS NEXT

Related Posts

https://iotcircuithub.com/smart-home-with-google-assistant-alexa/ 15/19
8/3/23, 3:18 PM Smart Home with Google Assistant & Alexa using NodeMCU ESP8266 - IotCircuitHub

Tasmota ESP01 Alexa control relay


June 25, 2023

ESP01 Relay Project using Blynk IoT


May 30, 2023

https://iotcircuithub.com/smart-home-with-google-assistant-alexa/ 16/19
8/3/23, 3:18 PM Smart Home with Google Assistant & Alexa using NodeMCU ESP8266 - IotCircuitHub

ESP8266 Cadio Home Automation Project


May 5, 2023

Explore simple IoT Projects on Home Automation, Internet of Things using


Arduino, ESP8266, ESP32, ESP32 CAM, LoRa for engineering students. All
projects explained with source code, circuit diagram, woking principle.

Categories:

Arduino Projects (9)


ESP32 Projects (30)
ESP32-CAM Projects (2)
ESP8266 Projects (20)
Tutorials (15)

Translate:

https://iotcircuithub.com/smart-home-with-google-assistant-alexa/ 17/19
8/3/23, 3:18 PM Smart Home with Google Assistant & Alexa using NodeMCU ESP8266 - IotCircuitHub

Tags:

Alexa Arduino Beginners Bluetooth Blynk

Cadio DHT11 EEPROM ESP01 ESP32

ESP32-CAM ESP RainMaker Google IFTTT

Intermediate IR Remote LDR LED LoRa

NodeMCU PIR Sensors Sinric Pro Smart Home

Tasmota Telegram

Latest Posts:

Tasmota ESP01 Alexa control relay


June 25, 2023

ESP01 Relay Project using Blynk IoT


May 30, 2023

ESP8266 Cadio Home Automation Project


May 5, 2023

Universal Remote Control using ESP8266 Cadio


April 25, 2023

Steps to Install Cadio Firmware on ESP8266


April 25, 2023

Copyright © 2023 - IotCircuitHub

Home Contact Privacy Policy Disclaimer About Affiliate Disclosure

https://iotcircuithub.com/smart-home-with-google-assistant-alexa/ 18/19
8/3/23, 3:18 PM Smart Home with Google Assistant & Alexa using NodeMCU ESP8266 - IotCircuitHub

https://iotcircuithub.com/smart-home-with-google-assistant-alexa/ 19/19

You might also like