MagicMirror² v2.12.0 is available! For more information about this release, check out this topic.

MMM-PIR-Sensor Guide with Edits and Updates



  • 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.


  • Project Sponsor Module Developer

    @StacheEnthusiast

    I like people who write guides! I don’t use a PIR sensor but I do appreciate the time and effort you put in on everyone’s behalf.



  • @Mykle1

    Thanks!



  • I followed your guide but i still get version 2.25 when i use npm install wiring-pi.
    Any ideas why?



  • This post is deleted!


  • This post is deleted!


  • @StacheEnthusiast @DavA Hey, I just decided to try and install MMM-PIR-Sensor but am running into the same issue.
    I was wondering if you ever did get this working as the directions have stated…



  • shit, this killed my MM build…it’s all blank…crap



  • @msherwood Hey FYI I just got my lastest build (from scratch yesterday) working with MMM-PIR-Sensor:

    I also found that npm install wiring-pi installed the old (2.25) version for me. I found a newer library:

    npm install wiring-pi-2019

    which installs version 2.5 (this one works on my pi4!).

    I also found that the location of the tvservice binary has changed, so I just used the following bash commands:

    sudo chmod u+s `which tvservice`
    sudo chmod u+s `which chvt`
    

    It then worked for me (Note, I did not modify the node_helper.js script at all.)



  • Hi all, unfortunately I’m not too familiar with coding but had this module failed to update today having followed the steps above which worked fine with the previous version.

    Has anyone else experienced this?

    Thanks


Log in to reply