Before getting started, let's review what you'll need.
- Raspberry Pi 3 B/B+ (Recommended) or Pi 2 Model B (Supported).
- MATRIX Voice or MATRIX Creator - Raspberry Pi does not have a built-in microphone, the MATRIX Voice & MATRIX Creator each have an 8 mic array - Buy MATRIX Voice or Buy MATRIX Creator.
- Micro-USB power adapter for Raspberry Pi.
- Micro SD Card (Minimum 8 GB) - An operating system is required to get started. You can download Raspbian Stretch and use the guides for Mac OS, Linux and Windows In the Raspberry Pi website.
- Micro-USB Cable
- A USB Keyboard & Mouse, and an external HDMI Monitor - we also recommend having a USB keyboard and mouse as well as an HDMI monitor handy. You can also use the Raspberry Pi remotely, see this guide from Google.
- Internet connection (Ethernet or WiFi)
- (Optional) WiFi Wireless Adapter for Pi 2. Note: Pi 3 has built-in WiFi.
- A relay board like this one
OR
- A MATRIX Kit
- A relay board like this one
On your personal computer, you'll need the following:
- Node.js: Dependancy for Sam CLI Tool.
- Etcher.io: To easily flash our Raspbian Stretch image.
- Snips' Sam CLI Tool: Creates & manages Snip assistants on your Raspberry Pi. (Windows users may need to run their terminal session as Administrator when installing the Sam CLI Tool)
You'll also need a registered snips.ai account.
Let's Get StartedIf you haven't already, be sure to setup your Raspberry Pi with your MATRIX Device.
Once setup, ensure you enable SSH on your Raspberry Pi. This allows the SAM CLI tool to connect to your Pi.
Follow the steps below in order for Snips to work on your Raspberry Pi.
1. Connect SAM To Your Raspberry Pi (Personal Computer)By this step, you should have a snips.ai account and the Sam CLI Tool installed.
From your computer's terminal, sign in through the Sam CLI Tool.
sam login
Connect to your Raspberry Pi. When prompted for a username & password, you can press the enter key to insert the default Raspberry Pi credentials.
sam connect YOUR.PI.IP.HERE
Check if Snips is running.
sam status
# Example output
# If all services are not (running), use the `sam init` command
Service status:
snips-analytics .............. 0.60.10 (running)
snips-asr .................... 0.60.10 (running)
snips-audio-server ........... 0.60.10 (running)
snips-dialogue ............... 0.60.10 (running)
snips-hotword ................ 0.60.10 (running)
snips-nlu .................... 0.60.10 (running)
snips-skill-server ........... 0.60.10 (running)
snips-tts .................... 0.60.10 (running)
Begin viewing the Snips event stream. This will let us verify if your intents are being heard.
sam watch
2. Creating A Snips Assistant & AppSign into your Snips.ai and create an assistant. Feel free to choose the wakeword you want. Once created, make a new application named lights
.
Intents are what Snips uses to handle user requests. For this Assistant, we'll start by creating an intent for turning On/Off the MATRIX Creator's LEDs.
Create a new intent called lightState
for your lights
app.
Once you've defined your intent, you need to extract whether the user wants their lights on
or off
. This is where slots come in.
Create a new slot called power
and a custom slot type with the same name.
Make sure your power
slot type has on
& off
as values.
Create example sentences containing the words on and off. Highlight these words in your example sentences to assign them to your recently created power
slot.
Be sure to save!
Button is on the bottom right corner of the page.
You can now deploy your assistant! On the bottom right of the page will be a Deploy Assistant
button.
If you don't see Deploy Assistant
, increase your web browser's width.
Use the Sam CLI Tool to deploy the assistant to your Raspberry Pi. Below is an example of the command to use.
Congrats on deploying your Snips assistant!
5. Creating assistant.js (Raspberry Pi)This step will go show you how to setup a MATRIX Lite project with Snips. assistant.js
will be used to listen and respond to events from your Snips assistant
These files will go on your Raspberry Pi.
Copy the 3 required JavaScript files.
cd /home/pi/lite_js
curl https://raw.githubusercontent.com/matrix-io/snips-light-switch/master/code/assistant.js >> assistant.js
curl https://raw.githubusercontent.com/matrix-io/snips-light-switch/master/code/everloop.js >> everloop.js
curl https://raw.githubusercontent.com/matrix-io/snips-light-switch/master/code/relay.js >> relay.js
MQTT must be installed and used to listen in on events from Snips.
npm install mqtt --save
everloop.js
incorporates the code required to control the LEDs on the MATRIX Creator.
/////////////
//VARIABLES//
/////////////
const matrix = require("@matrix-io/matrix-lite");
var matrix_device_leds = 0;// Holds amount of LEDs on MATRIX device
var methods = {};// Declaration of method controls at the end
var waitingToggle = false;
var counter = 0;
setInterval(function(){
// Turns off all LEDs
if (waitingToggle == false) {
// Set individual LED value
matrix.led.set({});
}
// Creates pulsing LED effect
else if (waitingToggle == true) {
// Set individual LED value
matrix.led.set({
b: (Math.round((Math.sin(counter) + 1) * 100) + 10),// Math used to make pulsing effect
});
};
counter = counter + 0.2;
// Store the Everloop image in MATRIX configuration
},50);
///////////////////
//WAITING METHODS//
///////////////////
methods.startWaiting = function() {
waitingToggle = true;
};
methods.stopWaiting = function() {
waitingToggle = false;
};
module.exports = methods;
// Export methods in order to make them avaialble to other files
relay.js
includes the code required to turn the relay on and off. This relay will control power to the light switch.
/////////////
//VARIABLES//
/////////////
const matrix = require("@matrix-io/matrix-lite");
var methods = {};// Declaration of method controls at the end
var relayPin = 0;// The GPIO pin connected to your relay
matrix.gpio.setFunction(0, "DIGITAL");
matrix.gpio.setMode(0,"output");
////////////////////
//ON & OFF METHODS//
////////////////////
methods.lightsOff = function() {
matrix.gpio.setDigital(0,"ON");
console.log("Lights Have Been Turned Off");
};
methods.lightsOn = function() {
matrix.gpio.setDigital(0,"OFF");
console.log("Lights Have Been Turned On");
};
module.exports = methods;
// Export methods in order to make them avaialble to other files
assistant.js
combines all the code from everloop.js
& relay.js
to control the everloop and relay switch through voice. It shows you how to listen to lightState
intents and read slots. We created the lightState
intents and related slots in the previous section.
/////////////
//VARIABLES//
/////////////
var mqtt = require('mqtt');
var client = mqtt.connect('mqtt://localhost', { port: 1883 });
var relay = require('./relay.js');
var everloop = require('./everloop.js');
var snipsUserName = 'YOUR_SNIPS_USERNAME';
var wakeword = 'hermes/hotword/default/detected';
var sessionEnd = 'hermes/dialogueManager/sessionEnded';
var lightState = 'hermes/intent/'+snipsUserName+':Lights';
//////////////
//ON CONNECT//
//////////////
client.on('connect', function() {
console.log('Connected to Snips MQTT server\n');
client.subscribe('hermes/hotword/default/detected');
client.subscribe('hermes/dialogueManager/sessionEnded');
client.subscribe(lightState);
});
//////////////
//ON MESSAGE//
//////////////
client.on('message', function(topic,message) {
var message = JSON.parse(message);
switch(topic) {
// * On Wakeword
case wakeword:
everloop.startWaiting();
console.log('Wakeword Detected');
break;
// * On Light State Change
case lightState:
// Turn lights On/Off
try{
if (message.slots[0].rawValue === 'on'){
relay.lightsOn();
everloop.stopWaiting();
console.log('Lights On');
}
else if(message.slots[0].rawValue === 'off'){
relay.lightsOff();
everloop.stopWaiting();
console.log('Lights Off');
}
}
// Expect error if `on` or `off` is not heard
catch(e){
console.log('Did not receive an On/Off state')
}
break;
// * On Conversation End
case sessionEnd:
everloop.stopWaiting();
console.log('Session Ended\n');
break;
}
});
Don't forget to edit assistant.js
and replace the snipsUserName variable with your snips username which you will find in the top right corner of your Snips console.
Depending on the relay you use, the main connections to input to the relay are power (VCC), ground (GND), and signal (IN).
For the relay below, you would connect 5V from your MATRIX device to VCC, the ground pin from your MATRIX device to GND, and GPIO pin 0 to IN to match the relay.js
file.
Before starting this part, flip the breaker in your house that powers the light switch you will be working on. That way there is no electricity running through the wires while you are handling them.
Open up the wall switch you chose. Under the plastic, you will have to unscrew the switch, behind which there will be 2 live wires, and 1 green ground wire connected to the switch. If there is no ground wire then feel free to move on.
Disconnect all 3 wires from the switch. We will only need the 2 live wires for this guide so you can use a twist on wire connector or electrical tape to cover the ground wire
The relay has 3 outputs that you could connect the 2 live wires from the wall to. Whether the default state of your wall lights is on or off will depend on which output contacts on the relay you connect to.
For example, if you wire the relay outputs as shown below, the lights will be off when you send no signal from your MATRIX device, and on when you send an "on" signal from your MATRIX device. The signal from your MATRIX device controls the COM port, so one of the wires always has to be connected to the COM port. NO means "normally open", and NC means "normally connected".
Be sure to thoroughly apply electrical tape to any exposed contacts afterwards!
7. Printing & Assembling the CaseWe designed a case to replace the wall light switch with the MATRIX Voice + Raspberry Pi assembly. If you would like to print a similar case, find the STL files attached here.
The back piece of the case has a hole for all the wires to go through.
P.S. If you are printing the case, you could consider stripping the power supply and soldering it to the back of your Raspberry Pi as shown in the picture below (to PP1 and PP6). This will negate the need to have the microUSB cable attached to the Pi, and the Pi + MATRIX device assembly will fit nicer in the case.
Be advised that soldering to your Raspberry Pi may void its warranty.
The back piece is secured to the front piece using four screws as shown below.
And there you have it! Your custom light switch complete with an enclosure and ready to be controlled using voice commands!
Enjoy the laziness!
Comments