I recently got the MMM-PIR-Sensor up and going and it’s working flawlessly. I wanted to give a little information as I had to parse this from different sources and some of the info is a bit dated.
I am relatively new to pi in general so definitely point it out if something isn’t right.
Below is a step by step for MMM-PIR-Sensor by paviro.
Navigate to your modules folder at cd ~/MagicMirror/modules
Execute git clone https://github.com/paviro/MMM-PIR-Sensor.git
Navigate to cd ~/MagicMirror/modules/MMM-PIR-Sensor
This is one area I got held up
The guide states to perform an npm install
where a dated version(2.25) of wiring-pi would be installed.
When issuing the command gpio -v
, I would get an error message “Unable to determine hardware version. I see: Hardware : BCM2835 - expecting BCM2708 or BCM2709. Please report this to project@drogon.net”.
Instead use npm install wiring-pi
inside your MMM-PIR-Sensor folder. This will install the updated version(2.44) which you can verify with gpio -v
.
Then issue command sudo usermod -a -G gpio pi
Then sudo chmod u+s /opt/vc/bin/tvservice && sudo chmod u+s /bin/chvt
The guide says to reboot here but after that, it still wasn’t working for me. Then I stumbled on some excellent advice from the forums here from @JamesMM with @strawberry 3.141 contributing.
Their focus was on the node_helper.js within the module. These are their edits.
First, rename the current file. mv node_helper.js node_helper_backup.js
Now, create a new file called node_helper.js using sudo nano node_helper.js
Copy and paste this code
'use strict';
/* Magic Mirror
* Module: MMM-PIR-Sensor
*
* By Paul-Vincent Roll http://paulvincentroll.com
* MIT Licensed.
*/
const NodeHelper = require('node_helper');
const Gpio = require('onoff').Gpio;
const exec = require('child_process').exec;
module.exports = NodeHelper.create({
start: function()
{
this.started = false;
},
activateMonitor: function()
{
if (this.config.relayPIN != false)
{
this.relay.writeSync(this.config.relayOnState);
}
else if (this.config.relayPIN == false)
{
// Check if hdmi output is already on
exec("/opt/vc/bin/tvservice -s").stdout.on('data', function(data) {
if (data.indexOf("0x120002") !== -1)
exec("/opt/vc/bin/tvservice --preferred && chvt 6 && chvt 7", null);
});
}
},
deactivateMonitor: function()
{
if (this.config.relayPIN != false)
{
this.relay.writeSync(this.config.relayOffState);
}
else if (this.config.relayPIN == false)
{
exec("/opt/vc/bin/tvservice -o", null);
}
},
// Subclass socketNotificationReceived received.
socketNotificationReceived: function(notification, payload)
{
if (notification === 'CONFIG' && this.started == false)
{
const self = this;
this.config = payload;
self.timer = null;
self.onState = 0;
// Setup value which represent on and off
const valueOn = this.config.invertSensorValue ? 0 : 1;
const valueOff = this.config.invertSensorValue ? 1 : 0;
//Log.info('PIR: ' + this.name);
//Setup pins
this.pir = new Gpio(this.config.sensorPIN, 'in', 'both');
if (this.config.relayPIN)
{
this.relay = new Gpio(this.config.relayPIN, 'out');
this.relay.writeSync(this.config.relayOnState);
exec("/opt/vc/bin/tvservice --preferred && sudo chvt 6 && sudo chvt 7", null);
}
//Detected movement
this.pir.watch(function(err, value) {
if (value == 1)
{
clearTimeout(self.timer);
if (self.onState != 1)
{
self.sendSocketNotification("USER_PRESENCE", true);
if (self.config.powerSaving)
{
self.activateMonitor();
self.onState = 1;
}
}
}
else if (value == 0)
{
if (self.onState != 0)
{
self.timer = setTimeout(function(){
self.sendSocketNotification("USER_PRESENCE", false);
if (self.config.powerSaving)
{
self.deactivateMonitor();
self.onState = 0;
}
}, self.config.offDelay);
}
}
});
this.started = true;
}
else if (notification === 'SCREEN_WAKEUP')
{
this.activateMonitor();
}
}
});
This creates a new config called offDelay and it is in configurable in milliseconds.
Reboot your pi.
Then add the modules configuration to MM’s config.js. I used this but obviously adjust it to your own needs.
{
module: 'MMM-PIR-Sensor',
config: {
sensorPIN: 22,
powerSaving: true,
offDelay: 30000,
}
}
The offDelay of 30000 is 30 seconds.