As more businesses begin to slowly open, we are faced with the problem of how we keep our employees safe in this new world we are living in.
A small but potentially hazardous point of contact that employees interact with throughout the day are light switches. Whether you are the first one in the office that day or you are entering/leaving a conference room, light switches are one of the few points of contact that multiple people use throughout the day.
In order to help slow the spread of COVID19, we have released this guide on how to create your own smart light switch using a MATRIX Voice and Rhasspy, an offline open source voice assistant. With this guide your IT department can purchase the needed materials, easily setup & customize the device for your use case, and in under an hour you have a safe and privacy-minded voice controlled light switch.
For a video version of this guide, see below.
Required HardwareBefore getting started, let's review what you'll need.
- Raspberry Pi 3B/B+ or 4.
- 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.
- Power adapter for Raspberry Pi.
- Micro SD Card (Minimum 8 GB) - An operating system is required to get started. You can download Raspbian and use the guides for Mac OS, Linux and Windows on the Raspberry Pi website.
- (Optional) 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)
- A relay board like this one
OR
- A MATRIX Kit
- A relay board like this one
If 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.
1. Install MATRIX Libraries and RhasspyFollow the steps below in order for Rhasspy to work on your Raspberry Pi.
2. Creating an IntentOpen Rhasspy's web interface by opening your browser to http://YOUR_PI_IP_HERE:12101 and then click on the Sentences tab. Here is where all intents and sentences are defined.
By default there are a few example sentences in the text box. Remove the default intents and add the following:
[Light]
turn [the] light (on | off){power}
light (on | off){power}
Once created, click on Save Sentences and wait for Rhasspy to finish training. Here Light
is an intent and power
is a slot.
This step will show you how to setup a MATRIX Lite project with Rhasspy. assistant.js
will be used to listen and respond to events from the Rhasspy Web Socket API.
SSH into your Pi and run the following commands:
Install git.
sudo apt install git
Download our example repository.
cd /home/pi/lite_js
curl https://raw.githubusercontent.com/matrix-io/rhasspy-light-switch/master/code/assistant.js >> assistant.js
curl https://raw.githubusercontent.com/matrix-io/rhasspy-light-switch/master/code/everloop.js >> everloop.js
curl https://raw.githubusercontent.com/matrix-io/rhasspy-light-switch/master/code/relay.js >> relay.js
Install the project dependencies.
npm install ws --save
npm install http --save
everloop.js
incorporates the code required to control the LEDs on your MATRIX device.
// Variables
const matrix = require("@matrix-io/matrix-lite");
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 available 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(relayPin, "DIGITAL");
matrix.gpio.setMode(relayPin,"output");
// On & Off Methods
methods.lightOn = function() {
matrix.gpio.setDigital(relayPin,"ON");
console.log("\n**Light Has Been Turned On**");
};
methods.lightOff = function() {
matrix.gpio.setDigital(relayPin,"OFF");
console.log("\n**Light Has Been Turned Off**");
};
module.exports = methods;
// Export methods in order to make them available 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 Light
intents and read power
slots, both of which we created in the previous section.
// Variables
const WebSocket = require("ws");
const matrix = require("@matrix-io/matrix-lite");
const https = require("http");
const ws = new WebSocket("ws://localhost:12101/api/events/intent");
const wake = new WebSocket("ws://localhost:12101/api/events/wake");
var relay = require('./relay.js');
var everloop = require('./everloop.js');
console.log("**Started Web Socket Client**");
ws.on("open", function open() {
console.log("\n**Connected**\n");
});
ws.on("close", function close() {
console.log("\n**Disconnected**\n");
});
// On WakeWord
wake.on("message", function wake() {
everloop.startWaiting();
console.log("\n**WakeWord Detected**\n");
});
// Intents are passed through here
ws.on("message", function incoming(data) {
everloop.stopWaiting();
data = JSON.parse(data);
console.log("**Captured New Intent**");
console.log(data);
// Turn the light On/Off
if ("Light" === data.intent.name) {
switch (data.slots.power) {
case "on":
relay.lightOn();
break;
case "off":
relay.lightOff();
break;
}
};
});
4. Wiring the RelayDepending 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 tapeto any exposed contacts afterwards!
5. 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 and using a Pi 3 for your project, 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
Please log in or sign up to comment.