Instantly share code, notes, and snippets.

Embed
What would you like to do?
A script for the Raspberry Pi that turns on a Powerswitch when the user says "Lumos!"
const record = require('node-record-lpcm16');
const {Detector, Models} = require('./node_modules/snowboy/');
const Gpio = require('onoff').Gpio;
// Set up control of the pin that toggles the Powerswitch
power = new Gpio(14, 'out');
var on = false;
// Turn the power off by default
power.writeSync(0);
const models = new Models();
// Add "Lumos!" hotword
models.add({
file: 'Lumos.pmdl',
sensitivity: '0.5',
hotwords : 'lumos'
});
// Add "Nox!" hotword
models.add({
file: 'Nox.pmdl',
sensitivity: '0.5',
hotwords : 'nox'
});
const detector = new Detector({
resource: "./node_modules/snowboy/resources/common.res",
models: models,
audioGain: 2.0
});
detector.on('silence', function () {
});
detector.on('sound', function () {
console.log('sound');
});
detector.on('error', function () {
console.log('error');
});
detector.on('hotword', function (index, hotword) {
console.log('hotword', index, hotword);
if(hotword == 'lumos')
// Turn on power to lights
power.writeSync(1);
else if(hotword == 'nox')
// Turn off power
power.writeSync(0);
});
const mic = record.start({
threshold: 0,
verbose: true
});
mic.pipe(detector);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment