Skip to content

Commit

Permalink
add support for external motion detection
Browse files Browse the repository at this point in the history
  • Loading branch information
sdetweil committed Jun 13, 2019
1 parent 9163ff4 commit 99cfc7f
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 6 deletions.
66 changes: 63 additions & 3 deletions motion.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
'use strict'
const fs = require('fs');
const path = require('path');
const detectionDir='./motion';
const detectionFile='detected';
// Load in smart mirror config
var config = require("./config.json")

if(!config || !config.motion || !config.motion.enabled || !config.motion.pin || !config.general.language){
console.log("!E:","Configuration Error! See: https://docs.smart-mirror.io/docs/configure_the_mirror.html#motion")
}

if (config.motion.enabled == true && require.resolve('johnny-five').length > 0 && require.resolve('raspi-io').length > 0 ) {
if (config.motion.enabled == "pin" && require.resolve('johnny-five').length > 0 && require.resolve('raspi-io').length > 0 ) {

// Configure johnny-five
var five = require('johnny-five');
Expand Down Expand Up @@ -35,6 +40,61 @@ if (config.motion.enabled == true && require.resolve('johnny-five').length > 0 &
console.log("!e:","motionend");
});
});
} else if ( config.motion.enabled == true){
} else if ( config.motion.enabled === "pin"){
console.error("!E:","Motion Dependencies are missing! Therefore despite my best efforts I'll have to disable motion, Dave. This is most embarrassing for us both.")
}
} else if ( config.motion.enabled == "external"){
// check to see if the external motion event folder exists
fs.access(detectionDir, function(err) {
// if not
if (err && err.code === 'ENOENT') {
// create it
fs.mkdir(detectionDir);
console.debug('created motion directory', detectionDir);
}
else{
// make sure the directory is empty
rmDir(detectionDir,false);
}
// change detector function
// watch for a file to appear in the folder
fs.watch(detectionDir, (eventType, filename) => {
if (filename) {
// remove the file
fs.unlink(path.join(detectionDir,filename), function(error) {
// consume the enonet error
if(error == null){
//console.debug('motion detected from external source');
// if the start motion file
if(filename === detectionFile) {
// signal motion started
console.log("!s:","motionstart");
}
else {
// signal motion ended
console.log("!e:","motionend");
}
}
});
} else {
console.log('filename not provided');
}
});
});
}
var rmDir = function(dirPath, removeSelf) {
if (removeSelf === undefined)
removeSelf = true;
try { var files = fs.readdirSync(dirPath); }
catch(e) { return; }
if (files.length > 0)
for (var i = 0; i < files.length; i++) {
var filePath = dirPath + '/' + files[i];
if (fs.statSync(filePath).isFile())
fs.unlinkSync(filePath);
else
rmDir(filePath);
}
if (removeSelf)
fs.rmdirSync(dirPath);
};

44 changes: 44 additions & 0 deletions motion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
h1>Motion Detection configuration</h1>

<p>Smart-Mirror supports 2 approaches to motion detection

<ul>
<li>Hardware PIR sensor wired to Raspberry PI</li>
<li>Externally provided sensor of some kind
<p>this could be via a webcam or some other mechanism</p>
</li>
</ul>
<p>to enable motion detection, you must first enable the autosleep function.
othewise smart-mirror will never go to sleep at all

<p>once autosleep is enabled (pick TV or Monitor, depending on what kind of display you are using for smart-mirror)
then select either PIN or External mode in the Motion, Mode dropdown

<p>once you select external motion, smart-mirror will start looking for the files that indicate motion is occurring

<p>the <b>external-motion</b> script in the scripts folder can be used to provide the proper file names and location to support the built in detection code.

<p>an Example of an external motion detection provider is the github Motion project at <a href="https://motion-project.github.io/">https://motion-project.github.io/</a>

as part of the Motion project configuration, you can have it signal
<ul>
<li>motion start</li>
<li>how long to wait from last motion detection (gap)</li>
<li>motion ended</li>
</ul>

<p>to enable motion detection, modify the <b>/etc/motion/motion.conf</b> file, and set these lines
<ul>
<li>on_event_start /home/pi/smart-mirror/scripts/external-motion started</li>
<li>on_event_end /home/pi/smart-mirror/scripts/external-motion ended</li>
<li>event_gap n
<p>where n is the number of seconds since the last motion was detected until signalling motion end
<p> for example, if you had event_gap 15, this would mean wait 15 seconds since the last motion, and then signal event_ended.
<p>but if there was motion again in 14 seconds then it would wait ANOTHER 15 seconds from then, before signaling event end..
this helps detect continupus motion..
<p>see the help from the Motion tool for more configuration options.

</li>
<p>Note: smart-mirror currently does not have any processing when motion ended is detected, but this event is reported.
this could change in the future
</ul>
11 changes: 8 additions & 3 deletions plugins/autosleep/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@
"title": "Motion Settings",
"properties": {
"enabled": {
"type": "boolean",
"title": "Check to Enable Motion Detection",
"default":false
"type": "string",
"title": "Select type Motion Detection",
"default":false,
"enum":[
"false",
"pin",
"external"
]
},
"pin": {
"type": "string",
Expand Down

0 comments on commit 99cfc7f

Please sign in to comment.