Skip to content

Instantly share code, notes, and snippets.

@cshotton
Last active November 14, 2022 13:07
Show Gist options
  • Save cshotton/47fbc90dbb5f0508863ebb61425b68d3 to your computer and use it in GitHub Desktop.
Save cshotton/47fbc90dbb5f0508863ebb61425b68d3 to your computer and use it in GitHub Desktop.
// index.js
console.log ("Running...");
const Gpio = require('onoff').Gpio; // https://www.npmjs.com/package/onoff
const motion = new Gpio(17, 'in', 'both');
var Sound = require('node-aplay'); // https://www.npmjs.com/search?q=node-aplay
var music = new Sound('./loser.wav');
motion.watch ((err, value)=> {
if (err) throw err;
console.log (`Motion: ${value}`);
if (value==1) {
music.play();
}
else {
music.pause();
}
})
process.on('SIGINT', _ => {
motion.unexport();
console.log ('\nBye!');
process.exit();
});
// package.json
{
"name": "motion",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "cshotton",
"license": "ISC",
"dependencies": {
"node-aplay": "^1.0.3",
"onoff": "^6.0.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment