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.

    PIR-Sensor - put your mirror to sleep if not used

    Scheduled Pinned Locked Moved System
    utilitygpiomotion-sensorpower-saving
    56 Posts 32 Posters 114.0k Views 47 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.
    • IngmarSwartI Offline
      IngmarSwart
      last edited by

      Using motion detection to switch your mirror on and off is very appealing. PIRs are a good option and Paviro’s module does a wonderful job. However, for an aesthetic point of view, I would like to mount the motion sensor behind the mirror, i.e. in such a way you can’t see the sensor. Since glass absorbs the light the PIR is sensitive to, PIRs (or at least the ones I tested, played with sensitivities as well) are not the best option. I played around a bit and found an alternative solution based on the Picamera, OpenCV and Paviro’s MMM-PIR module.

      Downsides include having to run a python program next to the Magic Mirror. On a RPi3 it this is not a problem and is in my view therefore a rather minor drawback. If you run your MM on a RPi2, this may be an issue. Getting this to work also requires a non-trivial installation of OpenCV. However, thanks to the fantastic people over at PyImageSearch, detailed instructions are available. Upsides of this solution are: motion detection using a camera from behind the glass and the possibility to upload a photo taken by the camera each time it detects motion to your dropbox account.

      Steps to follow:

      • Disable the red LED on the Picamera by adding disable_camera_led = 1 to \boot\config.txt
        (you don’t want to see red LED of the camera when looking at your mirror)
      • Install a full version of OpenCV on the RPi. Detailed instructions can be found here
      • Follow the two part tutorial on writing a python based code for motion detection: Part I and Part II
      • Modify the python program found on the second page to generate a 3.3 V signal on a specified pin when the camera detects motion. This pin (pin 4 below) is then connected by a wire to the pin you specified in the MMM-PIR section of the MM config.js file. My modifications to the python code are:

      On line 14 of the python code, add:

      import RPi.GPIO as GPIO
      GPIO.setmode(GPIO.BCM)
      OPENCV_pin = 4 # specify whatever pin you want to generate the 3.3 V at when motion is detected.
      GPIO.setup(OPENCV_pin, GPIO.OUT)
      

      On line 26 add:

      UnoccupiedCounter = 0
      NumUnoccFramesSwitchOff = conf["fps"] * conf["time_to_switch_off"]
      
      

      On line 108 insert:

      UnoccupiedCounter = 0
      if GPIO.input(OPENCV_pin) == 0 # check if openCV pin is high or low. If low, turn high
               GPIO.output(OPENCV_pin,1)
               print "Switched Mirror ON"
      

      On line 137 insert:

      UnoccupiedCounter =  UnoccupiedCounter + 1
      if UnoccupiedCounter = numUnoccFramesSwitchOff and GPIO.input(OPENCV_pin)==1:
               GPIO.output(OPENCV_pin, 0)
               print "Switched Mirror OFF"
      
      • Add "time_to_switch_off": 30 to the conf.json file. Don’t forget to add the comma behind the previous entry.
      • Optional but recommended to get a clean exit:
        Insert the whole for loop in a try statement.
        Between line 53 and 54, add
      try: 
      

      Don’t forget to indent all the code that comes next. Add the very bottom, add:

      except KeyboardInterrupt:
            print "Stopped camera surveillance" # exit the program when you press CNRL +C
      
      except: 
            print "Other error or exception occurred!" # catch all other errors
      
      finally:
           GPIO.cleanup() # this ensures a clean exit.
      

      I hope the above is of use to some of you.

      D 1 Reply Last reply Reply Quote 6
      • D Offline
        DirkS
        last edited by

        HI @IngmarSwart,

        great hack!! Did you use the original Camera behind the glass? Noir or without the IR Filter?

        I’m using a PI2, can you specify why this should be an issue?

        Best regards
        Dirk

        IngmarSwartI 1 Reply Last reply Reply Quote 0
        • IngmarSwartI Offline
          IngmarSwart @DirkS
          last edited by IngmarSwart

          Hi @DirkS,

          I use a standard first generation RPi camera board. In other words, this solution works perfectly fine with IR filter on the camera.

          The reason I think there might be issues with the RPi2: you should realize that this solution requires the RPi to analyze 10 images per second (give or take a few, you can specify the frame rate) using OpenCV routines. This causes a significant bump in processor load (approximately 33% on the RPi3 on top of everything else, if I’m not mistaken). I simply don’t know if the processor of the RPi2 is fast enough to do the image analysis, run the MM and the operating system simultaneously. The only way to know for sure is to test. I can’t help you with that as I don’t own a RPi2. Should the RPi2 struggle, perhaps you can get away with analyzing fewer frames (5 or maybe even 2). I encourage you to try and report back!

          Best wishes,
          Ingmar

          1 Reply Last reply Reply Quote 0
          • S Offline
            samydp
            last edited by

            Hello,
            my module is not working, I have the pir sensor connected to Pin 7 and when you start the screen stays white and nothing happens when I take the pir of the config it goes again.

            zamZ 1 Reply Last reply Reply Quote 0
            • zamZ Offline
              zam @samydp
              last edited by

              @samydp Maybe you can find the answer here :After rebooting, run MM with pm2 and shows “White Screen”, I checked the pm2 error log shows something like “Expected 49, got 48” as before,
              Run “npm rebuild --runtime=electron --target=1.3.4 --disturl=https://atom.io/download/atom-shell --abi=49” in the MMM-PIR-Sensor folder,
              Run “pm2 restart mm” and it works.”

              1 Reply Last reply Reply Quote 0
              • S Offline
                samydp
                last edited by

                it is not’ve tried as it stands anyway thanks for your help

                1 Reply Last reply Reply Quote 0
                • D Offline
                  Defibrillat0r @IngmarSwart
                  last edited by

                  @IngmarSwart Thats a really nice solution. Did you use the virtual environment from the tutorials? If you did so, how did you wrap them together? Is it even possible? Thanks

                  1 Reply Last reply Reply Quote 0
                  • B Offline
                    bibi
                    last edited by

                    Since IngmarSwart didn’t connect since few months, maybe someone can just explain me the reason why a PIR sensor is still needed in his config? i don’t get the role of the PIR since there is already a cam to detect the move…? if anyone… thx guys!

                    1 Reply Last reply Reply Quote 0
                    • IngmarSwartI Offline
                      IngmarSwart
                      last edited by

                      @Defibrillat0r: Thanks. Yes, I use the virtual environment. That being said, I don’t think it is stricktly necessary. In our configuration, the RPi is dedicated to running the MM, so the likelyhood that you need different versions of packages is very small. If you do use the virtual environment, you just have to make sure all the packages listed at the top of the python file are installed for the virtual environment you are working on. Other than that, no special tricks are needed.

                      @bibi: In my solution a PIR sensor is not needed. You only use @paviro’s module to interface the python code with the MM.

                      B 1 Reply Last reply Reply Quote 0
                      • B Offline
                        bibi @IngmarSwart
                        last edited by

                        @IngmarSwart ohh ok now it’s clear! thx alot for your reply.
                        your hack is working perfectly so far?

                        1 Reply Last reply Reply Quote 0
                        • IngmarSwartI Offline
                          IngmarSwart
                          last edited by

                          @bibi: I’ve only tested my solution for a couple of days, so I can’t say much about the long term stability. The reason for this is simply that I was in the process of designing a (nearly) frameless mirror the last couple of months. The components have been ordered and I hope to assemble everything in the coming month. It might take a bit longer, as I have quite a few business trips lined up for November.

                          B 1 Reply Last reply Reply Quote 0
                          • B Offline
                            bibi @IngmarSwart
                            last edited by

                            @IngmarSwart I see i am still getting everything but in meantime i am working on the software side. You solution is nice and i will try to implement it i think… with my noob skills :)))

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

                              Hi,

                              i had big problems by switching off my screen with the PIR or another module with camera. The problem was not the module itself it is a firmware problem of the PI. I could see the same problem outside of MagicMirror as well by trying different things.

                              Reported also here:
                              https://github.com/raspberrypi/linux/issues/487

                              The symptom is that after a while of switching on and off the screen it remains off.

                              Dmesg reports the problem:

                              Nov  1 08:22:00 MagicMirror kernel: [86411.138649] raspberrypi-firmware soc:firmware: Request 0x00048003 returned status 0x80000001
                              Nov  1 08:22:00 MagicMirror kernel: [86411.138665] bcm2708_fb soc:fb: Failed to allocate GPU framebuffer (-22)
                              Nov  1 08:22:00 MagicMirror kernel: [86411.138670] detected fb_set_par error, error code: -22
                              Nov  1 08:22:00 MagicMirror kernel: [86411.139138] raspberrypi-firmware soc:firmware: Request 0x00048003 returned status 0x80000001
                              Nov  1 08:22:00 MagicMirror kernel: [86411.139148] bcm2708_fb soc:fb: Failed to allocate GPU framebuffer (-22)
                              Nov  1 08:22:00 MagicMirror kernel: [86411.139152] detected fb_set_par error, error code: -22
                              Nov  1 08:22:00 MagicMirror kernel: [86411.140642] raspberrypi-firmware soc:firmware: Request 0x00048003 returned status 0x80000001
                              Nov  1 08:22:00 MagicMirror kernel: [86411.140654] bcm2708_fb soc:fb: Failed to allocate GPU framebuffer (-22)
                              Nov  1 08:22:00 MagicMirror kernel: [86411.140659] detected fb_set_par error, error code: -22
                              Nov  1 08:22:00 MagicMirror kernel: [86411.140777] raspberrypi-firmware soc:firmware: Request 0x00048003 returned status 0x80000001
                              Nov  1 08:22:00 MagicMirror kernel: [86411.140785] bcm2708_fb soc:fb: Failed to allocate GPU framebuffer (-22)
                              Nov  1 08:22:00 MagicMirror kernel: [86411.140789] detected fb_set_par error, error code: -22
                              Nov  1 08:22:00 MagicMirror kernel: [86411.140856] raspberrypi-firmware soc:firmware: Request 0x00048003 returned status 0x80000001
                              Nov  1 08:22:00 MagicMirror kernel: [86411.140863] bcm2708_fb soc:fb: Failed to allocate GPU framebuffer (-22)
                              Nov  1 08:22:00 MagicMirror kernel: [86411.140868] bcm2708_fb_pan_display(0,0) returns=-22
                              Nov  1 08:22:00 MagicMirror systemd[1]: Started Getty on tty6.
                              Nov  1 08:22:00 MagicMirror kernel: [86411.256398] raspberrypi-firmware soc:firmware: Request 0x00048003 returned status 0x80000001
                              Nov  1 08:22:00 MagicMirror kernel: [86411.256413] bcm2708_fb soc:fb: Failed to allocate GPU framebuffer (-22)
                              Nov  1 08:22:00 MagicMirror kernel: [86411.256418] detected fb_set_par error, error code: -22
                              Nov  1 08:22:00 MagicMirror kernel: [86411.256758] raspberrypi-firmware soc:firmware: Request 0x00048003 returned status 0x80000001
                              Nov  1 08:22:00 MagicMirror kernel: [86411.256770] bcm2708_fb soc:fb: Failed to allocate GPU framebuffer (-22)
                              Nov  1 08:22:00 MagicMirror kernel: [86411.256775] bcm2708_fb_pan_display(0,0) returns=-22
                              Nov  1 08:22:00 MagicMirror kernel: [86411.296428] raspberrypi-firmware soc:firmware: Request 0x00040002 returned status 0x80000001
                              Nov  1 08:22:00 MagicMirror kernel: [86411.296455] bcm2708_fb soc:fb: bcm2708_fb_blank(0) failed: -22
                              

                              Switching off the screen with

                              /opt/vc/bin/tvservice -o

                              and on with

                              /opt/vc/bin/tvservice --preferred && sudo chvt 6 && sudo chat 7

                              does not work reliable! The git issue tells you more about.

                              The workaround is a not yet described solution inside the config.txt

                              https://github.com/raspberrypi/documentation/pull/245/files

                              the option “hdmi_blanking=1” is your friend in this case! :D

                              Now i’m switching off the screen with

                              vcgencmd display_power 0

                              and on with

                              vcgencmd display_power 1

                              And it is working now like a charm! Did started to use it one day before this report and it is still working without any issue :)

                              Honestly speaking i don’t use any MM module for switching off anymore. But maybe my discovered solution will help any other and might be a way to change the module as well.

                              If you have trouble with on/off as well please check the output of dmesg. If you have the frambuffer problem you can fix it.

                              My personal solution is to have the PIR state at FHEM and from FHEM i’m using a script to switch the screen. But that’s a special case and for the most people here the usage of a MM Module will be the better way.

                              regards
                              Dirk

                              M 1 Reply Last reply Reply Quote 0
                              • N Offline
                                nemesismega @paviro
                                last edited by

                                @paviro said in PIR-Sensor - put your mirror to sleep if not used:

                                MMM-PIR-Sensor will monitor a connected PIR-sensor and putt your mirror to sleep if no one uses it either by disabling HDMI output or by turning of a relay.

                                Developer note

                                If you are a developer and want to pause your module while no one uses it (if it is processor intense), you can listen to the USER_PRESENCE broadcast. It will return true or false as its payload.

                                will vga / dvi monitor work with this module?

                                [card:paviro/MMM-PIR-Sensor]

                                S 1 Reply Last reply Reply Quote 0
                                • T Offline
                                  Them Russians
                                  last edited by

                                  This post is deleted!
                                  1 Reply Last reply Reply Quote 0
                                  • foxF Offline
                                    fox
                                    last edited by

                                    i have bought a sensor.
                                    This is not a troubleshooting post. I want only know how to connect it.
                                    Please add this to the description.
                                    Three wires from the sensor and to which raspi port?

                                    Issues With MMM-WatchDog known?

                                    https://forum.magicmirror.builders/post/29827

                                    Useful for PIR-Sensor: https://forum.magicmirror.builders/post/21299

                                    T 1 Reply Last reply Reply Quote 0
                                    • T Offline
                                      Them Russians @fox
                                      last edited by

                                      @fox pop the plastic dome off and you’ll see the labeling. I believe the middle pin is the digital output and the ground and 5V power are on the outside.

                                      foxF 1 Reply Last reply Reply Quote 0
                                      • foxF Offline
                                        fox @Them Russians
                                        last edited by

                                        @Them-Russians said in PIR-Sensor - put your mirror to sleep if not used:

                                        @fox pop the plastic dome off and you’ll see the labeling. I believe the middle pin is the digital output and the ground and 5V power are on the outside.

                                        The sensor is labeled. But on which pin on the raspi? Have i missed something in the module how-to?

                                        Issues With MMM-WatchDog known?

                                        https://forum.magicmirror.builders/post/29827

                                        Useful for PIR-Sensor: https://forum.magicmirror.builders/post/21299

                                        T 1 Reply Last reply Reply Quote 0
                                        • T Offline
                                          Them Russians @fox
                                          last edited by

                                          @fox You define the pin in the config.js file.

                                          Make sure you sue the GPIO pin number, and not the generic pin number.

                                          For instance, in the pin diagram the pin 1 down from the top left is GPIO pin 2, but regular pin 3. In the config file, you would define it a SensorPIN = 2.

                                          foxF 1 Reply Last reply Reply Quote 0
                                          • foxF Offline
                                            fox @Them Russians
                                            last edited by fox

                                            @Them-Russians said in PIR-Sensor - put your mirror to sleep if not used:

                                            @fox You define the pin in the config.js file.

                                            Make sure you sue the GPIO pin number, and not the generic pin number.

                                            For instance, in the pin diagram the pin 1 down from the top left is GPIO pin 2, but regular pin 3. In the config file, you would define it a SensorPIN = 2.

                                            Thank you very much. I understood.
                                            Does someone control this with a timeout? Or when switches the script the HDMI Port off? Perhaps some want x minutes to be shown?

                                            Edit. I am a noob. I just have to adjust the delay knob on the PIR-Sensor PCB :)

                                            Issues With MMM-WatchDog known?

                                            https://forum.magicmirror.builders/post/29827

                                            Useful for PIR-Sensor: https://forum.magicmirror.builders/post/21299

                                            1 Reply Last reply Reply Quote 0

                                            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                                            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                                            With your input, this post could be even better 💗

                                            Register Login
                                            • 1
                                            • 2
                                            • 3
                                            • 1 / 3
                                            • 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