<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.7.4">Jekyll</generator><link href="https://www.atwing.net/feed.xml" rel="self" type="application/atom+xml" /><link href="https://www.atwing.net/" rel="alternate" type="text/html" /><updated>2019-01-06T20:54:10+00:00</updated><id>https://www.atwing.net/feed.xml</id><title type="html">Anthony Nguyen</title><subtitle>Machine Learning, Home Automation, Information Security</subtitle><author><name>Anthony Nguyen</name></author><entry><title type="html">Control lights &amp;amp; home devices using voice commands</title><link href="https://www.atwing.net/home%20automation/light-switch-assistant/" rel="alternate" type="text/html" title="Control lights &amp; home devices using voice commands" /><published>2017-08-05T00:00:00+00:00</published><updated>2017-08-05T00:00:00+00:00</updated><id>https://www.atwing.net/home%20automation/light-switch-assistant</id><content type="html" xml:base="https://www.atwing.net/home%20automation/light-switch-assistant/">&lt;aside class=&quot;sidebar__right&quot;&gt;
&lt;nav class=&quot;toc&quot;&gt;
    &lt;header&gt;&lt;h4 class=&quot;nav__title&quot; style=&quot;text-align: left&quot;&gt;&lt;i class=&quot;fa fa-list&quot;&gt;&lt;/i&gt; Contents&lt;/h4&gt;&lt;/header&gt;
&lt;ul class=&quot;toc__menu&quot; id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#overview&quot; id=&quot;markdown-toc-overview&quot;&gt;Overview&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#step-1-wiring-relay-module-with-raspberry-pi&quot; id=&quot;markdown-toc-step-1-wiring-relay-module-with-raspberry-pi&quot;&gt;Step 1: Wiring relay module with Raspberry Pi&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#step-2-connect-with-target-device&quot; id=&quot;markdown-toc-step-2-connect-with-target-device&quot;&gt;Step 2: Connect with target device&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#step-3-operate-relay-with-python-script&quot; id=&quot;markdown-toc-step-3-operate-relay-with-python-script&quot;&gt;Step 3: Operate relay with Python script&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#appendix-ceiling-lights-circuit&quot; id=&quot;markdown-toc-appendix-ceiling-lights-circuit&quot;&gt;Appendix: Ceiling lights circuit&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#two-way-switch&quot; id=&quot;markdown-toc-two-way-switch&quot;&gt;Two-way switch&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#serial-lighting-circuit&quot; id=&quot;markdown-toc-serial-lighting-circuit&quot;&gt;Serial lighting circuit&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#parallel-lighting-circuit-with-relay-unit&quot; id=&quot;markdown-toc-parallel-lighting-circuit-with-relay-unit&quot;&gt;Parallel lighting circuit with relay unit&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

  &lt;/nav&gt;
&lt;/aside&gt;

&lt;p&gt;Most of us have already been there. You had a long and productive day and now it is time to have a good rest. You go on a light switch hunt, because it inconveniently went on a night stroll again and *BAM!*, your pinky toe decided to remind you that its only reason of existence is to let you &lt;strong&gt;FEEL&lt;/strong&gt; its existence. Next thing you know a war breaks out, every volcano on earth erupts at once and our planet flies straight to the center of the sun. All of this because a light switch didn’t do its job properly. We cannot waste another second - let us join forces in a rebellion against the source of all evil!&lt;/p&gt;

&lt;p&gt;If you have the exact same thought in mind as me or fight for an even greater cause, you are at the right place. Controlling home devices using relay modules is fairly quickly built up. We only need a few hardware components which don’t take too long to assemble and the code to control our system is a handful of Python lines.&lt;/p&gt;

&lt;p class=&quot;notice--danger&quot;&gt;&lt;strong&gt;Warning&lt;/strong&gt;: You will be working with mains electricity which can seriously injure you. Please use extreme caution and if you’re not absolutely certain, ask someone who is. Saving one or two questions isn’t worth endangering your life.&lt;/p&gt;

&lt;h3 id=&quot;overview&quot;&gt;Overview&lt;/h3&gt;
&lt;figure&gt;
  &lt;img src=&quot;/img/2017-08-05-light-switch-assistant/overview_setup.jpg&quot; /&gt;
  &lt;figcaption&gt;Figure 1: Setup overview&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;In the previous posts we have been implementing Google Assistant on a Raspberry Pi and use it to run &lt;a href=&quot;/home%20automation/shell-commands-assistant/&quot;&gt;shell commands using our voice&lt;/a&gt;. I will use that setup to switch different devices with voice commands but naturally you will be able to execute the Python script any way you want. This post only covers the hardware installation with the relay module up to the execution of the controlling script. If you want to know how to run the script with your voice, please refer to my &lt;a href=&quot;/home%20automation/shell-commands-assistant/&quot;&gt;previous guide&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For this project you need:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;a Raspberry Pi or any other single-board computer with GPIOs containing a 5V pin&lt;/li&gt;
  &lt;li&gt;a 5V relay module. The module I use it similar to &lt;a href=&quot;https://www.amazon.co.uk/SODIAL-R-Relay-Module/dp/B00L11KL10/ref=sr_1_5?ie=UTF8&amp;amp;qid=1501970083&amp;amp;sr=8-5&amp;amp;keywords=5v+relay+module&quot;&gt;this one&lt;/a&gt; and I have seen boards with 1/2/4/8/16 channels.&lt;/li&gt;
  &lt;li&gt;Jumper wires (&lt;a href=&quot;https://www.amazon.co.uk/Aukru-20cm-Female-Female-Jumpers-Raspberry/dp/B00OL6JZ3C/ref=sr_1_2?ie=UTF8&amp;amp;qid=1501970392&amp;amp;sr=8-2&amp;amp;keywords=female+to+female+arduino&quot;&gt;example&lt;/a&gt;). Depending on your single-board computer, you either need Female-Female or Male-Female wires.&lt;/li&gt;
  &lt;li&gt;Voltage tester&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;step-1-wiring-relay-module-with-raspberry-pi&quot;&gt;Step 1: Wiring relay module with Raspberry Pi&lt;/h3&gt;
&lt;p class=&quot;notice--info&quot;&gt;Note: In this post I will use the American terminology for the switches. Refer to &lt;a href=&quot;https://en.wikipedia.org/wiki/Switch#contact_terminology&quot;&gt;this page&lt;/a&gt; for their alternate names.&lt;/p&gt;
&lt;p&gt;The relay modules we use will typically contain a mechanical metal switch which is controlled by an electromagnetic coil. Upon feeding power through the coil, the switch is moved to its alternate position. For the ceiling lights, we can use this three-way switch (SPDT) to keep the manual switches intact when adding the voice control feature. But this will heavily depend on how your lights switches have been wired up. Some will have the lights and switched directly connected to the mains, while others have a separate circuit for the switches and thus safer system for the installer. Either way, you should disconnect the power source by &lt;strong&gt;shutting off the appropriate breaker or fuse in the fuse box&lt;/strong&gt;. More on this later.&lt;/p&gt;

&lt;p&gt;We will start off by connecting the relay module with the Raspberry Pi. Locate the set of pins that contains a GND, VCC and a IN# pin for every channel on your module. We connect the GND pin with a ground pin and the VCC with a 5V pin on the Raspberry Pi. Then connect a GPIO pin from the RasPi to the input pin of the channel you want to control. E.g. I used the GPIO2 pin to control the relay on channel 1, like so:&lt;/p&gt;

&lt;figure&gt;
  &lt;img src=&quot;/img/2017-08-05-light-switch-assistant/relay-raspi-circuit.png&quot; alt=&quot;image alt&quot; class=&quot;ImageBorder&quot; /&gt;
  &lt;figcaption&gt;Figure 2: Wiring between relay module &amp;amp; Raspberry Pi (&lt;a href=&quot;https://www.raspberrypi.org/documentation/usage/gpio-plus-and-raspi2/&quot;&gt;Source&lt;/a&gt;)&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;h3 id=&quot;step-2-connect-with-target-device&quot;&gt;Step 2: Connect with target device&lt;/h3&gt;
&lt;p&gt;Next, we hook up the device we intend to control to the relay. Wiring with ceiling lamps is a bit tricky, so we will use a simple power strip for now. If you want to know how to connect your ceiling lights, refer to the &lt;a href=&quot;http://localhost:4000/home%20automation/light-switch-assistant/#appendix-ceiling-lights-circuit&quot;&gt;appendix&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We will have to cut open the power strip wire, so ideally you want to use an old power strip which needs a “new haircut” anyway.&lt;br /&gt;
Cut the power strip cable into two parts. Depending on what type of cable you have, you will find three wires: Blue(Neutral), Brown(Live/Line/Hot), Green/Yellow(Earth/Ground). Remove approx. 1cm insulation from each of all six ends and twist the ends. Reconnect the Neutral and Earth wires with connector blocks and plug the Live wires into the relay block. It doesn’t really matter which sockets you use as long one wire goes into the middle socket:&lt;/p&gt;

&lt;figure&gt;
  &lt;img src=&quot;/img/2017-08-05-light-switch-assistant/Relay-power-strip.jpg&quot; alt=&quot;image alt&quot; /&gt;
  &lt;figcaption&gt;Figure 3: Wiring power strip with relay module&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Make sure no wires are exposed at the joints. Best put the relay module in a small box.&lt;br /&gt;
Once everything looks tidy and ready, plug the power strip into the wall socket and your target device into the power strip.&lt;/p&gt;

&lt;h3 id=&quot;step-3-operate-relay-with-python-script&quot;&gt;Step 3: Operate relay with Python script&lt;/h3&gt;
&lt;p&gt;Time to peck some keys. Logged into your trusty Raspberry Pi and install RPi.GPIO if you don’t have it yet:&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get update &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get install python-rpi.gpio
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;If you are running Python 3, use:&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get update &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get install python3-rpi.gpio
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Create a file in a location of your choosing and name it something like &lt;strong&gt;relay_on.py&lt;/strong&gt; (although we don’t really have an “on” and “off” state, it’s simply two different states whose function will depend on how and what you plugged into the relay blocks). This file should contain the following:&lt;/p&gt;
&lt;div class=&quot;language-py highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;RPi.GPIO&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# refer to pins by the &quot;Broadcom SOC channel&quot; number&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setmode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BCM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# set GPIO2 to output&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# set to False to turn on relay&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Run the script with &lt;code class=&quot;highlighter-rouge&quot;&gt;python relay_on.py&lt;/code&gt; to test your setup.&lt;br /&gt;
Change the number in &lt;code class=&quot;highlighter-rouge&quot;&gt;GPIO.setup()&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;GPIO.output()&lt;/code&gt; of both scripts if you selected a different pin to control the relay channel. If your device still wont turn on, try changing the boolean value in &lt;code class=&quot;highlighter-rouge&quot;&gt;GPIO.output()&lt;/code&gt; to &lt;code class=&quot;highlighter-rouge&quot;&gt;True&lt;/code&gt;. This will depend on which relay sockets you chose.&lt;/p&gt;

&lt;p class=&quot;notice--info&quot;&gt;Note: In Python you can choose between GPIO numbering (GPIO.BCM) and physical numbering (GPIO.BOARD). The first option uses the labels as the computer sees them while the latter counts the pins across as you see them. If you use this script on different Raspberry Pi models, you might want to the physical numbering system, which didn’t change over the different models.&lt;/p&gt;

&lt;p&gt;Create a second file named e.g. &lt;strong&gt;relay_off.py&lt;/strong&gt; which contains:&lt;/p&gt;
&lt;div class=&quot;language-py highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;RPi.GPIO&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# refer to pins by the &quot;Broadcom SOC channel&quot; number&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setmode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BCM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# set PIN 2 to output&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# set to False to turn off lamp&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# clean exit by resetting all ports used&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cleanup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Change the values accordingly based on which pin(s) and relay sockets you chose.&lt;/p&gt;

&lt;p&gt;If you can run [shell commands with your voice], you can now tell your digital assistant to run the Python scripts after specific voice commands. Smooth!&lt;/p&gt;

&lt;h3 id=&quot;appendix-ceiling-lights-circuit&quot;&gt;Appendix: Ceiling lights circuit&lt;/h3&gt;
&lt;p&gt;First we need to determine what kind of circuit you have within your walls, except if you wired everything yourself - but then again you probably wouldn’t read this part if you did.&lt;br /&gt;
There are a lot of ways to setup a lighting system. I will list the most common systems I encountered and write how to add our custom switch to it. When dismantling the light switch from the wall, you should be able to determine which system is installed in your room.&lt;/p&gt;

&lt;p class=&quot;notice--danger&quot;&gt;&lt;strong&gt;Warning&lt;/strong&gt;: Remember to turn off the appropriate breaker or fuse in the fuse box before tinkering with the light switches.&lt;/p&gt;

&lt;h4 id=&quot;two-way-switch&quot;&gt;Two-way switch&lt;/h4&gt;
&lt;figure&gt;
  &lt;img src=&quot;/img/2017-08-05-light-switch-assistant/Twoway-lighting-circuit.png&quot; alt=&quot;image alt&quot; style=&quot;width: 75%&quot; class=&quot;ImageBorder&quot; /&gt;
  &lt;figcaption&gt;Figure 4: Circuit with two-way switch&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;This is the simplest layout and unfortunately you wont be able to just add a custom switch while leaving the existing one operational (at least I wasn’t smart enough to come up with a solution). You either would have to replace the existing switch, which means if the voice switch fails, you wont be able to control the lights. Otherwise you could replace the two-way switch (SPST, single pole single throw) with a three-way switch (SPDT, single pole double throw) and run two wires between it and the relay block. The resulting circuit would look similar to the Figure 5 below, without &lt;strong&gt;SW2&lt;/strong&gt; &amp;amp; &lt;strong&gt;SW3&lt;/strong&gt; and e.g. &lt;strong&gt;SW4&lt;/strong&gt; replaced with the relay switch. The wire from the lamp would go into the relay’s middle socket while the two wires from the three-way switch in &lt;strong&gt;SW1&lt;/strong&gt; would go into the left &amp;amp; right socket.&lt;/p&gt;

&lt;h4 id=&quot;serial-lighting-circuit&quot;&gt;Serial lighting circuit&lt;/h4&gt;
&lt;figure&gt;
  &lt;img src=&quot;/img/2017-08-05-light-switch-assistant/Serial-lighting-circuit.png&quot; alt=&quot;image alt&quot; class=&quot;ImageBorder&quot; /&gt;
  &lt;figcaption&gt;Figure 5: Serial circuit&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;If you already have a circuit with multi-way switches in your room, we can reconstruct a four-way switch (DPDT, double pole double throw) with two relay blocks to get a component similar to &lt;strong&gt;SW2&lt;/strong&gt; and &lt;strong&gt;SW3&lt;/strong&gt;. Plug each of the two wires coming from &lt;strong&gt;SW1&lt;/strong&gt; into the middle slots of separate relay blocks. The two wires from the next switch (e.g. &lt;strong&gt;SW2&lt;/strong&gt;) go into the left and right slot of one relay channel. From the same slots you run two additional wires into the second relay channel but interchanged, i.e. left from the first into the right of the second and right of the first into the left of the second. As always, a picture is worth a thousand words:&lt;/p&gt;

&lt;figure&gt;
  &lt;img src=&quot;/img/2017-08-05-light-switch-assistant/Relay-block-serial.png&quot; alt=&quot;image alt&quot; class=&quot;ImageBorder&quot; /&gt;
  &lt;figcaption&gt;Figure 6: Relay blocks in a serial lighting circuit&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;In the Python script, set both relays to the same boolean value at the same time to simulate a four-way switch.&lt;/p&gt;

&lt;h4 id=&quot;parallel-lighting-circuit-with-relay-unit&quot;&gt;Parallel lighting circuit with relay unit&lt;/h4&gt;
&lt;figure&gt;
  &lt;img src=&quot;/img/2017-08-05-light-switch-assistant/Parallel-lighting-circuit.png&quot; alt=&quot;image alt&quot; class=&quot;ImageBorder&quot; /&gt;
  &lt;figcaption&gt;Figure 7: Parallel circuit&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p class=&quot;notice--warning&quot;&gt;Note: The voltage values in the upper circuit are just an example. It could even have a higher value if you are working on an unusual system. Check if it is VAC first. If your multimeter says 0V, then it is probably VDC. The other way round is also applicable.&lt;/p&gt;

&lt;p&gt;This one is fairly simple to set up. Every time the upper circuit is closed, a relay unit switches its state and opens or closes the lower circuit.&lt;br /&gt;
We install one relay block in parallel to one of the switches. One wire from each connector on the switch go the middle and an arbitrary side socket of the relay block.&lt;br /&gt;
To toggle the lights, you close the circuit once and open it after a short delay. Example Python script:&lt;/p&gt;
&lt;div class=&quot;language-py highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;RPi.GPIO&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;time&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# refer to pins by the &quot;Broadcom SOC channel&quot; number&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setmode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BCM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# set GPIO2 to output&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;OUT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# close circuit&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# small delay, otherwise relay wont register value change&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# reopen circuit&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# clean exit&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;GPIO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cleanup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;&lt;br /&gt;
This concludes the relay module implementation with single board computers. Have fun tinkering and always stay safe! I would love to hear about what you put together. See you next time!&lt;/p&gt;</content><author><name>Anthony Nguyen</name></author><category term="Home Automation" /><summary type="html">Don't you hate it when you enter a dark room and hit your toe on furniture while searching for the light switch? Control lights and power plugs with your voice using relay modules, a Raspberry Pi and Google Assistant today!</summary></entry><entry><title type="html">Run shell commands and boot your computer with your voice</title><link href="https://www.atwing.net/home%20automation/shell-commands-assistant/" rel="alternate" type="text/html" title="Run shell commands and boot your computer with your voice" /><published>2017-08-02T00:00:00+00:00</published><updated>2017-08-02T00:00:00+00:00</updated><id>https://www.atwing.net/home%20automation/shell-commands-assistant</id><content type="html" xml:base="https://www.atwing.net/home%20automation/shell-commands-assistant/">&lt;aside class=&quot;sidebar__right&quot;&gt;
&lt;nav class=&quot;toc&quot;&gt;
    &lt;header&gt;&lt;h4 class=&quot;nav__title&quot; style=&quot;text-align: left&quot;&gt;&lt;i class=&quot;fa fa-list&quot;&gt;&lt;/i&gt; Contents&lt;/h4&gt;&lt;/header&gt;
&lt;ul class=&quot;toc__menu&quot; id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#overview&quot; id=&quot;markdown-toc-overview&quot;&gt;Overview&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#step-0-prerequisites&quot; id=&quot;markdown-toc-step-0-prerequisites&quot;&gt;Step 0: Prerequisites&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#step-1-install-home-assistant&quot; id=&quot;markdown-toc-step-1-install-home-assistant&quot;&gt;Step 1: Install Home Assistant&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#step-2-home-assistant-setup-and-shell-command-services&quot; id=&quot;markdown-toc-step-2-home-assistant-setup-and-shell-command-services&quot;&gt;Step 2: Home Assistant setup and shell command services&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#step-3-link-the-good-the-bad-and-the-ugly&quot; id=&quot;markdown-toc-step-3-link-the-good-the-bad-and-the-ugly&quot;&gt;Step 3: Link the Good, the Bad and the Ugly&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

  &lt;/nav&gt;
&lt;/aside&gt;

&lt;p&gt;Combining Google Assistant, IFTTT and Home Assistant provides us access to a powerful tool: Voice-activated shell commands. No more getting up from the couch and physically control devices. Calories have rights too and we should stop burning them. Let’s go!&lt;/p&gt;

&lt;p&gt;IFTTT stands for “If This Then That” and is a web-based service which enables a large variety of applications to communicate with each other. Conveniently for us, it supports both Google Assistant and Home Assistant - an open-source platform which you can as a server on your computer (at the time of writing it supports Linux, Windows, OS X, FreeBSD) and is able to monitor and control your home automation devices. But the main feature of interest for us in this post is the ability to run &lt;a href=&quot;https://home-assistant.io/components/shell_command/&quot;&gt;shell commands&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The implementation of the Home Assistant wasn’t quite intuitive, so I will provide a clear step-by-step guide to the best of my writing skills. If you are running a Debian-based OS like I do (Raspbian), this guide should be a perfect fit for you. I haven’t tried this setup on other OSs but it should work on everything that can run Python 3. Alright, enough with the introduction, our homes are eagerly waiting to be made alive!&lt;/p&gt;

&lt;h3 id=&quot;overview&quot;&gt;Overview&lt;/h3&gt;
&lt;p&gt;Needed for this project:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Computer which runs the Google Assistant and can run Home Assistant, e.g. Raspberry Pi 3 running Raspbian Jessie. Additionally, internet access and peripherals to record and play audio&lt;/li&gt;
  &lt;li&gt;(Optional) A computer that supports &lt;a href=&quot;https://en.wikipedia.org/wiki/Wake-on-LAN&quot;&gt;Wake-on-LAN&lt;/a&gt; for booting from the network&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The second computer is for me to demonstrate the functionality of the shell command feature. But also since its base unit is cramped somewhere under my desk guarded by a cable clutter and a potential army of spiders, I might as well have a small upgrade in quality of living. Naturally you can use any other shell command such as &lt;code class=&quot;highlighter-rouge&quot;&gt;touch ~/test.txt&lt;/code&gt; to test the functionality.&lt;/p&gt;

&lt;h3 id=&quot;step-0-prerequisites&quot;&gt;Step 0: Prerequisites&lt;/h3&gt;
&lt;p&gt;You first need a device running Google Assistant. Google provides a great guide on how to install it. You can take a look at &lt;a href=&quot;/home%20automation/assistant-on-pi/&quot;&gt;this post&lt;/a&gt; for more details - I guarantee it wont bite!&lt;/p&gt;

&lt;h3 id=&quot;step-1-install-home-assistant&quot;&gt;Step 1: Install Home Assistant&lt;/h3&gt;
&lt;p&gt;Similar to the Google Assistant installation, I will install the Home Assistant in a Python virtual environment again so it wont mess with any existing Python installments. If you prefer a different installation method, check out &lt;a href=&quot;https://home-assistant.io/docs/installation/&quot;&gt;this page&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;[IMAGE: screenshot of Home Assistant installation in Terminal]&lt;/p&gt;

&lt;p&gt;As usual, we start by getting all packages up-to-date from the command line:&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get update &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get upgrade
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Install &amp;amp; update &lt;strong&gt;Python 3&lt;/strong&gt;, &lt;strong&gt;pip&lt;/strong&gt; and the &lt;strong&gt;virtualenv&lt;/strong&gt; tool afterwards:&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;apt-get install python-pip python3-dev
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;pip install &lt;span class=&quot;nt&quot;&gt;--upgrade&lt;/span&gt; virtualenv
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The Home Assistant server will be constantly connected to the internet, so it is a good idea to create a separate user for it to limit its permissions. This part could introduce complications to the implementation, but security is a value worth fighting for!&lt;br /&gt;
Add a system user and a group to your system:&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;adduser &lt;span class=&quot;nt&quot;&gt;--system&lt;/span&gt; homeassistant
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;addgroup homeassistant
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We will need a directory to store for the virtual environment and the server files and you can choose whichever location you prefer. As an example I will use the directory suggested in the documentation. Afterwards we need to grant our new user full access to the directory:&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;mkdir /srv/homeassistant
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;chown homeassistant:homeassistant /srv/homeassistant
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Once this is done,  we will switch to the homeassistant user and create a virtual environment in which the Home Assistant is installed:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;su &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; /bin/bash homeassistant &lt;span class=&quot;c&quot;&gt;# -s option specifies a shell in which we will operate. By default, system users don't have a shell.&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;virtualenv &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; python3 /srv/homeassistant &lt;span class=&quot;c&quot;&gt;# create virtual environment&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; /srv/homeassistant/bin/activate &lt;span class=&quot;c&quot;&gt;# change into virt. env.&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;homeassistant&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;pip3 install &lt;span class=&quot;nt&quot;&gt;--upgrade&lt;/span&gt; homeassistant
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now you can start Home Assistant while in the virtual environment with&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;hass
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;If you are outside of the environment, run these lines which can be saved in a bash script:&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo &lt;/span&gt;su &lt;span class=&quot;nt&quot;&gt;-s&lt;/span&gt; /bin/bash homeassistant &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;EOF&lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;source /srv/homeassistant/bin/activate
&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;sh&quot;&gt;hass
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Or alternatively simply run:&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-u&lt;/span&gt; homeassistant &lt;span class=&quot;nt&quot;&gt;-H&lt;/span&gt; /srv/homeassistant/bin/hass
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To see if the Home Assistant server is running, launch &lt;code class=&quot;highlighter-rouge&quot;&gt;http://&amp;lt;Rasperry-Pi-IP&amp;gt;:8123&lt;/code&gt; on another computer in your web browser or &lt;code class=&quot;highlighter-rouge&quot;&gt;http://localhost:8123&lt;/code&gt; if your Raspberry Pi has a GUI and a web browser.&lt;/p&gt;

&lt;figure&gt;
  &lt;img src=&quot;/img/2017-08-02-shell-commands-assistant/HA_web_interface.png&quot; alt=&quot;image alt&quot; class=&quot;ImageBorder&quot; /&gt;
  &lt;figcaption&gt;Figure 1: Home Assistant, up and ready &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;You can close the server for now with the shortcut &lt;code class=&quot;highlighter-rouge&quot;&gt;Ctrl + C&lt;/code&gt;.After we adjust the Home Assistant configurations, we should specify what shell command to run and then copy-paste a few lines to setup IFTTT with the rest, which should be smooth sailing. Arr!&lt;/p&gt;

&lt;h3 id=&quot;step-2-home-assistant-setup-and-shell-command-services&quot;&gt;Step 2: Home Assistant setup and shell command services&lt;/h3&gt;
&lt;p&gt;The link to the IFTTT service demands that our Home Assistant server is exposed to the outside world. As such we need to tend to the server security for a bit.&lt;/p&gt;

&lt;p&gt;Open the &lt;strong&gt;configuration.yaml&lt;/strong&gt; file found in &lt;code class=&quot;highlighter-rouge&quot;&gt;~/.homeassistant&lt;/code&gt; of in homeassistant’s home directory. By default the full path is &lt;code class=&quot;highlighter-rouge&quot;&gt;/home/homeassistant/.homeassistant/&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; /home/homeassistant/.homeassistant/ &lt;span class=&quot;c&quot;&gt;# run as user homeassistant&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;nano configuration.yaml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Add a password for the Home Assistant web interface in the following entry:&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;http:
  api_password: &amp;lt;your-ingenious-password&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p class=&quot;notice--info&quot;&gt;Note: Make sure to include keep the indent of two spaces which are crucial for the YAML language and add them if you want to add new configuration variables.&lt;/p&gt;

&lt;p class=&quot;notice--warning&quot;&gt;Note: Unfortunately this wont be enough to secure our server. The connection to it is unencrypted and we have to make web requests which target an URL that contains our password in plaintext.&lt;br /&gt;
Additionally, since our Internet Service Provider will change our external IP in random intervals, we will have a difficult time keeping our setup running without adjusting the configurations every now and then. But luckily we can set up encrypted communication by using HTTPS and dynamic DNS service. The latter will assign a static URL to your external IP address and whenever it changes, your device will tell the DDNS service to assign the URL to your new IP address.&lt;br /&gt;
For the sake of briefness I will leave this part out for this guide and will at a later point write on how I set up these components. We will focus on getting the system up and running for now. Meanwhile here is a great guide on how to use DuckDNS and Let’s Encrypt to solve the dynamic IP issue and implement encryption: &lt;a href=&quot;https://home-assistant.io/docs/ecosystem/certificates/lets_encrypt/&quot;&gt;I’m a fancy guide&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Add this line at the end for the shell command service:&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;shell_command: &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;include shell_commands.yaml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This will read the configuration for shell commands from a file called &lt;code class=&quot;highlighter-rouge&quot;&gt;shell_commands.yaml&lt;/code&gt;. It is good practice to split up the configuration file into multiple files for readability and manageability.&lt;/p&gt;

&lt;p&gt;In the same directory as &lt;code class=&quot;highlighter-rouge&quot;&gt;configuration.yaml&lt;/code&gt; we create and edit a new file:&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;nano shell_commands.yaml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Then add a shell command of your choice using the format &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;command-name&amp;gt;: &amp;lt;shell-command&amp;gt;&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;test_cmd: touch /home/homeassistant/.homeassistant/test.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Save and reboot your Home Assistant server.&lt;br /&gt;
Log into the web interface with the password you wrote into &lt;code class=&quot;highlighter-rouge&quot;&gt;configuration.yaml&lt;/code&gt;. In the sidebar on the left under &lt;strong&gt;Developer Tools&lt;/strong&gt;, click the left-most button which reads “Services”.&lt;/p&gt;

&lt;figure&gt;
  &lt;img src=&quot;/img/2017-08-02-shell-commands-assistant/HA_services.png&quot; alt=&quot;image alt&quot; class=&quot;ImageBorder&quot; /&gt;
  &lt;figcaption&gt;Figure 2: Services menu of Home Assistant web interface&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;After choosing &lt;code class=&quot;highlighter-rouge&quot;&gt;shell_command&lt;/code&gt; in &lt;strong&gt;Domain&lt;/strong&gt; you should be able to see and select your custom shell command, e.g. &lt;code class=&quot;highlighter-rouge&quot;&gt;test_cmd&lt;/code&gt;. Test the shell command by clicking &lt;code class=&quot;highlighter-rouge&quot;&gt;CALL SERVICE&lt;/code&gt; down below and check if the shell command was executed.&lt;/p&gt;

&lt;figure&gt;
  &lt;img src=&quot;/img/2017-08-02-shell-commands-assistant/HA_test_shell_command.png&quot; alt=&quot;image alt&quot; /&gt;
  &lt;figcaption&gt;Figure 3: File &lt;b&gt;test.txt&lt;/b&gt; sucessfully created by shell command &lt;i&gt;test_cmd&lt;/i&gt;&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p class=&quot;notice--info&quot;&gt;Note: If Home Assistant did not properly execute the shell command, check if homeassistant has the ownership of all necessary files.&lt;br /&gt;
[IMAGE: screenshot of ls -lh in /home/homeassistant/.homeassistant/]&lt;/p&gt;

&lt;p class=&quot;notice--info&quot;&gt;Too many notes: If you want to boot your computer using a shell command, there are a lot of online guides on how to set up Wake-on-LAN. I am using the &lt;a href=&quot;https://packages.debian.org/search?keywords=wakeonlan&quot;&gt;&lt;strong&gt;wakeonlan&lt;/strong&gt;&lt;/a&gt; package for example. I could write a quick guide on this too on demand, although in the end the exact details depend on your OS, network adapter and motherboard.&lt;/p&gt;

&lt;p&gt;Almost there! Now that we got the shell command service running, we only need to link IFTTT with Home Assistant &amp;amp; Google Assistant and finally create an IFTTT applet to run the whole setup.&lt;/p&gt;

&lt;h3 id=&quot;step-3-link-the-good-the-bad-and-the-ugly&quot;&gt;Step 3: Link the Good, the Bad and the Ugly&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;Head over to the &lt;a href=&quot;https://ifttt.com/&quot;&gt;IFTTT web page&lt;/a&gt; and create an account.&lt;/li&gt;
  &lt;li&gt;Click on the &lt;strong&gt;My Applets&lt;/strong&gt; tab and create a &lt;strong&gt;New Applet&lt;/strong&gt;.
    &lt;figure&gt;
   &lt;img src=&quot;/img/2017-08-02-shell-commands-assistant/IFTTT_new_applet.png&quot; alt=&quot;image alt&quot; class=&quot;ImageBorder&quot; /&gt;
   &lt;figcaption&gt;Figure 4: Create a new applet in IFTTT&lt;/figcaption&gt;
 &lt;/figure&gt;
  &lt;/li&gt;
  &lt;li&gt;Click on &lt;strong&gt;this&lt;/strong&gt;
    &lt;figure&gt;
   &lt;img src=&quot;/img/2017-08-02-shell-commands-assistant/IFTTT_this_button.png&quot; alt=&quot;image alt&quot; class=&quot;ImageBorder&quot; /&gt;
   &lt;figcaption&gt;Figure 5: It wasn't obvious enough..&lt;/figcaption&gt;
 &lt;/figure&gt;
  &lt;/li&gt;
  &lt;li&gt;Search and connect with “Google Assistant” (grant permission to the Google account you use with Google Assistant)&lt;/li&gt;
  &lt;li&gt;Choose the trigger “Say a simple phrase”&lt;/li&gt;
  &lt;li&gt;Write a custom voice command phrase and a response phrase for the Assistant
    &lt;figure style=&quot;margin-bottom: 0;&quot;&gt;
   &lt;img src=&quot;/img/2017-08-02-shell-commands-assistant/IFTTT_assistant_trigger.png&quot; alt=&quot;image alt&quot; style=&quot;width: 50%;&quot; class=&quot;ImageBorder&quot; /&gt;
 &lt;/figure&gt;
    &lt;figure style=&quot;margin-top: 0;&quot;&gt;
   &lt;figcaption&gt;Figure 6: Words can hurt&lt;/figcaption&gt;
 &lt;/figure&gt;
  &lt;/li&gt;
  &lt;li&gt;Click on &lt;strong&gt;that&lt;/strong&gt;. Search, select, connect to “Webhooks” and click on “Make a web request”.&lt;/li&gt;
  &lt;li&gt;In &lt;strong&gt;URL&lt;/strong&gt;, type:&lt;br /&gt;
 &lt;code class=&quot;highlighter-rouge&quot;&gt;http://&amp;lt;Raspberry-Pi-IP&amp;gt;:8123/api/services/shell_command/&amp;lt;command_name&amp;gt;?api_password=&amp;lt;your-password&amp;gt;&lt;/code&gt;,&lt;br /&gt;
 e.g. &lt;code class=&quot;highlighter-rouge&quot;&gt;http://192.168.1.101:8123/api/services/shell_command/test_cmd?api_password=PASSWORD&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Choose &lt;code class=&quot;highlighter-rouge&quot;&gt;POST&lt;/code&gt; for &lt;strong&gt;Method&lt;/strong&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;application/json&lt;/code&gt; for &lt;strong&gt;Content type&lt;/strong&gt; and click on “Create action”. Click on “Finish” to complete the Applet.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the final part we need to enter the API key from IFTTT into the &lt;code class=&quot;highlighter-rouge&quot;&gt;configuration.yaml&lt;/code&gt; file of our Home Assistant server.&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Still on the IFTTT webpage, navigate to “My Applets” &amp;gt; “Services” &amp;gt; “Webhooks” &amp;gt; “Settings”.&lt;/li&gt;
  &lt;li&gt;Copy the letter-number-jumble at the end of the URL
    &lt;figure&gt;
   &lt;img src=&quot;/img/2017-08-02-shell-commands-assistant/IFTTT_api_key.png&quot; alt=&quot;image alt&quot; class=&quot;ImageBorder&quot; /&gt;
   &lt;figcaption&gt;Figure 7: IFTTT API key&lt;/figcaption&gt;
 &lt;/figure&gt;
  &lt;/li&gt;
  &lt;li&gt;Head over to the &lt;code class=&quot;highlighter-rouge&quot;&gt;configuration.yaml&lt;/code&gt; file of Home Assistant and append a new entry:
    &lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; ifttt:
   key: &amp;lt;your-API-KEY&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;At long last, boot up your Google Assistant and say the magic words to run your shell command.&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;&lt;br /&gt;
This has been quite a ride! From here on out the possibilities are endless. One of the many ways to impress your friends (and enemies?) is controlling your home devices’ electricity with your voice. &lt;a href=&quot;/home%20automation/light-switch-assistant/&quot;&gt;Join me&lt;/a&gt; when we use relay modules and a couple wires to toggle ceiling lights, wall outlets and more!&lt;/p&gt;</content><author><name>Anthony Nguyen</name></author><category term="Home Automation" /><summary type="html">Combining Google Assistant, IFTTT and Home Assistant provides us access to a powerful tool&amp;#58; Voice-activated shell commands. No more getting up from the couch and physically control devices. Calories have rights too and we should stop burning them. Let's go!</summary></entry><entry><title type="html">Custom audio response for Google Assistant</title><link href="https://www.atwing.net/home%20automation/custom-response-assistant/" rel="alternate" type="text/html" title="Custom audio response for Google Assistant" /><published>2017-07-30T00:00:00+00:00</published><updated>2017-07-30T00:00:00+00:00</updated><id>https://www.atwing.net/home%20automation/custom-response-assistant</id><content type="html" xml:base="https://www.atwing.net/home%20automation/custom-response-assistant/">&lt;aside class=&quot;sidebar__right&quot;&gt;
&lt;nav class=&quot;toc&quot;&gt;
    &lt;header&gt;&lt;h4 class=&quot;nav__title&quot; style=&quot;text-align: left&quot;&gt;&lt;i class=&quot;fa fa-list&quot;&gt;&lt;/i&gt; Contents&lt;/h4&gt;&lt;/header&gt;
&lt;ul class=&quot;toc__menu&quot; id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#requirements&quot; id=&quot;markdown-toc-requirements&quot;&gt;Requirements&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#step-1-extract-frames-from-audio-file&quot; id=&quot;markdown-toc-step-1-extract-frames-from-audio-file&quot;&gt;Step 1: Extract frames from audio file&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#step-2-play-audio-file&quot; id=&quot;markdown-toc-step-2-play-audio-file&quot;&gt;Step 2: Play audio file&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

  &lt;/nav&gt;
&lt;/aside&gt;

&lt;p&gt;Our custom &lt;a href=&quot;/home%20automation/BT-control-assistant/&quot;&gt;Google Assistant trigger&lt;/a&gt; opened up many possibilities for starting a conversation with the Assistant. But without any display on the headless Raspberry Pi, it is hard to know whether the trigger has been registered and when we can start making our voice request.&lt;br /&gt;
In this post we will provide the Google Assistant with a custom audio response. This can be anything from a beep tone, a spoken “Listening”, “Yes Sir” to “Sorry buddy, not today!”.&lt;/p&gt;

&lt;p&gt;The problem with playing an arbitrary audio file while using Google’s gRPC API as in the &lt;a href=&quot;/home%20automation/BT-control-assistant/&quot;&gt;previous post&lt;/a&gt;, is that it blocks our audio output device so that other applications aren’t able to access it while the streaming runs. One more or less inelegant solution would be to tap into this audio stream and push the frames of our custom audio response. Afterwards we release it for the Assistant to use as usual.&lt;/p&gt;

&lt;h3 id=&quot;requirements&quot;&gt;Requirements&lt;/h3&gt;
&lt;p&gt;You can find the necessary files in this Git repository: &lt;a href=&quot;https://github.com/atwing/HomeAI-tutorial&quot;&gt;link&lt;/a&gt;. It also contains scripts from the Bluetooth remote implementation which we will use as an example script to implement our custom response.&lt;/p&gt;

&lt;h3 id=&quot;step-1-extract-frames-from-audio-file&quot;&gt;Step 1: Extract frames from audio file&lt;/h3&gt;
&lt;p&gt;I will use the default beep sound from the Google Assistant on Google Home or Android devices, uploaded in the Git repo as &lt;strong&gt;beep-response.wav&lt;/strong&gt;. We can handle WAVE files in Python using the &lt;strong&gt;wave&lt;/strong&gt; module. Add &lt;code class=&quot;highlighter-rouge&quot;&gt;import wave&lt;/code&gt; to the script to use it.&lt;/p&gt;

&lt;p class=&quot;notice--info&quot;&gt;Note: When selecting a custom audio file, keep in mind that it has to have a sample rate of 16kHz. The stream used by the Assistant is configured sample at this rate.&lt;/p&gt;

&lt;p&gt;The file is first converted into a wave_read object from which we can extract the frames for later use:&lt;/p&gt;
&lt;div class=&quot;language-py highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# load audio response file as a wave_read object&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;wavep&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wave&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'beep-response.wav'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'rb'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# number of frames in audio response file&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;num_frames&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wavep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getnframes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# get frames from wav file&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;resp_samples&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wavep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readframes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num_frames&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The sequence of frames contained in &lt;strong&gt;resp_samples&lt;/strong&gt; can be written directly onto the &lt;strong&gt;conversation_stream&lt;/strong&gt; object in the sample script.&lt;/p&gt;

&lt;h3 id=&quot;step-2-play-audio-file&quot;&gt;Step 2: Play audio file&lt;/h3&gt;
&lt;p&gt;Before forwarding the frames we need to activate the audio stream which is a private member of the &lt;strong&gt;conversation_stream&lt;/strong&gt; object, meaning we can’t activate it directly. And here comes the inelegant part: the audio stream can be activated using the &lt;strong&gt;start_recording()&lt;/strong&gt; function. We will hold the input channel with &lt;strong&gt;stop_recording()&lt;/strong&gt;, start the output channel with &lt;strong&gt;start_playback()&lt;/strong&gt; and are then able to write our frames onto the stream. Finally the stream is inactivated and released for the Assistant to use.&lt;/p&gt;

&lt;div class=&quot;language-py highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conversation_stream&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# [...]&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# announce listening phase with audio response&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start_recording&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# activate audio stream&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start_playback&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stop_recording&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;resp_frames&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# write response frames to output stream&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;stop_playback&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# inactivate audio stream&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Add the lines before &lt;strong&gt;assistant.converse()&lt;/strong&gt; is executed to get a response whenever the Assistant is about to recording your query.&lt;br /&gt;
You can find my implementation in the file &lt;strong&gt;pushtotalk_resp.py&lt;/strong&gt;.&lt;br /&gt;
Whenever you trigger a request you should now hear your custom response first, before the Assistant starts recording with the announcement “Recording audio request”.&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;&lt;br /&gt;
If you have a better way of playing a custom response, I would love to know! Happy hacking and hopefully I will see you next time when we integrate IFTTT and Home Assistant to the system to run &lt;a href=&quot;/home%20automation/shell-commands-assistant/&quot;&gt;shell commands via Google Assistant&lt;/a&gt;.&lt;/p&gt;</content><author><name>Anthony Nguyen</name></author><category term="Home Automation" /><summary type="html">Triggering Google Assistant on your own device without hotword detection does not give a trigger response. This entry intends to fix this by adding a custom audio response every time a conversation is started.</summary></entry><entry><title type="html">Start Google Assistant via Bluetooth Remote</title><link href="https://www.atwing.net/home%20automation/BT-control-assistant/" rel="alternate" type="text/html" title="Start Google Assistant via Bluetooth Remote" /><published>2017-07-28T00:00:00+00:00</published><updated>2017-07-28T00:00:00+00:00</updated><id>https://www.atwing.net/home%20automation/BT-control-assistant</id><content type="html" xml:base="https://www.atwing.net/home%20automation/BT-control-assistant/">&lt;aside class=&quot;sidebar__right&quot;&gt;
&lt;nav class=&quot;toc&quot;&gt;
    &lt;header&gt;&lt;h4 class=&quot;nav__title&quot; style=&quot;text-align: left&quot;&gt;&lt;i class=&quot;fa fa-list&quot;&gt;&lt;/i&gt; Contents&lt;/h4&gt;&lt;/header&gt;
&lt;ul class=&quot;toc__menu&quot; id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#overview&quot; id=&quot;markdown-toc-overview&quot;&gt;Overview&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#step-1-connect-bluetooth-remote&quot; id=&quot;markdown-toc-step-1-connect-bluetooth-remote&quot;&gt;Step 1: Connect Bluetooth remote&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#step-2-test-bluetooth-remote-input&quot; id=&quot;markdown-toc-step-2-test-bluetooth-remote-input&quot;&gt;Step 2: Test Bluetooth remote input&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#step-3-triggering-a-google-assistant-query-with-the-remote&quot; id=&quot;markdown-toc-step-3-triggering-a-google-assistant-query-with-the-remote&quot;&gt;Step 3: Triggering a Google Assistant query with the remote&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

  &lt;/nav&gt;
&lt;/aside&gt;

&lt;p&gt;After successfully running the Google Assistant on the &lt;a href=&quot;/home%20automation/assistant-on-pi/&quot;&gt;Raspberry Pi&lt;/a&gt;, we proceed with initiating a voice command with a Bluetooth remote. The motive behind this is that even though starting a query with a hotword is a great feature, we might not want the Assistant to always listen - especially if you tend to paranoia like I do. We could instead physically start a conversation and preferably from any part of the house.&lt;br /&gt;
One quite cheap solution that I found is using Bluetooth remote buttons that cost less than &lt;a href=&quot;https://www.amazon.com/UFCIT-Bluetooth-Wireless-Control-Smartphones/dp/B00LUNAA2O/&quot;&gt;two bucks online&lt;/a&gt; and with Bluetooth LE the electricity bill will be happy too. Additionally it is easy to carry around, e.g. with your key ring. Four birds with one stone? So many birds!&lt;/p&gt;

&lt;h3 id=&quot;overview&quot;&gt;Overview&lt;/h3&gt;
&lt;figure&gt;
  &lt;img src=&quot;/img/2017-07-28-BT-control-assistant/overview_setup.jpg&quot; /&gt;
  &lt;figcaption&gt;Figure 1: Setup overview&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Pretty much the same setup as the previous part.&lt;/p&gt;

&lt;p&gt;Hardware:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Bluetooth remote controller (&lt;a href=&quot;https://www.amazon.com/UFCIT-Bluetooth-Wireless-Control-Smartphones/dp/B00LUNAA2O/&quot;&gt;example&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;Raspberry Pi 3 Model B running Raspbian Jessie&lt;/li&gt;
  &lt;li&gt;Speakers with 3.5mm jack&lt;/li&gt;
  &lt;li&gt;Microphone with 3.5mm jack&lt;/li&gt;
  &lt;li&gt;3.5mm to USB audio adapter to plug the mic into the Raspberry Pi&lt;/li&gt;
  &lt;li&gt;Notebook running OSX 10.11&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Software:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;a couple of scripts I wrote/modified and uploaded on Github (&lt;a href=&quot;https://github.com/atwing/HomeAI-tutorial&quot;&gt;link&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;step-1-connect-bluetooth-remote&quot;&gt;Step 1: Connect Bluetooth remote&lt;/h3&gt;
&lt;p&gt;To link our remote we can use the &lt;code class=&quot;highlighter-rouge&quot;&gt;bluetoothctl&lt;/code&gt; command in the Terminal. The Bluetooth module should already be installed in the latest release of Raspbian, but if it is not, you can install using &lt;code class=&quot;highlighter-rouge&quot;&gt;sudo apt-get install pi-bluetooth&lt;/code&gt;.&lt;br /&gt;
We can start scanning for the remote as follows:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;bluetoothctl
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;bluetooth]# agent on
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;bluetooth]# default-agent
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;bluetooth]# scan on
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;figure&gt;
  &lt;img src=&quot;/img/2017-07-28-BT-control-assistant/scan_bt.gif&quot; alt=&quot;image alt&quot; /&gt;
  &lt;figcaption&gt;Figure 2: Scan for Bluetooth devices ... successful!&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p class=&quot;notice--info&quot;&gt;Note: You might need to run &lt;code class=&quot;highlighter-rouge&quot;&gt;sudo bluetoothctl&lt;/code&gt;, depending on your user rights.&lt;/p&gt;
&lt;p class=&quot;notice--info&quot;&gt;Note: If the Bluetooth module can’t find anything, try restarting the Bluetooth daemon: &lt;code class=&quot;highlighter-rouge&quot;&gt;sudo systemctl restart bluetooth&lt;/code&gt;. Or if it wasn’t running in the first place, use &lt;code class=&quot;highlighter-rouge&quot;&gt;sudo systemctl start bluetooth&lt;/code&gt; to start it and &lt;code class=&quot;highlighter-rouge&quot;&gt;sudo systemctl enable bluetooth&lt;/code&gt; to run it automatically at boot time.&lt;/p&gt;

&lt;p&gt;The device should show up as &lt;strong&gt;AB Shutter 3&lt;/strong&gt; or similar. Let us take a closer look:&lt;/p&gt;

&lt;figure&gt;
  &lt;img src=&quot;/img/2017-07-28-BT-control-assistant/bt_device_info.png&quot; alt=&quot;image alt&quot; /&gt;
  &lt;figcaption&gt;Figure 3: Bluetooth remote details&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;It is registered as a keyboard.  We should keep this in mind if we later want to control the Google Assistant with it. Time to connect to it:&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;bluetooth]# pair &amp;lt;device MAC address&amp;gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;bluetooth]# trust &amp;lt;device MAC address&amp;gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;bluetooth]# connect &amp;lt;device MAC address&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;figure&gt;
  &lt;img src=&quot;/img/2017-07-28-BT-control-assistant/bt_connect.png&quot; alt=&quot;image alt&quot; /&gt;
  &lt;figcaption&gt;Figure 4: Successful link with &lt;b&gt;AB Shutter 3&lt;/b&gt;&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p class=&quot;notice--info&quot;&gt;Note: You can use tab completion and don’t need to type in the full address. Use &lt;code class=&quot;highlighter-rouge&quot;&gt;pair&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;trust&lt;/code&gt; for it to automatically reconnect in the future and &lt;code class=&quot;highlighter-rouge&quot;&gt;connect&lt;/code&gt; to complete the connection.&lt;/p&gt;

&lt;h3 id=&quot;step-2-test-bluetooth-remote-input&quot;&gt;Step 2: Test Bluetooth remote input&lt;/h3&gt;
&lt;p&gt;Now we can test whether the device is properly connected and we can use it like a keyboard. I put together a short &lt;a href=&quot;https://github.com/atwing/HomeAI-tutorial/blob/master/Test_BT_trigger.py&quot;&gt;Python script&lt;/a&gt; which uses the &lt;strong&gt;evdev&lt;/strong&gt; package. Thanks to &lt;a href=&quot;https://github.com/jburgess777&quot;&gt;Jon Burgess&lt;/a&gt; for providing the base code!&lt;br /&gt;
Switch to the virtual environment which we created while installing the Google Assistant.   &lt;code class=&quot;highlighter-rouge&quot;&gt;source &amp;lt;path-to-environment&amp;gt;/env/bin/activate&lt;/code&gt;&lt;br /&gt;
I have mine in the home directory, so I typed &lt;code class=&quot;highlighter-rouge&quot;&gt;source ~/env/bin/activate&lt;/code&gt;. In the environment, we install the &lt;strong&gt;evdev&lt;/strong&gt; package using &lt;code class=&quot;highlighter-rouge&quot;&gt;sudo pip install evdev&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Once that is done, the Python script is fired up:
&lt;code class=&quot;highlighter-rouge&quot;&gt;python3 Test_BT_trigger.py&lt;/code&gt;&lt;/p&gt;

&lt;p class=&quot;notice--info&quot;&gt;Note: Make sure that the variable &lt;code class=&quot;highlighter-rouge&quot;&gt;TRIGGER_DEVICE&lt;/code&gt; in the script contains the exact name of your device in case yours is named differently.&lt;/p&gt;

&lt;p class=&quot;notice--info&quot;&gt;Comments to script: We basically load a list of input devices connect to our system and look for our trigger device by its advertised name. Once found, the device is grabbed/blocked so that other applications wont be able to use it simultaneously and then iteratively check for key events from it which we print out.&lt;/p&gt;

&lt;figure&gt;
  &lt;img src=&quot;/img/2017-07-28-BT-control-assistant/test_bt_trigger.png&quot; alt=&quot;image alt&quot; /&gt;
  &lt;figcaption&gt;Figure 5: Testing output of &lt;b&gt;AB Shutter 3&lt;/b&gt;&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Apparently the keys simulate the “Enter” key and the “Volume Up” key. But that wont be a problem, as the &lt;code class=&quot;highlighter-rouge&quot;&gt;grab()&lt;/code&gt; function prevents it from messing with other programs.&lt;/p&gt;

&lt;p&gt;Before we continue, there are a couple of issues we should take note of: The device has to already be connected when starting the script, otherwise it will just quit after checking the device list once. Furthermore, when the connected device moves out of range or is turned off, the function &lt;code class=&quot;highlighter-rouge&quot;&gt;read_loop()&lt;/code&gt; returns an &lt;strong&gt;IOError&lt;/strong&gt; with “No such device”, which also quits the program. We will fix this when we combine it with the Google Assistant. Almost there!&lt;/p&gt;

&lt;h3 id=&quot;step-3-triggering-a-google-assistant-query-with-the-remote&quot;&gt;Step 3: Triggering a Google Assistant query with the remote&lt;/h3&gt;
&lt;p&gt;Google provides us with a nice example to trigger requests pressing any key. Clone &lt;a href=&quot;https://github.com/googlesamples/assistant-sdk-python/tree/master/google-assistant-sdk/googlesamples/assistant/grpc&quot;&gt;these&lt;/a&gt; files and run &lt;code class=&quot;highlighter-rouge&quot;&gt;sudo apt-get install portaudio19-dev libffi-dev libssl-dev&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;pip install --upgrade -r requirements.txt&lt;/code&gt; to make sure you have all the necessary dependencies.&lt;/p&gt;

&lt;p&gt;After that, run &lt;code class=&quot;highlighter-rouge&quot;&gt;python3 pushtotalk.py&lt;/code&gt; from the files you just copied and press any button to test the sample.&lt;/p&gt;

&lt;figure&gt;
  &lt;img src=&quot;/img/2017-07-28-BT-control-assistant/test_pushtotalk.png&quot; alt=&quot;image alt&quot; /&gt;
  &lt;figcaption&gt;Figure 6: Testing &lt;b&gt;pushtotalk.py&lt;/b&gt;&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p class=&quot;notice--info&quot;&gt;Note: The SoundDeviceStream warning occurs when recorded frames are lost between reading iterations according to the &lt;a href=&quot;http://python-sounddevice.readthedocs.io/en/0.3.8/index.html?highlight=overflow#sounddevice.Stream.read&quot;&gt;&lt;strong&gt;sounddevice&lt;/strong&gt; documentation&lt;/a&gt;. These can be ignored if they only occur when starting the program, as was in my case. If they turn out to be a problem, try increasing the iteration and block size and see if it helps, e.g. &lt;code class=&quot;highlighter-rouge&quot;&gt;python3 pushtotalk.py --audio-iter-size=4000 --audio_block_size=8000&lt;/code&gt;. I got less frame losses after this but I couldn’t eliminate them completely.&lt;/p&gt;

&lt;p&gt;Once this is done, we can modify the script to exclusively listen to our Bluetooth remote. The main part of interest in the script is in line 314:&lt;/p&gt;
&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wait_for_user_trigger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;click&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pause&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Press Enter to send a new request...'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;continue_conversation&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;assistant&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;converse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Instead of waiting for any arbitrary key, this is where the &lt;code class=&quot;highlighter-rouge&quot;&gt;read_loop()&lt;/code&gt; function from the &lt;strong&gt;evdev&lt;/strong&gt; package should run. If we receive a trigger event from the remote, e.g. the “Enter” key, the program continues by starting a conversation with the Assistant.&lt;/p&gt;

&lt;p&gt;I split our previous Python script into two functions:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;get_BT_device_list()&lt;/code&gt; which will return the Bluetooth device if it was found, else &lt;strong&gt;None&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;wait_for_BT_trigger(device)&lt;/code&gt; the device from the previous function is listens to the keycode &lt;strong&gt;115&lt;/strong&gt; (which is the “Enter” key). We execute this in a “try-except” block to handle the &lt;strong&gt;IOError&lt;/strong&gt;, so the program doesn’t exit once the device disconnects.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The first function is called once every 5 seconds to check if the device is connected. Once it is, we will start polling for the trigger key and start a conversation whenever it is pressed.&lt;br /&gt;
I uploaded the code as &lt;code class=&quot;highlighter-rouge&quot;&gt;pushtotalk_BT.py&lt;/code&gt; into the Git repo. You will also need &lt;code class=&quot;highlighter-rouge&quot;&gt;BT_trigger.py&lt;/code&gt; for to handle the Bluetooth remote.&lt;/p&gt;

&lt;figure&gt;
  &lt;img src=&quot;/img/2017-07-28-BT-control-assistant/pushtotalk_BT.png&quot; alt=&quot;image alt&quot; /&gt;
  &lt;figcaption&gt;Figure 7: She replied &quot;Sorry, I don't understand&quot;... &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;&lt;br /&gt;&lt;br /&gt;
This concludes the Bluetooth remote feature implementation. I hope you didn’t mind the amount of detail.&lt;br /&gt;
See you in the next posts where I will add a &lt;a href=&quot;/home%20automation/custom-response-assistant/&quot;&gt;custom audio response&lt;/a&gt; whenever a request is initiated and then combine the Google Assistant with IFTTT and Home Assistant to &lt;a href=&quot;&quot;&gt;run shell commands using your voice&lt;/a&gt;!&lt;/p&gt;</content><author><name>Anthony Nguyen</name></author><category term="Home Automation" /><summary type="html">Hotword detection is an impressive and convenient feature, but has with a big drawback&amp;#58; it comes at the price of sacrificing part of our privacy. To address this issue, we can use a Bluetooth remote button to trigger a query.</summary></entry><entry><title type="html">Build a home assistant for less than 40 $</title><link href="https://www.atwing.net/home%20automation/assistant-on-pi/" rel="alternate" type="text/html" title="Build a home assistant for less than 40 $" /><published>2017-07-24T00:00:00+00:00</published><updated>2017-07-24T00:00:00+00:00</updated><id>https://www.atwing.net/home%20automation/assistant-on-pi</id><content type="html" xml:base="https://www.atwing.net/home%20automation/assistant-on-pi/">&lt;aside class=&quot;sidebar__right&quot;&gt;
&lt;nav class=&quot;toc&quot;&gt;
    &lt;header&gt;&lt;h4 class=&quot;nav__title&quot; style=&quot;text-align: left&quot;&gt;&lt;i class=&quot;fa fa-list&quot;&gt;&lt;/i&gt; Contents&lt;/h4&gt;&lt;/header&gt;
&lt;ul class=&quot;toc__menu&quot; id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#overview&quot; id=&quot;markdown-toc-overview&quot;&gt;Overview&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#step-1-preparation--ssh-connection&quot; id=&quot;markdown-toc-step-1-preparation--ssh-connection&quot;&gt;Step 1: Preparation &amp;amp; SSH connection&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#step-2-audio-configuration&quot; id=&quot;markdown-toc-step-2-audio-configuration&quot;&gt;Step 2: Audio configuration&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#step-3-create-google-developer-project--configure-google-account&quot; id=&quot;markdown-toc-step-3-create-google-developer-project--configure-google-account&quot;&gt;Step 3: Create Google Developer Project &amp;amp; configure Google account&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#step-4-install-google-assistant-sdk-and-run-demo&quot; id=&quot;markdown-toc-step-4-install-google-assistant-sdk-and-run-demo&quot;&gt;Step 4: Install Google Assistant SDK and run demo&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

  &lt;/nav&gt;
&lt;/aside&gt;

&lt;p&gt;With the Google Assistant SDK being released, speech-based home projects received a powerful new component in its arsenal and with this I thought it’s about time for me to jump on the home assistant hype train!&lt;br /&gt;
The Google Assistant will be implemented on a Raspberry Pi, which makes it a potentially more powerful and much cheaper system than the Google Home device and along with fun and joy we will hopefully learn a thing or two.&lt;/p&gt;

&lt;p&gt;This project contains a variety of features among which are:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Google Assistant running on a Raspberry Pi (covered in this post)&lt;/li&gt;
  &lt;li&gt;Use a bluetooth remote to initiate request to Google Assistant (&lt;a href=&quot;/home%20automation/BT-control-assistant/&quot;&gt;link&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;Execute shell commands with Google Assistant + IFTTT + Home Assistant with voice commands (&lt;a href=&quot;/home%20automation/shell-commands-assistant/&quot;&gt;link&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;Control home devices and ceiling lights with voice commands (&lt;a href=&quot;/home%20automation/light-switch-assistant/&quot;&gt;link&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want to use this post as a guide or inspiration for your own projects, I am going to assume that you are already somewhat familiar with Linux systems and the Raspberry Pi, although I will try to mention all important details on what I did. Alright, let’s get right to it!&lt;/p&gt;

&lt;h3 id=&quot;overview&quot;&gt;Overview&lt;/h3&gt;
&lt;figure&gt;
  &lt;img src=&quot;/img/2017-07-24-assistant-on-pi/overview_setup.jpg&quot; width=&quot;620&quot; /&gt;
  &lt;figcaption&gt;Figure 1: More cables than components&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;A quick overview on what components I used for this setup:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Raspberry Pi 3 Model B running Raspbian Jessie&lt;/li&gt;
  &lt;li&gt;Speakers with 3.5mm jack&lt;/li&gt;
  &lt;li&gt;Microphone with 3.5mm jack&lt;/li&gt;
  &lt;li&gt;3.5mm to USB audio adapter to plug the mic into the Raspberry Pi&lt;/li&gt;
  &lt;li&gt;Notebook running OSX 10.11&lt;/li&gt;
  &lt;li&gt;at least one finger&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;step-1-preparation--ssh-connection&quot;&gt;Step 1: Preparation &amp;amp; SSH connection&lt;/h3&gt;
&lt;p&gt;To start off, we first need to install the Google Assistant on a Raspberry Pi which will be the main focus of this post. Google provides a great guide for the installation, I started off &lt;a href=&quot;https://developers.google.com/assistant/sdk/develop/python/hardware/setup&quot;&gt;here&lt;/a&gt;.&lt;br /&gt;
Conveniently, I already have a Raspberry Pi 3 Model B with Raspbian Jessie installed which I use for my home surveillance system. I will access my headless Raspberry Pi via &lt;a href=&quot;https://www.raspberrypi.org/documentation/remote-access/ssh/&quot;&gt;SSH&lt;/a&gt;, so except for the power supply no additional wiring will be necessary for now, when using the WiFi connection - gotta save the cable clutter for later!&lt;br /&gt;
With the option to comfortably control the Raspberry Pi from my notebook, I connect to it via SSH in Terminal.&lt;/p&gt;

&lt;h3 id=&quot;step-2-audio-configuration&quot;&gt;Step 2: Audio configuration&lt;/h3&gt;

&lt;p&gt;The audio input and output can be set by editing the file &lt;code class=&quot;highlighter-rouge&quot;&gt;~/.asoundrc&lt;/code&gt;.
With the commands &lt;code class=&quot;highlighter-rouge&quot;&gt;arecord -l&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;aplay -l&lt;/code&gt;, we can find the card/device numbers of the desired input and output devices respectively:&lt;/p&gt;

&lt;figure&gt;
  &lt;img src=&quot;/img/2017-07-24-assistant-on-pi/input-output-devices.png&quot; /&gt;
  &lt;figcaption&gt;Figure 2: List of input and output devices&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Choosing my USB dongle as input and the 3.5mm jack as output, my &lt;code class=&quot;highlighter-rouge&quot;&gt;.asoundrc&lt;/code&gt; file is changed accordingly:&lt;/p&gt;

&lt;figure&gt;
  &lt;img src=&quot;/img/2017-07-24-assistant-on-pi/asoundrc-config.png&quot; /&gt;
  &lt;figcaption&gt;Figure 3: Content of &lt;b&gt;.asoundrc&lt;/b&gt;&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;speaker-test &lt;span class=&quot;nt&quot;&gt;-t&lt;/span&gt; wav &lt;span class=&quot;c&quot;&gt;# test output&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;arecord &lt;span class=&quot;nt&quot;&gt;--duration&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;5 &lt;span class=&quot;nt&quot;&gt;-f&lt;/span&gt; S16_LE &lt;span class=&quot;nt&quot;&gt;--rate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;16k ~/test.wav &lt;span class=&quot;c&quot;&gt;# test input&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;aplay ~/test.wav &lt;span class=&quot;c&quot;&gt;# check recording&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;All good? Onwards!&lt;/p&gt;

&lt;h3 id=&quot;step-3-create-google-developer-project--configure-google-account&quot;&gt;Step 3: Create Google Developer Project &amp;amp; configure Google account&lt;/h3&gt;

&lt;p&gt;This step is fairly straight-forward: &lt;a href=&quot;https://developers.google.com/assistant/sdk/develop/python/config-dev-project-and-account&quot;&gt;follow this&lt;/a&gt;&lt;br /&gt;
We need to create a Google Developer Project, grant the Raspberry Pi access to the Google Assistant API by downloading a client secret JSON file. Since I downloaded the JSON file on my notebook, I have to transfer the file on my Raspberry Pi via SFTP (e.g. using FileZilla) or alternatively by simply executing in the Terminal:&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;scp &amp;lt;path to file&amp;gt;/client_secret_&amp;lt;client-id&amp;gt;.json pi@&amp;lt;raspberry-pi-ip-address&amp;gt;:/home/pi/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After enabling all necessary activity controls, we arrive at the final step.&lt;/p&gt;

&lt;h3 id=&quot;step-4-install-google-assistant-sdk-and-run-demo&quot;&gt;Step 4: Install Google Assistant SDK and run demo&lt;/h3&gt;

&lt;p&gt;Again, the guide is quite thorough in the &lt;a href=&quot;https://developers.google.com/assistant/sdk/develop/python/run-sample&quot;&gt;last step&lt;/a&gt;. If the Assistant responds to the voice command &lt;em&gt;Hey Google&lt;/em&gt; or &lt;em&gt;Ok Google&lt;/em&gt;, we are ready to proceed to the next exciting part: &lt;a href=&quot;/home%20automation/BT-control-assistant/&quot;&gt;Start a conversation with a bluetooth remote&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you later want to come back and test the Assistant, execute on the Raspberry Pi:&lt;/p&gt;
&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;source&lt;/span&gt; ~/env/bin/activate
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;google-assistant-demo
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;figure&gt;
  &lt;img src=&quot;/img/2017-07-24-assistant-on-pi/google-assistant-demo.png&quot; /&gt;
  &lt;figcaption&gt;Figure 4: Google Assistant Demo&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;&lt;br /&gt;&lt;br /&gt;
Feel free to post questions/comments/suggestions and let me know if you run into any trouble!&lt;/p&gt;</content><author><name>Anthony Nguyen</name></author><category term="Home Automation" /><summary type="html">With the Google Assistant SDK released three months ago, home projects have received a great skill set. In this project the Google Assistant is implemented on a Raspberry Pi to later control switchable devices such as fans, lights, computers using voice commands.</summary></entry></feed>