MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    MMM-PIR-Sensor Guide with Edits and Updates

    Scheduled Pinned Locked Moved Development
    20 Posts 15 Posters 15.6k Views 17 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S Offline
      StacheEnthusiast
      last edited by StacheEnthusiast

      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.

      Mykle1M 1 Reply Last reply Reply Quote 7
      • Mykle1M Offline
        Mykle1 Project Sponsor Module Developer @StacheEnthusiast
        last edited by

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

        Create a working config
        How to add modules

        S 1 Reply Last reply Reply Quote 3
        • S Offline
          StacheEnthusiast @Mykle1
          last edited by

          @Mykle1

          Thanks!

          1 Reply Last reply Reply Quote 0
          • D Offline
            DavA
            last edited by

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

            G F 2 Replies Last reply Reply Quote 1
            • S Offline
              SvenT
              last edited by

              This post is deleted!
              1 Reply Last reply Reply Quote 0
              • G Offline
                greenwaydev @DavA
                last edited by

                This post is deleted!
                1 Reply Last reply Reply Quote 0
                • F Offline
                  FruityBebbles @DavA
                  last edited by FruityBebbles

                  @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…

                  1 Reply Last reply Reply Quote 0
                  • M Offline
                    msherwood
                    last edited by

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

                    1 Reply Last reply Reply Quote 0
                    • A Offline
                      ardentaardvark
                      last edited by

                      @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.)

                      1 Reply Last reply Reply Quote 0
                      • D Offline
                        dubalda
                        last edited by

                        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

                        FoziF 1 Reply Last reply Reply Quote 0
                        • 1
                        • 2
                        • 1 / 2
                        • First post
                          Last post
                        Enjoying MagicMirror? Please consider a donation!
                        MagicMirror created by Michael Teeuw.
                        Forum managed by Sam, technical setup by Karsten.
                        This forum is using NodeBB as its core | Contributors
                        Contact | Privacy Policy