Build Your Own Google Home WiFi Control Smart Switch

Shahariar Rabby
10 min readJun 7, 2020

--

Controlling the lights in your house using your voice is a great way to dip your toes into the smart homeworld. And if you already have a smartphone you can build your own smart switch using a few bucks.

You could go with smart bulbs or fan instead, but smart switches are already costly. So if you want to save some money and code your own; this tutorial is for you.

Note: This tutorial needs to work with a higher AC voltage. Please contact a good electrician to set up the process. Also, read this 21 rule.

Hardwear Requirements

  • NodeMCU ESP8266
  • 4 channel Relay Board (3v or 5v)
  • Female to Female Jumper Cables
  • Electric Cable
  • Call bell switch/ Push Switch
Hardwear Requirements

Software Requirements

Install NodeMCU ESP8266 Driver and Library

File -> Preferences

5. Go to “Tools -> Board -> Boards Manager”, type “ESP8266” and install.

Tools -> Board -> Boards Manager

6. Go again to “Tools -> Board” and select “Generic ESP8266 Module”.

Install Blynk Library using built-in library manager in Arduino IDE

To install a new library into your Arduino IDE you can use the Library Manager (available from IDE version 1.6.2). Open the IDE and click to the “Sketch ->Include Library -> Manage Libraries”. or press “Ctrl+Shift+I”.

Sketch ->Include Library -> Manage Libraries

Then the Library Manager will open and you will find a list of libraries that are already installed or ready for installation. Search for Blynk library and in the version, selection chooses the latest version to date.

Blynk

Finally, click on Install and wait for the IDE to install the new library. Downloading may take time depending on your connection speed. Once it has finished, the Installed tag should appear next to the Bridge library. You can close the library manager. You can now find the new library available in the “Sketch > Include Library” menu.

Read this if you still having any issues.

Installing the Blynk App on the Smartphone

  • Open the Play Store and install the Blynk App.
Blynk App
  • Once the app is installed either login to it using Facebook or create a new account on Blynk and log in using that.
  • After logging in, create a new project by clicking ‘New Project’.
New Project
  • Give the project a name of your liking, in my case, it’s “My Room”. Select the hardware device as NodeMCU and select the connection type as WIFI, and hit create.
New Project
  • Now Blynk will send an Auth token to your email id. We will use this “Auth token” later in the tutorial to link our app with the NodeMCU.
  • Now since we are using a four-channel relay, I’ll add 4 buttons on the blank project. To add a new button just click anywhere on the blank area and select button from the side menu that pops up. You can place the button anywhere on the screen.
New Button
  1. Click on the “Button ” and in the textbox select the pin as digital pin V13. Below the textbox, give it an “ON/OFF Labels”. I’ll name it “Light 1” as I’ll use it to control light. To know about the Virtual Pin, Check this.
New Button Configuration
  • Repeat the process 3 more times and add three more buttons. Naming them anything you like And choosing their Virtual Pins as V12, V14, and V15.
All Button

Now, the Blynk app is all set.

Uploading the code to NodeMCU

  • Connect the NodeMCU to your PC using a USB cable.
  • Now, we’ll set up the Arduino IDE by changing some settings. So, open up the Arduino IDE.
  • Go to Tools > Port and make sure an appropriate port is selected. In my case it’s COM 3. This is the USB port in which the NodeMCU is connected.
COM Port

Code: https://github.com/ShahariarRabby/Blynk-Home-Automation

  • Now, in this file, we only need to change 3 lines of code. Add your WiFi SSID, Password, and Auth token from the email in the code.
  • That’s really all the code that we need to write! We are now ready to upload this code to the NodeMCU. So directly hit the upload button at the top (besides the button that has a checkmark), and wait for it to process.
Upload
  • The code will be uploaded to the NodeMCU and the next time you power it on, it will automatically connect to the specified WIFI network.
  • Press the play button in the Blynk app on your phone and you will see it is connected.
App
  • if you see the device is not connected check your WiFi SSID, Password, and Auth token from the email. Also, check the serial monitor in the IDE and set baud rate 11520.
Serial monitor

Check this video if you need more explanation about IDE: https://youtu.be/FhS44hGk1Lc

Hardware Assembly

  1. We’ll have to connect the NodeMCU with the Relay board, you can choose to do it with a breadboard or without.
  2. Connect the D0 pin of NodeMCU with Pin 1 of Relay. Similarly connect D1 pin of NodeMCU with Relay pin 2, D2 with Relay 3, and D3 with Relay 1.
  3. Connect the D5, D6, D7, and TX pin to a Push Button or One way or doorbell switch.
  4. Connect Ground Pin of Relay with Ground Pin of NodeMCU.
  5. Connect VCC of the relay module with a 5v power supply. If the relay module is 3v, then connect VCC with NodeMCU 3.3v (No need for extra power supply).
  6. Power up both Nodemcu and Realy module with 5v power supply.
  7. As we are using a four-channel relay you can connect at most 4 electronic appliances to the Relay and control them over the internet.
Circuit Diagram

Now if you want to connect your household appliances like Fan, Lights, etc. which are connected to the main power of your house, I would recommend you take the help of a professional electrician and ask him/her to connect those appliances to the relay. Because working with the mains is no joke and if not done properly, it can cause serious damage.

At this point, we have a fully functional connection between the NodeMCU, Blynk app, and our electrical appliances. So, you can directly run your Blynk project from your phone and turn the electrical appliances on or off using the buttons that we created in the app. And if you are satisfied with this and don’t want to connect the NodeMCU with the Google Assistant and control the appliances using voice commands, then you don’t have to read the remaining tutorial and you can stop right here. Otherwise, let’s move forward.

Connecting Google Assistant (using IFTTT) to make the NodeMCU work with voice commands.

We cannot connect the Google Assistant to the NodeMCU directly, and that is the only reason we are using the Blynk app. Blynk app can directly connect to the NodeMCU and send data to it. So, if we can send the voice commands interpreted by google assistant directly to the Blynk app, the Blynk app can then forward those commands to the NodeMCU. But the problem is Google Assistant cannot directly understand foreign commands like “turn on the fan” or “turn on relay one” etc. on its own. So, to solve this we use another intermediate app/website called ‘IFTTT’.

Simply, to control our home appliances over the internet we are using NodeMCU and to connect NodeMCU with the home appliances we use a relay board. Now to send on or off signals to the NodeMCU we use our smartphone, and we do this using the Blynk app. But we want to send the on or off signals using voice commands. To do this we use google assistants in our smartphone and an app called IFTTT.

So, in the end, what will happen is, when we say a voice command like “ok google turn on the light” to the Google Assistant, Google Assistant sends that this foreign command to IFTTT. IFTTT interprets this command and sends an On or Off signal to the Blynk app via the Blynk Server. Blynk will then send this signal to the NodeMCU and then to our electrical appliances.

  1. Enough said, let's configure IFTTT. Go to IFTTT’s website and sign up for it using your Google Account.
Sign Up

2. After Signing in click on your profile icon on top right and select create.

Create Applets

3. Click on “+This”.

+This

4. Search for Google Assistant and select it. And then Click on Connect.

Google Assistant

5. At this point, IFTTT will ask you permission to use your google account to add voice commands to it. Which you simply allow by clicking on ‘Allow’.

6. Select the card that says “Say a simple phrase”.

Say a simple phrase

7. Next, for the first textbox type the phrase that you want to say to Google Assistant. It can be anything such as “Turn on the T.V”, “Turn on the fan” or anything you like.

8. For the next two text boxes, you write some other ways to say the first command. For example, if in the first textbox you wrote “Turn on Fan”, then in the second and third text boxes you can write something like “Fan On” or “On fan” or “In the Fan”.

9. In the fourth textbox type the reply that Google Assistant should respond with. For example, “Turning on the Fan…”.

10. Finally, click on ‘Create Trigger’.

11. Now, click on “+That

+That

12. And type “Webhooks”, select it and click connect. Webhooks will allow us to send commands to the Blynk server.

13. Click “Make a web request” :

Make a web request

14. now, in the URL field type this URL: http://188.166.206.43/ YourAuthTokenHere / update / virtualPinNumber?value=<1 or 0>

15. This is the URL of the Blynk Server. Replace the “YourAuthTokenHere” part with your Blynk Auth token that you received in the mail. And “virtualPinNumber” part with the virtual pin from App that is to be updated. We assigned the Digital Pin D0 of NodeMCU to relay 1 and D5 with push-button and map that two-button with a virtual pin V13 in our code.

16. So, we will write v13 at the end of our code to update it.
http://188.166.206.43/ YourAuthTokenHere/ update / v15

17. Then we need to set the value for the virtual pin, where 0 in on and 1 mean off. In this website, we set the value to 0 to turn on the light. http://188.166.206.43/YourAuthTokenHere/update/V13?value=0

18. Next, select the ‘Method’ field as GET

Webhook

19. Now click on ‘Create Action’ and then Finish.

20. Similarly, we create another applet to turn off the relay. Repeat all the steps above from step 2 except the following changes:

21. In steps 8 and 9, instead of writing “Turn on the Fan”, type “Turn off the Fan”

22. In step 17, instead of setting value = 0, set it 1

http://188.166.206.43/YourAuthTokenHere/update/V13?value=0

So now we have successfully created two triggers to turn on and off one Relay. So Similarly, we create triggers for the remaining 3 relays. Just change the phrase and Digital pin for each Relay. All the other steps will remain the same. So, in the end for 4 relays, we should have 8 triggers to turn each of them on or off.

When this is all done, you will be able to say the voice commands to your Google Assistant and it should work like a charm! So now, you have your own voice-controlled.

Known Bugs

  • You have to connect to the internet first after electric failer to push button work. If there is no internet after electric failur push button will not work.

Please share your sollution if you solve this bug to work completly offline with push button.

--

--

Shahariar Rabby

Like to make the conceptual practical to computer program. o.O