Arduino Based Voice-Controlled IOT Relay Switch (Google Home & Alexa Supported)
by brinohm in Circuits > Arduino
1772 Views, 2 Favorites, 0 Comments
Arduino Based Voice-Controlled IOT Relay Switch (Google Home & Alexa Supported)
This project describes how to make an Arduino-based, voice-controlled, IOT relay switch. This is a relay that you can turn on and off remotely using an app for iOS and Android, as well as tie it in to IFTTT and control it with your voice using Google Home and/or Alexa as well. We will cover all steps needed to create the device, connect the app, and connect in the various IOT services as well.
Supplies
- Wemos D1 Mini Lite ESP8285 (a few dollars on Amazon)
- 5V Relay Module for Arduino/Raspberry Pi
- 2N2222 NPN transistor
- 47K ohm resistor
- 1K ohm resistor
Build the Circuit
The first step is to wire up the circuit. I've included a schematic and a traditional wiring diagram, depending on what you're used to. I did my first circuit on a prototyping breadboard and then moved it over to a PCB for a more permanent setup and housed it in a 3D printed project box.
The relay is an opto-isolated H/L 5V relay, which means that first, the trigger circuitry is optically isolated from the relay activation itself, which removes any potential issue with feedback from the relay back to the Wemos microcontroller. A nice safety to have. Second, it is high/low switchable, which means that it can be configured, by moving the yellow jumper you see in the pictures, from triggering when the signal is high (+5V), or triggering when the signal is low (0V). The Wemos itself sends out 3.3V from it's digital pins and we're using D1 as our trigger source, which means we need to amplify it slightly, so that we get close to a +5V digital signal to trigger the relay. Alternately you could use a 3.3V relay and eliminate the transistor amplifier component in the circuit and go straight from D1 to the signal pin on the relay. I didn't have 3.3V relays, so we're using a more common 5V relay and the amp circuit.
The DC+ and DC- on the relay connect to the 5V and GND pins of the Wemos, which provides the necessary voltage to operate the relay. The relay is rated from small voltages all the way to line voltage, but I'm using this to control a low-voltage wiring application. If you're using this to control line voltage, please make sure you know what you're doing, you're qualified, and you take the appropriate precautions. If not, then don't use this to control line voltage applications.
Pin D1 on the Wemos connects to the 47K ohm resistor which feeds into the base on the NPN transistor. The emitter ties back to ground. The collector is connected to the signal input on the relay. The relay is set to trigger on low, so when D1 is giving a signal, the 3.3v signal is amplified to approx 5V and the relay is open. When D1 goes low, the signal to the relay goes low and the relay closes and completes a circuit.
I forgot to take pictures of my temporary circuit on the prototyping breadboard, but it looked exactly like the Fritzing diagram above, if it's helpful. I included a number of pictures of my final permanent circuit so you can see how it's wired up, in case you need some insights or need visibility on the specific components I'm using.
Programming the Wemos - Overview
One of the things that I like about the Wemos is that they can be programmed just like an Arduino, using the same IDE. There are a few steps here though.
- Install the Wemos drivers to the computer can talk to the Wemos
- Update the Arduino IDE so the Wemos board is available
- Do a quick "Blink" test to make sure everything is working correctly
- Get set up with Blynk (I know it's confusing, but it's actually different than the "blink test")
- Get an application code from Blynk
- Update the Wemos/Arduino code with the Blynk info
- Upload the code to your Wemos setup.
Programming the Wemos - Install Drivers
Go here (this link seems to change periodically, I'll try to keep it updated):
https://www.wemos.cc/en/latest/ch340_driver.html
And download the correct driver package for your OS. Then extract the zip to a directory and execute the "SETUP" application to install it.
When I did this the first time, I got some weird error about it not installing. I saw someone else's note about this and their solution, which worked for me. So if you get an error, try using the "Uninstall" button and then use the "Install" again. Hopefully that clears the issue like it did for me.
Programming the Wemos - Update the Arduino IDE
If you've not installed the Arduino IDE, it's a good time to do that now. You can download it from www.arduino.cc
Here's a reference for what we're going to do in this step.
https://wiki.wemos.cc/tutorials:get_started:get_st...
- Let's install the new board so it shows up as an option in the Arduino IDE. Steps 2 - 4 are excerpted from the installation guide on the following github page.
-
Start the Arduino IDE and open the Preferences window.
-
Enter "https://arduino.esp8266.com/stable/package_esp8266com_index.json" into the Additional Board Manager URLs field. You can add multiple URLs, separating them with commas.
-
Open Boards Manager from Tools > Board > Boards Manager (at the top), search for "esp8266" and install the listed esp8266 platform.
-
At this point you will see Wemos boards in the Tools→Board:xxx Select the Wemos D1 Mini. In my case, as you can see from the image, the specific name was "LOLIN(WEMOS) D1 R2 & mini"
- Connect a USB cable between the computer and the Wemos
- Confirm that the "Port" is now active in the Tools menu and looks correct.
Programming the Wemos - Blink Test
Before we go further, we need to make sure we can communicate with the Wemos board and everything looks ok. The easiest way to do this is plug in a USB cable and try and push a simple program to the Wemos. Blink is the easiest example and has saved me a lot of hassle when working with new boards, that I always do this first.
- Go to: File > Examples > ESP8266 > Blink and load the blink sketch for a Wemos device
- Compile and upload the sketch
If the blue LED starts blinking about once a second, then you are good to go on to the next step! If not, then go back through and troubleshoot the previous steps. See if there's an issue with the drivers and the board manager. I had some issues with the board package for the ESP8266 and had to completely uninstall it and reinstall it because of something weird with versions that was happening. Don't give up, and internet searches can be your friend!
Assuming all is good, head to the next step.
Programming the Wemos - Setting Up Blynk
In these next steps, we need to set up an account with Blynk and get a token for us to use to control the circuit we've created.
- Download the app, and create an account (its free)
- Create a new project
- Select Wemos D1 as the Hardware under Choose Device
- Give the project a name and keep the connector type as WiFi
- Click Create Project
- At this point the Auth Token will be emailed to the email address you used when you created your Blynk account. Save that for later, we'll be inserting the values into the sketch in the next step.
- When you get to the blank canvas screen, just swipe left and you'll get the widget menu. Select "Button" to add a "button" to the project
- Select the button and then configure the Pin by clicking it and selecting "Digital" and "D1" as the pi and click "Ok"
- At this point, everything should be ready. To make this active you would want to select the triangle icon in the upper right, but it won't work just yet, until we get the sketch uploaded and configured, which is the next step!
Programming the Wemos - Installing the Blynk Library
To be able to upload a Blynk based sketch, we need to install their library. The details can be found here.
https://github.com/blynkkk/blynk-library/releases
Also, for some background, check out their main website here ( https://blynk.io/en/getting-started )
This was confusing for me at first, but it's way easier than it sounds. Just unzip the file to your Arduino directory. For me this was in \user\Documents\Arduino. There was already a folder called "libraries" there. The zip file contains a "library" and a "tools" directory. When you unzip it to the Arduino directory, it adds it's contents to libraries and creates tools if it didn't already exist.
Programming the Wemos - the Sketch
We're almost done at this point. The sketch is pretty simple, it's straight from Blynk and essentially connects the Blynk service and the board. You can use their builder here:
https://examples.blynk.cc/?board=WeMos%20D1&shield=ESP8266%20WiFi&example=Widgets%2FTerminal
Or you can use this sample which should work for you. Just make sure you substitute the values for the auth token and your credentials for your wifi network.
/************************************************************* Download latest Blynk library here: https://github.com/blynkkk/blynk-library/releases/latest Blynk is a platform with iOS and Android apps to control Arduino, Raspberry Pi and the likes over the Internet. You can easily build graphic interfaces for all your projects by simply dragging and dropping widgets. Downloads, docs, tutorials: http://www.blynk.cc Sketch generator: http://examples.blynk.cc Blynk community: http://community.blynk.cc Follow us: https://www.fb.com/blynkapp http://twitter.com/blynk_app Blynk library is licensed under MIT license This example code is in public domain. ************************************************************* You can send/receive any data using WidgetTerminal object. App project setup: Terminal widget attached to Virtual Pin V1 *************************************************************/ /* Comment this out to disable prints and save space */ #define BLYNK_PRINT Serial #include <ESP8266WiFi.h> #include <BlynkSimpleEsp8266.h> // You should get Auth Token in the Blynk App. // Go to the Project Settings (nut icon). char auth[] = "YourAuthToken"; // Your WiFi credentials. // Set password to "" for open networks. char ssid[] = "YourNetworkName"; char pass[] = "YourPassword"; // Attach virtual serial terminal to Virtual Pin V1 WidgetTerminal terminal(V1); // You can send commands from Terminal to your hardware. Just use // the same Virtual Pin as your Terminal Widget BLYNK_WRITE(V1) { // if you type "Marco" into Terminal Widget - it will respond: "Polo:" if (String("Marco") == param.asStr()) { terminal.println("You said: 'Marco'") ; terminal.println("I said: 'Polo'") ; } else { // Send it back terminal.print("You said:"); terminal.write(param.getBuffer(), param.getLength()); terminal.println(); } // Ensure everything is sent terminal.flush(); } void setup() { // Debug console Serial.begin(9600); Blynk.begin(auth, ssid, pass); // You can also specify server: //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80); //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080); // This will print Blynk Software version to the Terminal Widget when // your hardware gets connected to Blynk Server terminal.println(F("Blynk v" BLYNK_VERSION ": Device started")); terminal.println(F("-------------")); terminal.println(F("Type 'Marco' and get a reply, or type")); terminal.println(F("anything else and get it printed back.")); terminal.flush(); } void loop() { Blynk.run(); }
Programming the Wemos - Final Step
Once the sketch is loaded, compile and upload the sketch and you should be ready to test your setup!
Open the Blynk app, click the triangle in the upper right corner for your Wemos Project
Then click the button! You should hear the relay click and the status LEDs on the relay board indicate that the relay has changed state.
Now you can connect the relay to any low voltage application requiring a simple switch and control it from your Blynk app. The last thing we need to do is tie in IFTTT and use Google or Alexa to control the Blynk system so we don't have to do it from our Blynk app.
Controlling Blynk With IFTTT and Google Home or Alexa
I'm assuming at this point that you have some familiarity with IFTTT. If not, there are some excellent tutorials out there which will help you learn how to use and leverage IFTTT. It's very powerful and something that you'll want to learn if you're not familiar with already.
- Create a new applet in IFTTT
- Select Google Assistant as the "If This" and use a "Simple Phrase" as the trigger. For me, since this was going to turn on or off my fireplace, my phrase was "turn on the fireplace"
- For the "Then That" part, search for and use Webhooks
- Select "Make a web request"
- For the URL you'll want to use:
"http://blynk-cloud.com/XXXXYYYYZZZZZ/update/d5?value=1"
- Set the method to GET, the content type to urlencoded, and you can leave the BODY blank and then save
Wait a few minutes and then go ahead and test your phrase with your google home device. I noticed that it takes about two minutes for it to be ready.
NOTE: one other thing to pay attention to here. Notice that I'm using "d5" in my API call, but I connected to pin D1 on the board. It took me about a day to figure out that the GPIO pin numbering and the screen printed numbering on the board are not the same. After changing values with direct URL calls and testing the voltages at different pins with a volt meter, I was able to notice that a call to d1 via the API did change the voltage, but not at D1 on the board. d5/GPIO 5 actually corresponded to D1 on the board. Once I made that adjustment, everything worked beautifully!
Hooking up Alexa is identical to Google home, just using the Alexa service on IFTTT instead.
Conclusion
At this point, you should have a functioning voice-controlled IOT relay using the Wemos D1 mini lite. Enjoy and good luck!
Again, electricity is dangerous, so please take appropriate precautions and if you're not qualified, please don't.