My new project: Raspberry Pi Zero W + Pi Camera "security" cam

Recently I embarked on a “fun” project quest to set up a “security” camera to observe the walkway to my front door. My door doesn’t have a peephole, although I might eventually add one, and by the time some stranger has rung my doorbell, it’s too late to see them through the nearest window.

So, the RPi Zero W (for WiFi) with the Pi Camera V1.2 integrated into the case seemed like a good place to start. The RPi Zero (which I’ll abbreviate to “RPi0”) is tiny - only half the area of a 3in X 3in (7.5cm X 7.5cm) Post-It note.

I installed RPiOS (Bullseye), and the MotionEye package (not MotionEyeOS, which is apparently defunct), and prerequisites/dependencies. However, I couldn’t seem to get MotionEye fully functional, so I eventually opted to just use the motion package without it. (MotionEye, as far as I can tell, is just a configuration interface for the motion package, with some added useful features.)

I learned that Bullseye introduced some changes that affect Pi camera operation - use of libcamerify instead of raspivid, etc. - so I’ve been reading up on how to set it up for motion-activated video and stills. I adjusted the motion.conf settings, attached (via Velcro) the RPi0 to a slat in an upstairs window blind, angled it down at the approach to my door, and started libcamerify motion via ssh from my other Pi, which is powered on 24/7, screen locked.

Success:
Motion-activated video clips and still pictures get saved to a small USB drive I’ve attached to the RPi0, which I can access across my LAN via sftp.

I’m not concerned about physical security of this USB drive and the stored pictures, as my goal is simply to know who is, or was, at my door. Using the USB drive will prevent a lot of wear and tear on the RPi0’s micro-SD card, and I’ve set a cron job to periodically auto-delete the image and video files older than 1 day from the USB drive: 0 0 * * * find /media/my_home-folder/USBdrive -type f -mtime +1 -delete)

Failure:
I haven’t been able to successfully stream video, i.e. live, from the camera to any of my machines, whether in Firefox (LibreWolf, actually), or in VLC Media Player.

It seems there are plenty of people with the same problem posting all over the internet, but trying to sift through all the noise and outdated recommendations, (Buster vs. Bullseye and such), has gotten me nowhere so far. Hope springs eternal, though! Ideas welcome!

Could also be Improved:
Across my LAN, I can open and view my image and video files over sftp from any of my computers, even the Librem 5 ( :slight_smile: ):

I generally have at least one computer powered on when I’m at home, so I can access the files at will.

I need to set up some form of auto-notification for detected motion, though, because the stored image directory doesn’t refresh itself, meaning I have to manually refresh it to see if there are any newly captured images, i.e. if anybody is at my door. (Hence the need to get streaming working.)

It can take quite some time to repopulate the many, many files that might have been stored throughout the day, so it’s not ideal to have to refresh the directory… unless I want everybody who rings my doorbell to go away before I get to the door. (If that were the case, I could just avoid ever answering the bell and not even bother with the camera. :wink: )

I know it’s possible to set up alerts by email, or with certain messenger applications, and possibly get pop-up notifications over the LAN (?), so I’ll be researching those more, especially if I don’t figure out streaming. (Ideas welcome here, too.)

Incidentally, I don’t intend to expose the camera to the internet at large, so no worries there.

===
Somewhat related threads: Home security cameras and Security: Anyone Using Security Cameras w/ L5?

5 Likes

Just thinking aloud but that suggested to me a couple of things:

  • Would it be better to store every file on a different device, rather than on the camera Pi? i.e. transfer the file across the network at the time it is captured? (In other words, reverse the roles of sftp.)
  • Adjust the directory structure so that not all the files are in the one directory. Maybe a directory with the last 5 images only will be adequate, with earlier images retained in another directory or directories (and then time-based cleanup of those).

Certainly I’ve found that listing a directory that is being accessed over sftp and that contains many files can be quite slow (albeit that that is over the internet, not over the local network).

That may depend on whether you want this to be “zero code”.

If you adopted my suggestion per bullet point one above then on the “server” you should be able to watch the directory for new images (man inotify ?) and do whatever you want with events - presuming that you are willing and able to write the code.

My concern though would be that if you are already getting lots and lots of files then getting automatic notifications will be painful / boy who cried wolf.

If on the local network, the server could push new images to all client computers so that all client computers would always have those last 5 images, for example.

2 Likes

I considered that, too, but:

  1. I use a couple of different computers throughout the day; some will be powered on, some will not (excepting an RPi4 that’s running Pi-hole for me.) I suppose I could designate one for constant operation, but it would use more electricity than the RPi0.

  2. Would I need to leave the ssh connection between the two devices open all the time? (I use a password, not keys, for ssh authentication, with recipriocal/interchangeable connections across any of my devices.)

That’s not a bad idea. I suppose that separate storage could be automated?

That would work. I’m guessing I would have problems sending “motion-detected” alerts direct from the RPi0 to other desktops…?

That’s why I wanted to use the config file options to, e.g., notify only on start of motion via (?some service?), rather than watching the directory for every created file. It could still get annoying, though.

Would it matter if some were powered on and some not? That is, would destination errors interfere with the motion detection on the RPI0?

I don’t suppose the failed files would be created later once the machine is turned on, right?

1 Like

Did I missunderstand your success? Doesn’t motion have notification feature? Also netcam? At least on this man page (but could be different with RPi) Ubuntu Manpage: motion - Detect motion using a video4linux device or network camera The features do not work or your setup does not support them?

1 Like

I think quite a few motion config settings have been deprecated, as the configuration tutorial I linked to mentioned. Some of them can still be added back manually, I think. The current config file defaults include these options which could be used for notifications, I guess:

############################################################
# Script execution configuration parameters
############################################################

# Command to be executed when an event starts.
; on_event_start value

# Command to be executed when an event ends.
; on_event_end value

# Command to be executed when a movie file is closed.
; on_movie_end value

I can probably think of some command to achieve what I need, but as for email, I’m unclear how I might email myself from the RPi0, which is fairly low in processing power, gets bogged down, and on which I haven’t set up my email accounts. (And I think that might be problematic anyway, given my encrypted email providers’ IMAP requirements and number-of-allowed-device limits.) Also I have no experience with messaging from the shell, so there’s a learning curve there. That’s why I asked for ideas.

As for netcam and streaming, it’s just that I don’t know what the Pi camera uses or needs, or how to set it up. As I said, the internet is full of very dated questions and circuitous meanderings about that, and the recommendations I’ve seen so far don’t align with successful implementation for the Pi camera in particular. I’m still researching, though. And is the Pi camera a “netcam?” I don’t know.

When I access the allegedly correct stream in a browser, through the correct port, I see the display, but it’s just a gray window, with a message like “unable to open video device.” That’s whether I have libcamerify motion running or stopped on the RPi0. I’m a novice at this, so I have no idea what is required. Could it be a firewall issue?

You see? I’m shooting blind here. :wink:

But at least I’ve got some functionality:

  • manual test still
  • manual test video
  • motion-activated stills
  • motion-activated video
  • configuration adjustments/refinemints

Here’s what a current default motion config file looks like, without added parameters:

# Rename this distribution example file to motion.conf
#
# This config file was generated by motion 4.5.1
# Documentation:  /usr/share/doc/motion/motion_guide.html
#
# This file contains only the basic configuration options to get a
# system working.  There are many more options available.  Please
# consult the documentation for the complete list of all options.
#

############################################################
# System control configuration parameters
############################################################

# Start in daemon (background) mode and release terminal.
daemon off

# Start in Setup-Mode, daemon disabled.
setup_mode off

# File to store the process ID.
; pid_file value

# File to write logs messages into.  If not defined stderr and syslog is used.
; log_file value

# Level of log messages [1..9] (EMG, ALR, CRT, ERR, WRN, NTC, INF, DBG, ALL).
log_level 6

# Target directory for pictures, snapshots and movies
; target_dir value

# Video device (e.g. /dev/video0) to be used for capturing.
video_device /dev/video0

# Parameters to control video device.  See motion_guide.html
; video_params value

# The full URL of the network camera stream.
; netcam_url value

# Name of mmal camera (e.g. vc.ril.camera for pi camera).
; mmalcam_name value

# Camera control parameters (see raspivid/raspistill tool documentation)
; mmalcam_params value

############################################################
# Image Processing configuration parameters
############################################################

# Image width in pixels.
width 640

# Image height in pixels.
height 480

# Maximum number of frames to be captured per second.
framerate 15

# Text to be overlayed in the lower left corner of images
text_left CAMERA1

# Text to be overlayed in the lower right corner of images.
text_right %Y-%m-%d\n%T-%q

############################################################
# Motion detection configuration parameters
############################################################

# Always save pictures and movies even if there was no motion.
emulate_motion off

# Threshold for number of changed pixels that triggers motion.
threshold 1500

# Noise threshold for the motion detection.
; noise_level 32

# Despeckle the image using (E/e)rode or (D/d)ilate or (l)abel.
despeckle_filter EedDl

# Number of images that must contain motion to trigger an event.
minimum_motion_frames 1

# Gap in seconds of no motion detected that triggers the end of an event.
event_gap 60

# The number of pre-captured (buffered) pictures from before motion.
pre_capture 3

# Number of frames to capture after motion is no longer detected.
post_capture 0

############################################################
# Script execution configuration parameters
############################################################

# Command to be executed when an event starts.
; on_event_start value

# Command to be executed when an event ends.
; on_event_end value

# Command to be executed when a movie file is closed.
; on_movie_end value

############################################################
# Picture output configuration parameters
############################################################

# Output pictures when motion is detected
picture_output off

# File name(without extension) for pictures relative to target directory
picture_filename %Y%m%d%H%M%S-%q

############################################################
# Movie output configuration parameters
############################################################

# Create movies of motion events.
movie_output on

# Maximum length of movie in seconds.
movie_max_time 60

# The encoding quality of the movie. (0=use bitrate. 1=worst quality, 100=best)
movie_quality 45

# Container/Codec to used for the movie. See motion_guide.html
movie_codec mkv

# File name(without extension) for movies relative to target directory
movie_filename %t-%v-%Y%m%d%H%M%S

############################################################
# Webcontrol configuration parameters
############################################################

# Port number used for the webcontrol.
webcontrol_port 8080

# Restrict webcontrol connections to the localhost.
webcontrol_localhost on

# Type of configuration options to allow via the webcontrol.
webcontrol_parms 0

############################################################
# Live stream configuration parameters
############################################################

# The port number for the live stream.
stream_port 8081

# Restrict stream connections to the localhost.
stream_localhost on

##############################################################
# Camera config files - One for each camera.
##############################################################
; camera /usr/etc/motion/camera1.conf
; camera /usr/etc/motion/camera2.conf
; camera /usr/etc/motion/camera3.conf
; camera /usr/etc/motion/camera4.conf

##############################################################
# Directory to read '.conf' files for cameras.
##############################################################
; camera_dir /usr/etc/motion/conf.d
2 Likes

I like the project and understand your predicament better now. Difficult to come up anything specific without that hardware. Some searches show that similar projects have been done, apparently successfully (like: 1, 2, 3, 4). Motion seems to be up to date (github is alive), so I don’t think the features are deprecated (maybe some supporting packages are missing or not configured). It would be the simplest solution to get that to work. It seems that this kind of projects had more interest several years ago - you’re a bit late to the party :wink:
Did you come across this thread in your previous searches (similar problem) https://forums.raspberrypi.com/viewtopic.php?t=43844?
Are you using motion or the newer motionplus?

2 Likes

Have you considered serving the files over NFS? (Then no need for ssh sftp, etc.) The NFS clients could have automount set up so that trying to access /net/nfs-server-hostname would automatically mount the remote file system and some idle period would automatically unmount.

2 Likes

No, but I can see from the commands in that thread (and by the 2013 date) that it’s not written for Bullseye, which supposedly implemented several changes in managing the camera, in addition to having moved to Python3 (and I’m not sure if that last item affects anything other than the install process for MotionEye, which I’m not attempting to use anymore for now.)

Motion.

According to the Motion Project (for info):

Motion is the original software dating back to 2000 while MotionPlus is a new application. MotionPlus was added as a new application in order to allow for removal of older functionality rarely used while also adding new functionalities.

1 Like

I don’t have any experience with it, but sounds like an interesting option, if a bit complicated (for me). I’m currently using an external SSD attached to a hub on my always-on RPi4B for backing up files from all my machines. (Low electricity usage)

1 Like

I can only really dabble around the edges of your questions.

If you are coding it yourself, no. Handle errors as you want.

Yes, but, for example, sshfs has a reconnect option.

Personally, I envisaged using ssh (as either sftp or sshfs) to transfer files from the source (the Pi with the camera) to a server. I take your point that you don’t really have a server - but maybe that is telling you that it is time that you do. :slight_smile:

Then, if performance is not adequate for random clients pulling files from the server, you would push files to the client using some other protocol or the same protocol, whatever works.

That depends.

NFS is a bit complicated if you want it to be secure.

For limited use and strictly being used on the local network without security, NFS is really simple to set up.

If the possibility exists that you might want to access the camera’s images from outside your local network then security is a must - and in that case SSH is not a bad starting point.

Frankly, from a functionality point of view, it won’t really matter. There are many choices of protocol for remote access to files / for transferring files from one computer to another. So I would suggest getting the bigger picture design sorted out before worrying about exactly what protocol(s) move files around.

I wouldn’t worry about the power consumption of the computers involved but that might depend on whether you have solar panels or not. I mean sure if you are going to operate an industrial strength server in a rack in an air-conditioned computer room then it’s a consideration but my guess is that one solar panel will run every Pi in your house and an optional ultra-compact server if you wanted one - and still have plenty left over.

You could legitimately raise the issue of … but what about at night? (when there is no solar generation) In that case, the first question to you is whether the camera you are using has the IR filter or not. If you are filtering IR then you probably aren’t capturing images at night (which may or may not be a consideration relating back to the underlying purpose here). If you aren’t capturing images at night then you can just sleep the whole functionality overnight (when a panel wouldn’t be generating any power).

IMAP is not used to send email. IMAP is used to receive email when the email is stored permanently on the server. In most cases SMTP will be used to send email to your email service provider for delivery to your mailbox - from where one or more IMAP clients could access that email.

Provided that the email is strictly a notification and does not include any image content, that could work as a way of distributing notifications (and should nicely handle which clients are on at the time and which clients are off).

Sorry, I can’t help with the core of the problem, which is actually using the camera - but it is not something that I have played with.

2 Likes

I just wanted to say this is super cool and I wish you luck. Reading your posts are like reading a master class in fun computing.

4 Likes

Battery powered movement (IR) activated light will help with that. It will make life a bit easier and safer too. And you can either use rechargeable batteries or get a light that has solar panels (and a sensor that activates the light only when it’s dark). Pretty cheap old school tech. Or, you can get a high powered construction light with the sensor and blind anyone trying to approach your porch (makes funny clips for a compilation of blinded people cussing) :bulb:

3 Likes

Thanks for this tip.

Actually, with the exterior lighting I do get some somewhat usable imagery at night, of my walkway and a short stretch of the public sidewalk out front. But right, I don’t expect anyone will knock on my door in the wee hours, although I still review the video clips out of interest. (Unfortunately I don’t have solar panels, but maybe in the future; electricity here is some of the most expensive in the nation, although my usage is not so high. Or I could get a Pi Camera NOIR.)

I was simply thinking that I might have already maxed out the number of devices that Startmail allows for desktop email (unless they’ve removed the limit lately). They require a per-device password. I also use Proton Mail, but I don’t know if it would work on desktop without their “bridge.” I’ll have to investigate, if I decide to use email notification. Or just get a third, simpler provider.

But really, maybe your suggestion about desktop notification and sshfs might work better anyway.

Still, I’ll keep trying to get streaming working, because then I’ll likely notice in real time when random people (petitioners, solicitors, etc.) head for my door.

1 Like

:blush:
You flatter, sir!

1 Like

Using TightVLC Remote/Viewer, I hooked up a plain camera to a small laptop and tried stream. It worked, albeit clumsily but I was just checking to see if it would work. The laptop was old so I had it send (stream IMO is same thing) from my garage peeking out the window, to my desktop in home.

It’s been a long time since I used it when it crashed, but for Linux versions, TightVLC today says:

If you need a version working in Windows 95/98/ME, Windows NT 4.0, or in Unix-like systems (including Linux)

Just a thought,
~s

1 Like

Thanks for the suggestion. The tiny PiZero is fairly low-spec’ed, so I don’t know if it could handle remotely displaying an entire desktop over VNC. There’s also still the issue that my Pi Camera isn’t conveying the video stream to the remote browser (or remotely in VLC), which is my main problem.

Works perfectly for creating and storing stills and video clips with the motion software, though. [Edit: The motion package works for stills and video clips, I mean. Still working on getting actual live streaming to work with the Pi Camera.]

1 Like

Wow. Now I feel like I finally made a contribution. Unless you already tried VNC in which case, don’t tell me that :shushing_face:

2 Likes

No, I meant that the installed motion software makes the Pi Camera work for stills and video clips. :slight_smile:

But keep contributing. You’re a valued forum member. :+1:

4 Likes

Or, at the risk of sounding like a stuck record :wink:, run your own mail server. If you only need this internally to your network then the security considerations become less significant, and the degree of difficulty will be lower, and you just won’t have any restrictions. However running your own mail server may be overkill if the only thing it is used for is motion notifications.

From what I’ve seen of commercial offerings, they probably just use some proprietary protocol to throw UDP (unicast or multicast) notifications around the local network. That would very likely require some coding.

2 Likes

For notifications you can also look into https://ntfy.sh/

Just another option.

2 Likes