Thursday, January 18, 2018

Getting started with Snips and HomeAssistant: Part 1

So I have been using Snips to add voice controls to my Home Assistant setup. I just want to document some of the most frequent questions I get and give people some guidance.

Why use Snips instead of Alexa or Google Home? Because I really don't want a huge company recording everything I say.

Setting up Home Assistant is a whole topic in itself, but is not actually required to use snips. In a later post I'll give some example Python code you can use to set up your own voice responses.

Snips is in pretty fast development so this will probably get outdated soon enough, but will hopefully still have good information for people.

Installing Snips

Snips has their own documentation here so if you run into issues go back to their docs.

My installs are on Raspberry Pi 3 running Raspbian. I have been mucking with Linux for a long time so I prefer to have my own OS and such I can tinker with so I won't detail the docker installation.

The actual installation instructions are here: https://github.com/snipsco/snips-platform-documentation/wiki/1.-Setup-the-Snips-Voice-Platform

Hardware

I have two setups

That Jabra is a lot more expensive than when I bought it, get the Respeaker since it also give you pretty lights you can use to show when Snips is listening. Downside, is you need speakers to get sound output then, but that's a good thing since you can then use your Raspberry to play music too.

Sound Setup

The Snips software can simply use your alsa settings. Jabra is pretty straight forward, just plug it in and copy this to your asound.conf

Jabra /etc/asound.conf

pcm.!default {
  type asym
  playback.pcm {
    type plug
    slave {
      pcm "hw:0,0"
      rate 48000
      format "S16_LE"
      channels 2
    }
  }
  capture.pcm {
    type plug
    slave.pcm "hw:1,0"
  }
}

ctl.!default {
  type hw
  card 1
}


Respeaker 4 Mic Hat

This install is a little more complicated, from their oficial installation instructions here: https://github.com/SeeedDocument/ReSpeaker-4-Mic-Array-for-Raspberry-Pi/blob/master/ReSpeaker-4-Mic-Array-for-Raspberry-Pi.md

Get the seeed voice card source code.
# as your pi user on your raspberry
mkdir /home/pi/WORK
git clone https://github.com/respeaker/seeed-voicecard.git
cd seeed-voicecard
sudo ./install.sh 4mic
reboot
Then select the headphone jack on Raspberry Pi for audio output:
sudo raspi-config
# Select 7 Advanced Options
# Select A4 Audio
# Select 1 Force 3.5mm ('headphone') jack
# Select Finish
Check that the sound card name looks like this:
pi@raspberrypi:~/seeed-voicecard $ arecord -L
null
    Discard all samples (playback) or generate zero samples (capture)
playback
capture
dmixed
array
ac108
default:CARD=seeed4micvoicec
    seeed-4mic-voicecard,
    Default Audio Device
sysdefault:CARD=seeed4micvoicec
    seeed-4mic-voicecard,
    Default Audio Device
dmix:CARD=seeed4micvoicec,DEV=0
    seeed-4mic-voicecard,
    Direct sample mixing device
dsnoop:CARD=seeed4micvoicec,DEV=0
    seeed-4mic-voicecard,
    Direct sample snooping device
hw:CARD=seeed4micvoicec,DEV=0
    seeed-4mic-voicecard,
    Direct hardware device without any conversions
plughw:CARD=seeed4micvoicec,DEV=0
    seeed-4mic-voicecard,
    Hardware device with all software conversions

Respeaker 4 Mic Hat /etc/asound.conf

pcm.!default {
    type asym
    playback.pcm "playback"
    capture.pcm "ac108"
}

pcm.playback {
    type plug
    slave.pcm "hw:0,0"
}


pcm.dmixed {
    type dmix
    slave.pcm "hw:0,0"
    ipc_key 555555
}


pcm.ac108 {
        type ac108
        slavepcm "hw:1,0"
        channels 4
}


Setting up the pretty lights

Here are the instructions for setting up the pretty led lights. In a later part we will detail how to how this go on automatically when Snips is listening.


cd /home/pi/WORK
git clone https://github.com/respeaker/4mics_hat.git
cd /home/pi/WORK/4mics_hat
sudo apt install python-virtualenv          # install python virtualenv tool
virtualenv --system-site-packages ~/env     # create a virtual python environment
source ~/env/bin/activate                   # activate the virtual environment
pip install spidev gpiozero           # install spidev and gpiozero
python pixels.py
You should see pretty lights flashing on the respeaker

Testing Sound

With the Jabra you will get sound from the speaker, for the Respeaker you need to plug in external speakers.


arecord test.wav

aplay test.wav

You should hear whatever you recorded. If that works you can now start with configuring Snips in Part 2