DEV Community

Paul
Paul

Posted on

Voice-Activated Lights with Raspberry Pi and Google Assistant

I built a Raspberry Pi-powered Google Assistant for my daughter's room so she could turn on her lights with her voice. But before I get into it, I want to give a little background.

My daughter is disabled. We live in an apartment, which means I can't make drastic changes, and it also means that most things in the apartment are not really accessible for her. We've done what we can, but we can't do anything about the wiring. So if I can't move the light switch (which would be a bad idea anyway as I'm not an electrician), I figured I'd change how she turns on her light altogether.

Here's my daughter demonstrating how it works:

I didn’t want to just buy a Google Home. Instead, I bought a Raspberry Pi and some other equipment:

  • Raspberry Pi 3
  • Speakers
  • Microphone
  • Wires + LED light
  • Smart plugs (I found a 2-pack on Amazon)

Following this tutorial, I was able to get the Pi connected to my Google account as an Assistant client. For the most part, the tutorial worked like a charm. Unfortunately, there were a couple sticky bits.

When setting up the speakers and microphone, I had to change the rate line from “16k” to “16000” on these two lines to avoid getting an invalid argument error:

arecord --format=S16_LE --duration=5 --rate=16k --file-type=raw out.raw
aplay --format=S16_LE --rate=16k out.raw

When I initially ran the main.py script, I got another error:

TypeError: __init__() missing 1 required positional argument: 'device_model_id'

This was because since the article was written, Google updated the Assistant API. (Google gonna Google.) Since making these changes, developers now have to register custom devices like the Raspberry Pi. A couple extra steps proved fairly easy to do (as documented by Google on their Python docs), and the Raspberry Pi was up and running as a Google Assistant.

Following the tutorial’s directions, I was able to get the LED connected just fine. It lights up when the Pi is listening, and turns off when it’s not.

Raspberry Pi 3 with speakers and microphone

Here’s where the tutorial ended, and I had to figure out the next steps for myself. I had my 2-pack of smart plugs, and as it turned out, they have a companion app: Smart Life.

I plugged in the two smart plugs in Namine’s room. I plugged her lamp into one, and her moon night light into the other. (The lamp was plugged into the outlet controlled by the light switch, which Namine can’t reach. Now it’s plugged into an always-on outlet.)

In retrospect, I probably should have taken some screenshots of the setup process. Anyway, the app required me to create an account, then it scanned for available devices. It detected the two I had just plugged in and turned on, so it let me add them and name them.

Screenshot of Smart Life app

The app also let me define rooms, so I could turn every smart device off in a room, were I so inclined. The app also has timing capability, for turning lights on or off at a certain time of day, and so on.

But that’s enough of that. The Smart Life app was simply a means to an end; I never meant to control anything in the apartment from this app on a long-term basis. The real goal was connecting the devices to Google Assistant, which, as it turned out, was just as easy as the initial device detection.

I opened the Google Assistant app, and navigated to Settings > Home control. I clicked the blue “plus” button in the bottom right corner to create a new device — rather, in this case, connect an existing device.

Starting out fresh, there were no linked services. I scrolled down and found Smart Life in the list, thereby allowing me to access my two smart plugs from within Google Assistant. Because Assistant is configured to use my Google account both on my phone and on the Raspberry Pi, I can control them from either device. (That includes controlling the lights when I’m away from home, as well.)

The rooms configured in Smart Life have no bearing on the rooms in Google Assistant, so I created new rooms with the same names. The two plugs’ names carried over from Smart Life: “Lamp” and “Moon.”

Here’s where things get tricky, because as much as these digital assistants (Google Assistant, Siri, Alexa, Cortana, and others) are touted as AI, they are still not capable of true natural language. It also — at least in Assistant’s case — is not yet capable of being taught how to pronounce new words or names.

My daughter's name is Namine. That's pronounced "NAH-me-nay." (Ten points for the Hogwarts house of your choice if you can identify where it's from. ;) Google Assistant pronounces it like "nominee," though, so naming the lamp "Namine's lamp" resulting in Assistant not recognizing it.

Interestingly enough, if I said “turn on the lamp” it did turn on the lamp, but it would then respond, “Turning on the nominee’s lamp.” So not only did there seem to be no way for me to teach Assistant how to pronounce her name, but it also didn’t recognize the possessive noun. No matter what, it always responded by referring to it as “the [nickname].” (Even if I named it "my lamp," it still responded with "turning on the my lamp.")

As a result, I had to get creative with the name. After some trial and error, I ended up giving it the phonetic nickname “lamp in naameenayz room.” (It took a few tries to get it to pronounce Namine’s name just right.)

Screenshot of Google Assistant devices

Despite all this, it still does not recognize the command “turn on Namine’s lamp,” even though the phonetic name that Assistant recognizes is exactly how we pronounce her name. On the other hand, the simpler command “turn on the lamp” still works. It then responds with “turning on the lamp in Namine’s room,” which is acceptable enough. Content with the results — or content enough, for now — I set up Namine’s moon night light, which hangs from her ceiling, in the same way.


A version of this post was orginally published on eichefam.net.

Oldest comments (6)

Collapse
 
ben profile image
Ben Halpern

Bookmarked for my own future home setup 🙂

Collapse
 
ben profile image
Ben Halpern

But also, it's amazing you're doing this for your daughter!!

Collapse
 
superdiana profile image
Super Diana

You are awesome! I will bookmark this too!. Big hugs to you and cute Namine <3

Collapse
 
jessicatv profile image
⚡𝕁𝕖ss𝕚c𝕒 T𝕣𝕒v𝕚𝕖so 🎠

Awesome!

Collapse
 
sidhantpanda profile image
Sidhant Panda

Awesome! I had setup something similar with Google Assistant (without the smart home integration) -> medium.com/@sidhantpanda/raspberry...

Lot to learn from this and implement in my project too now :)

Thank you!

Collapse
 
michaeltharrington profile image
Michael Tharrington

Wow, this is so freaking cool, Paul! Very nicely done. 🙌

I loved hearing Namine's shoutout to ya at the end of the video — so sweet. 💚