DIY,  Technology

Smart Mirror Build (Part 2: Software)

I recently made a smart mirror, and here’s how I did it. This article will be about the software setup. Part 1, will be about the hardware. Oh. And like 99% of the credit goes to Evan Cohen, and the people at http://smart-mirror.io/

For the sake of easiness, I did this first, then put it all in the cabinet. You will need an SD Card (at least 8 gigs. I went with 64 because they are cheap), a Raspberry Pi (I went with the Pi 3 B, because it’s the newest), and an existing computer to download Raspbian.

First, download Raspbian. You want the Desktop version. Use Win32DiskImager to put it on the SD Card. When done, pop the SD card into the Pi, make sure everything is plugged in, and get ready for some CLI fun.

Since we are using a large SD card, and the Raspbian image isn’t taking up that much space, any extra space is wasted. You can either run Raspian Config from the GUI or run this in a terminal and select “expand the file system”.

  • sudo rasp-config

Now that that is done, it’s time to start the fun stuff. We need Node and NPM to do things.

  • wget https://nodejs.org/dist/v4.4.3/node-v4.4.3-linux-armv7l.tar.gz
  • tar -xvf node-v4.4.3-linux-armv7l.tar.gz
  • cd node-v4.4.3-linux-armv7l
  • sudo cp -R * /usr/local/

wget downloads the file. tar -x-v-f unzips it, and cd changes the directory to the recently unzipped directory. Lastly, we copied files to /usr/local

  • sudo apt-get install python-pyaudio python3-pyaudio sox

This installs dependencies and a few things like python.

  • cd ~ git clone https://github.com/evancohen/smart-mirror.git

cd changes to the default directory. git then pulls the smart mirror code from github.

Now’s a good time to run this to make sure you have everything else updated.

  • sudo apt-get update && sudo apt-get upgrade -y

You might not need to do this, but it doesn’t take long, so you might as well. Plug in the microphone, and restart, just in case.

  • sudo shutdown -r now

When it boots back up, open a terminal and type

  • arecord -l

This lists all recording devices. Find the microphone. It will be something like hw1:0. Now enter that number in the following file.

  • nano ~/.asoundrc

Look for “pcm.dsnooped” and put the hw code there. Save and exit. Restart the Pi.

  • sudo shutdown -r now

The smart mirror uses a config.js file to customize the experience for you. It comes with a default. We copy that to a new file that we can edit.

  • cp config.example.js config.js

Now we can edit that file to our hearts content. There’s a lot to go through here, so I’ll point you to the Smart Mirror docs to break it all down.

There are a few things you need though. First, train the speech model. This can be any keyword you want. The default is “smart mirror”. I changed mine to “ok, computer” because Star Trek. Whatever you decide, you can go here. You’ll have to log in with Facebook for some asinine reason. You can always remove it’s permissions from Facebook when you’re done. Follow the instructions. You’ll have to say the phrase three times, then confirm that it works, then download the file. Place the file in the smart-mirror directory. Also, there can’t be any spaces in the filename. Don’t forget to change the keyword and model in the config.js file to match the hotword you’ve chosen.

For the most part, the config file has clear instructions. Change whatever you like to suit your needs.

Now the most frustrating part of this. The Chromium speech keys. I had so much trouble with this. Google wants you to use their cloud speech recognition, which doesn’t work with this, so you have to do it the old way. First, log into whatever gmail account you’re going to use. Subscribe to this. Next go here. Click “Create Project”. Call it whatever you want. Click on the three lines in the top left corner and click “API Manager”. Click “Enable API” and select “Speech API” NOT NOT NOT NOT Cloud Speech API. It has to be just “Speech API”.

We need three different keys to get the speech recognition working. the API Key, the Client ID, and the Secret. First, go the Credentials. Click “Create Credentials” and select API Key. Save this number somewhere. Next click “Create Credentials” again, but this time, select OAuth Client. Select Other. This will give you the Client ID and the Secret. Save those.

Edit the Config file.

  • nano smart-mirror/config.js

Scroll down until you find the Chromium Keys section in the file and enter the above keys. They do not get quotation marks. Restart your Pi.

  • sudo shutdown -r now

Now let’s clean up the area by turning off the screensaver.

  • nano /home/pi/./config/lxsession/LXDE-pi/autostart

Change @xscreensaver to #@xscreensaver. Then add this to the end.

  • @xset s off
    @xset -dpms
    @xset s noblank

Next, install unclutter to hide the mouse when not in use.

  • sudo apt-get install unclutter

Then edit a different autostart file.

  • sudo nano /etc/xdg/lxsession/LXDE-pi/autostart

Add this at the end.

  • unclutter -idle 0.1 -root

Install npm and test.

  • cd smart-mirror && npm install

When it’s done, test.

  • npm start

If everything is working, you should see the Smart Mirror software. Try the speech recognition. Does it work? Sweet. Press CTRL+C to quit.

If everything is working, let’s set it up to start on boot.

  • nano ~/smart-start.sh

This will be a blank file. Add this.

  • #!/bin/bash
    export DISPLAY:0
    export XAUTHORITY=/home/pi/.Xauthority
    cd /home/pi/smart-mirror && npm start

Save and close. Then own the file and make it executable.

  • chown pi:pi /home/pi/smart-start.sh
  • chmod +x /home/pi/smart-start.sh

Lastly, make the autostart file read the sh file.

  • nano /home/pi/.config/lxsession/LXDE-pi/autostart

Add this line at the end.

  • /home/pi/smart-start.sh &

Now is a good time to rotate the monitor.

  • sudo nano /boot/config.txt

Add this line. You might need to use 3 if the monitor is flipped.

  • display_rotate=1

Restart the pi, and you should be good to go.

  • sudo shutdown -r now

It should reboot, and automatically start the smart mirror software. If you have any questions, visit the GitHub page. They have a very helpful group of people.

Oh. Anytime you want to update the smart mirror software, just type this into terminal.

  • git pull

To update the Pi, run this.

  • sudo apt-get update && sudo apt-get upgrade -y

And you should be able to do most of this via SSH.

 

My name is Chris. I currently live in Seattle, though I’m formerly from California. I'm a writer, comic, and superhero (allegedly). I complain. A lot. About everything. I also tell jokes.

4 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.