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.

    Motion Detection with RaspiCam, Non-Module version

    Scheduled Pinned Locked Moved Tutorials
    47 Posts 13 Posters 51.4k 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.
    • bheplerB Offline
      bhepler Module Developer
      last edited by

      First, I want thank @alexyak for his motioncontrol module. Unfortunately for me, it doesn’t work on my Mirror due to new security controls put in place by the Node.js team. Apparently it’s not a wise move to let client-side Javascript access the webcam attached to your server. Who knew?

      In the end, I ended up seeking another solution. The Facial Recognition module didn’t work due to the reduction in light coming through the mirror itself. But I do get an image. I started browsing sites of people who have turned their Raspi into a security system. It turns out, someone ported the Motion framework to Raspberry Pi and called it MMAL-Motion.

      After a lot of reading, I found a Wiki for MMAL Motion. Following the steps for Jessie, I did the following:

      • Change to your home directory. cd ~
      • Install the library dependencies (just copy & paste into the terminal. I wouldn’t want to do this by hand)
        sudo apt-get install -y libjpeg-dev libavformat56 libavformat-dev libavcodec56 libavcodec-dev libavutil54 libavutil-dev libc6-dev zlib1g-dev libmysqlclient18 libmysqlclient-dev libpq5 libpq-dev
      • Download the precompiled binary archive file:
        wget https://www.dropbox.com/s/6ruqgv1h65zufr6/motion-mmal-lowflyerUK-20151114.tar.gz
      • Uncompress the archive file
        tar -zxvf motion-mmal-lowflyerUK-20151114.tar.gz

      This will put a configuration file called motion-mmalcam-both.conf and a folder called motion in your home folder. At this step you can test it if you like, but for my purposes I needed to make a few changes tot the config file. So, make a copy and edit the copy.
      cp motion-mmalcam-both.conf motion.conf
      nano motion.conf
      I played around with the configuration quite a bit until I found one I liked. But for our purposes (turning the display on when someone comes near) you only have to make a few changes.
      framerate 4 - run the detection video at 4 frames per second
      threshold 2500 - increase the # of pixels to trigger the screen. We want them to get close to the mirror
      minimum_motion_frames 2 - Motion must be detected in 2 consecutive frames (at 4 FPS)
      event_gap 60 - This is important. This is how long in seconds after no motion the screen will turn off.
      output_pictures off - Do not save images
      ffmpeg_output_movies off - Do not record video
      stream_port 0 - Turn off remote viewing
      webcontrol_port 0 - Turn off HTTP control of camera
      on_event_start vcgencmd display_power 1 - Important! This is the command to turn on the screen
      on_event_end vcgencmd display_power 0 - Important! This is the command to turn off the screen

      Save your motion.conf file and now you can test it out by entering ./motion -c motion.conf. If you stay very still for a minute, the screen should turn off. Move close to the mirror, and everything should come back after a couple seconds.

      B N 2 Replies Last reply Reply Quote 3
      • B Offline
        bibi @bhepler
        last edited by

        hi @bhepler ! thx for sharing your findings!i will try this settings later this week. before to get into it, could you please let me know if it will also work without a RaspiCam?? usb camera is ok? where did you put/hide your own cam? thx :)

        bheplerB 1 Reply Last reply Reply Quote 0
        • bheplerB Offline
          bhepler Module Developer @bibi
          last edited by

          @bibi I haven’t tried it with a USB cam, but it should work. In the config file, there is a switch to throw if you’re using a USB cam (and different switches to throw if you’re using a network cam - which raises interesting possibilities).

          My camera is mounted above the monitor & behind the one-way acrylic. My monitor is smaller than my acrylic by about 2" on a side. This is good in that it’s a very clean installation and you don’t see a camera when you look at the mirror. It’s bad in that it cuts down on the light reaching the camera, preventing the facial recognition from working.

          1 Reply Last reply Reply Quote 1
          • bheplerB Offline
            bhepler Module Developer
            last edited by bhepler

            Hrm. New problem: After a few hours, the screen goes white. This obviously will not do. Back to the drawing board.

            I found this in the motion log:

            [1] [NTC] [ALL] [Nov 15 20:25:02] motion_loop: End of event 74
            [1] [NTC] [ALL] [Nov 15 20:25:04] motion_loop: fps: 3.999920 idle 26.67% over 0.00%
            [1] [NTC] [ALL] [Nov 15 20:25:09] motion_loop: fps: 3.998465 idle 24.89% over 0.00%
            [1] [ERR] [ENC] [Nov 15 20:25:10] ffmpeg_put_frame: Error while writing video frame:
            [1] [NTC] [ALL] [Nov 15 20:25:10] motion_loop: Thread exiting
            [1] [INF] [ALL] [Nov 15 20:25:10] motion_cleanup: Calling vid_close() from motion_cleanup
            [1] [INF] [VID] [Nov 15 20:25:10] vid_close: calling video source cleanup
            [1] [ALR] [VID] [Nov 15 20:25:10] MMAL Camera cleanup
            [0] [NTC] [ALL] [Nov 15 20:25:11] main: Threads finished
            [0] [NTC] [ALL] [Nov 15 20:25:12] main: Motion terminating
            [0] [NTC] [ALL] [Nov 15 20:25:12] motion_remove_pid: Removed process id file (pid file).
            [0] [NTC] [ALL] [Nov 15 20:25:12] motion_remove_pid: Closing logfile (/tmp/motion.log).
            
            1 Reply Last reply Reply Quote 0
            • bheplerB Offline
              bhepler Module Developer
              last edited by bhepler

              I believe I have it cracked. The white screen was due to MMAL Motion crashing whilst writing a frame to disk. When Motion crashes, it apparently jacks up the video output.

              My current solution is to use PM2 to monitor the motion process and restart it when necessary. I’m testing now. If it works for a few more days I’ll report back.

              Adding motion to PM2: pm2 start motion. Be sure that the motion is not configured to run as a daemon (first option in config file).

              lolobyteL 1 Reply Last reply Reply Quote 1
              • lolobyteL Offline
                lolobyte @bhepler
                last edited by lolobyte

                @bhepler

                Hi, i do the same at first and restor my sw installation.
                But after the same procedure of sw installation this error was again on top.

                I resolve, by my sw, the reason for that was: motion must start after pm2 startin mm.

                My mm start’s automaticly after rebooting.
                My motion also. => collision of SW.

                By doing motion in an cron job with delay time al was fun an running as iwant.

                in my case i do following:
                don’t foget to install screen.

                sudo apt-get intsall screen

                My motion is running in a screen session.
                then the cronjob

                crontab -e

                and set motion with following parameters:

                @reboot sleep 150; screen -S motion -dmS sudo motion -c /home/pi/motion/motion-mmalcam.conf

                i hope this is helpfull for you and other persons here.

                B 1 Reply Last reply Reply Quote 1
                • bheplerB Offline
                  bhepler Module Developer
                  last edited by

                  Update: Having PM2 monitor the motion process seems to work quite well. However, I have now discovered that there is a memory leak somewhere in all of this. Using the MMM-SystemStats module, I can watch free memory trickle down throughout the day.

                  I’ve configured the Pi to reboot early each morning. That’s not an ideal situation, but it seems to have solved the problem. Good enough for me! And more importantly, good enough for the gift recipient.

                  lolobyteL 1 Reply Last reply Reply Quote 0
                  • lolobyteL Offline
                    lolobyte @bhepler
                    last edited by

                    @bhepler

                    have u increase the swapfile?

                    bheplerB 1 Reply Last reply Reply Quote 0
                    • bheplerB Offline
                      bhepler Module Developer @lolobyte
                      last edited by

                      @lolobyte Negative, I have not. What would you recommend?

                      lolobyteL 1 Reply Last reply Reply Quote 0
                      • lolobyteL Offline
                        lolobyte @bhepler
                        last edited by lolobyte

                        @bhepler
                        in general it’s not bad for a raspi3 to increase the swapfile from 100MB to a higher level.

                        at first check your status:
                        sudo service dphys-swapfile status
                        U see something like this:

                        ● dphys-swapfile.service - LSB: Autogenerate and use a swap file
                        Loaded: loaded (/etc/init.d/dphys-swapfile)
                        Active: active (exited) since Mi 2016-11-30 13:17:14 CET; 6h ago
                        Process: 613 ExecStart=/etc/init.d/dphys-swapfile start (code=exited, status=0 /SUCCESS)

                        Nov 30 13:17:13 raspi3 systemd[1]: Starting LSB: Autogenerate and use a swa…
                        Nov 30 13:17:14 raspi3 dphys-swapfile[613]: Starting dphys-swapfile swapfile…
                        Nov 30 13:17:14 raspi3 dphys-swapfile[613]: want /var/swap=1024MByte, checki…t
                        Nov 30 13:17:14 raspi3 dphys-swapfile[613]: done.
                        Nov 30 13:17:14 raspi3 systemd[1]: Started LSB: Autogenerate and use a swap…e.
                        Hint: Some lines were ellipsized, use -l to show in full.

                        Maybe u have 100MB instead of 1024 like in my case.

                        If u want to change this value the edit your

                        sudo nano /etc/dphys-swapfile

                        change to this value and save your file

                        CONF_SWAPSIZE=1024

                        the do a

                        sudo dphys-swapfile setup

                        and activate your new swap level:

                        sudo dphys-swapfile swapon

                        thats all. Now your System can handle wit more than his physical RAM.

                        In My case i do in raspi-config a new

                        “Expand File system”

                        to reorganize the file system an reboot after that my raspi.
                        If you are a owner of a raspi 2 it’s more common to change the swap file to max 512MB.

                        please inform me what’s your experience after this change

                        you can also check very niche your tasks mem, swap and other things in a ssh session on your PC with htop

                        sudo apt-get install htop

                        have a nice try

                        1 Reply Last reply Reply Quote 0
                        • B Offline
                          Binog @lolobyte
                          last edited by

                          @lolobyte
                          Can U specify what this command does,. Am not sure if it isn’t doubling the motion command:

                          @reboot sleep 150; screen -S motion -dmS sudo motion
                          (ignored the -c path part, cause renamed the config to motion.conf, so not needed)

                          why is there two time motion - in it?

                          By the way it isn’t working on my mirror

                          1 Reply Last reply Reply Quote 0
                          • KimzerK Offline
                            Kimzer
                            last edited by

                            I am completely unable to make this work.
                            The camera wont take any snapshots, maybe just a config error. But can someone confirm that this still works or show me something that works? Dont wanna go out and buy a pir sensor if i can make use of the raspicam. :)

                            bheplerB 1 Reply Last reply Reply Quote 0
                            • bheplerB Offline
                              bhepler Module Developer @Kimzer
                              last edited by

                              @Kimzer - If the camera won’t even take a snapshot, then none of this is going to work. Have you tried the various tutorials about getting the PiCam to work? The ones that are at the basic level of “I enter this command and it takes a picture right then and there and I can download it”?

                              Start with the official Getting Started Guide. Once you can get images from the camera to your Pi, come back.

                              If you can get the camera to produce images but have trouble getting Motion to work, follow my tutorial and the part where it says “at this point you can test it but…”, stop there. Change the parameter in the motion.conf file named output_pictures to on and change on_event_start to echo Motion Detected!. That should put a bunch of snapshots on your memory card. Test Motion by entering ./motion -c motion.conf and you should see motion writing messages to the screen.

                              1 Reply Last reply Reply Quote 0
                              • KimzerK Offline
                                Kimzer
                                last edited by

                                Ah my bad, poorly formulated on my end here.

                                I am able to take snapshots, and i have gotten the camera to work just fine itself. It is the motion part i am struggling with.

                                But i will have to test some more.

                                1 Reply Last reply Reply Quote 0
                                • F Offline
                                  Freebond
                                  last edited by

                                  This post is deleted!
                                  1 Reply Last reply Reply Quote 0
                                  • N Offline
                                    Natacha3 @bhepler
                                    last edited by

                                    @bhepler It works perfectly !
                                    Thank you so much for sharing !

                                    1 Reply Last reply Reply Quote 1
                                    • S Offline
                                      stangarrang
                                      last edited by

                                      @bhepler I was hoping you could give me a hand. I’ve got your motion working but. Pm2 is not auto starting the motion. Everything is working fine if I manually “pm2 start motion” and " pm2 start mm" and I’ve tried adding @pm2 start motion to my auto start along side my “pm2 start mm” but that only breaks both processes. I’m trying to get both the magic mirror and the motion to auto start upon reboot.

                                      bheplerB 1 Reply Last reply Reply Quote 0
                                      • bheplerB Offline
                                        bhepler Module Developer @stangarrang
                                        last edited by bhepler

                                        @stangarrang Sure, no problem. Quick question: Did you enter pm2 save after you started both the motion and the Magic Mirror processes?

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

                                          @bhepler yes that was one of the first things I did but after reboot nothing was saving. Actually just as you replied I went through a couple steps for pm2 startup plus the command that came after. After that I did a pm2 save and now things are working well :) thanks for your prompt reply!

                                          1 Reply Last reply Reply Quote 1
                                          • A Offline
                                            AnduriI
                                            last edited by

                                            is it possible to use Motion to activate the screen and MMM-Facial-Recignition simultaneously? Maybe also use the on_event_start to activate some lights in the front of the MM to increase image quality…

                                            bheplerB 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