DIY Indoor Surveilance Cameras with HomeAssistant
Created 2024-09-01
As a new homeowner getting ready to take my first big trip away from the house later this month, I'd naturally like to keep tabs on things while I'm away. I already have numerous environmental sensors scattered around, and I can connect to HomeAssistant remotely over Tailscale, but cameras would give me the extra peace of mind. Rather than buy an off-the-shelf IP camera (that would be too easy), I decided to put something together myself.
I started out with plugging a couple of cheap webcams that had been lying around directly into my Home Assistant box, which lives in a fairly central room on the ground floor. I installed the MotionEye add-on. The process was surprisingly painless and within 15 minutes or so I had those two cameras feeding into my HomeAssistant dashboards. I'd like to have a couple more cameras in key places, though, that are not as near the Home Assistant box.
An online order later I had two 1080p webcams, two Raspberry Pi Zero 2 boards, and most of the requisite accessories (had to do a second order for power supplies). The particular parts and sources I'm using come to around $60 per unit - though I'm sure cheaper options would be available.
For the software I turned to MotionEyeOS, an all-in-one solution which claimed to support the Pi. As it turned out the support didn't go as far as I had hoped.
MotionEyeOS Struggles
I downloaded the latest release for the Raspberry Pi, flashed it to an SD card using the provided script which sets up the Wi-Fi network automatically, plugged it in, and... nothing happened. The LEDs on the board did not turn on, I didn't see any new devices show up on the network.
Troubleshooting, I switched the power supply for a better one (nothing), re-flashed the SD card (still nothing), and grabbed an in-use RPiZ2 board running Octoprint and swapped the SD cards (Octoprint SD card booted in the new board, MotionEyeOS did not boot in the old board). This narrowed it down to the software on the SD card.
Re-reading the release notes I realized that while it supports the RPi Zero W, it did not mention the RPi Zero 2. A bit of searching turned up this fork which claimed to add support. Downloaded that, reflashed, plugged everything back together, and this time I got a nice green light on the board. Still nothing showing up on the network, though.
Here are the steps I followed so far:
- Download release which supports RPi Z2 from this fork
- Download the install script from here with
wget -O writeimage.sh https://raw.githubusercontent.com/ccrisan/motioneyeos/master/writeimage.sh
- Run
sudo ./writeimage.sh -i motioneyeos-raspberrypi3.img.gz -d /dev/sda -n MySSID:MyWiFiPassword
replacing /dev/sda with SD card block device and Wi-Fi credentials as appropriate
At this point I dug out a mini-HDMI cable (ugh) and plugged the thing into a monitor. It seems the new problem is that it cannot find the Wi-Fi interface. Throwing up my hands once again, I downloaded the latest Raspberry Pi OS and booted that up. I definitely didn't accidentally buy a board with no Wi-Fi because there was a wlan0
device sitting there. Looks like I may need to give up on MotionEyeOS. The latest release is nearly three years old and support for this hardware seems to be lacking.
Plans back in Motion
Luckily, it is completely possible to just install MotionEye directly on Raspberry Pi OS. I'm now following this official guide.
After using raspi-config
to connect to Wi-Fi I ran an apt upgrade
then installed the motion
daemon from the official repositories. It is a slightly out off date version, but will hopefully be good enough for now. A systemd start motion && systemd enable motion
gets it going.
To clarify, since this took me a bit to wrap my head around, MotionEye is a web UI frontend for motion
, which actually manages the cameras. I don't actually think I need the frontend since I intend to use HomeAssistant exclusively to view the camera streams. The config is rather labyrnthine, but I can mostly just copy the default.
The most important config parameters for my use are as follows:
# Get the full resolution
width 1920
heigth 1080
framerate 15
# For now I'm not interested in saving photos or videos
picture_output off
movie_output off
stream_port 8081
# Allow the camera to be accessed over the network
stream_localhost off
The last piece to change is to edit /etc/hostname
to something I'll recognize when I open up my router controls to give it a static IP.
In HomeAssistant, I added the camera directly as a video stream using the "MJPEG IP Camera" integration. All it needs is a name and the IP:port combination I set above in the config and using my router.
All this will do for now is give me a real-time view (at less than 15 FPS) of what the camera sees. No recording, stills, or motion alerts. I may add these things later, especially if I set up more outward facing cameras.
Adding More Cameras
Rather than go through the entire setup process again for a new camera, I opted to just image the SD card. There is probably a way to do this that doesn't involve a 32GB file (using a smaller SD card would be one, I guess) but the simplicity of this approach justifies the storage space, in my opinion.
sudo dd if=/dev/sdd of=motion_rpiz2.img bs=128M status=progress
I can then build more camera units as follows:
- Image the SD card by reversing the arguments of the above command:
sudo dd of=/dev/sdd if=motion_rpiz2.img bs=128M status=progress
- Mount the resulting root partition and change
/etc/hostname
.
And that's it! I can then plug the unit in, check for its IP address on my router, assign a static IP, and then connect to it in HomeAssistant.
Future Plans
Like all good projects, I expect to come back to this and improve it in the future. Here are a few of my ideas:
- Add a second camera to one or more of the units. This will involve editing
/etc/motion.conf
and figuring out how the cameras are indexed. I'll also need a USB hub since the Raspberry Pi Zero only has one USB port. - Add motion alerts. I'd like these to be published to MQTT and to save photos (maybe video) in a place where HomeAssistant can access them. I would also be nice to be able to enable and disable motion alerts - I only need alerts for interior cameras when I'm not home.
- Make 3D printed cases for the units. Right now it's a bare circuit board taped to the top of a webcam. Not the prettiest.
In Review
I wrote the first half of this post as I put together two units, in early September. They were great to have while I was away, it was nice to check in and make sure nothing horrible had happened to the house. I wrote the second half of this post just over a month later, as I was putting together a third camera. Ironically, this was also the first I've had any problems with the units - one got unplugged due to a loose outlet and some banging on the outside wall, and another just dropped off the network. Time to do some debugging... That said, I'm very happy with these cameras units overall!