Last active
November 14, 2022 13:07
-
-
Save cshotton/47fbc90dbb5f0508863ebb61425b68d3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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