Raspberry Pi Security Cam: Installing MotionEye
Installing MotionEye (the easy way)
MotionEye is a fantastic package that turns your SBC (Raspberry PI, Odroid, etc) with camera into a webcam with motion capture, etc. It has a nice web interface to allow configuration and will allow you to view multiple video streams in one web page.
The easiest way to install MotionEye is to download MotionEyeOS, a stripped down platform specific linux distribution with MotionEye preinstalled. More information is on the MotionEyeOS Wiki page here.
After copying MotionEyeOS to an SD card, plug it into the RaspberryPi, connect the Pi to your ethernet then power it up. Look for it on your network using something like Fing (it will appear as something like “meye-123456”) and connect to it using a browser. Log in using a user name of “admin” with NO password and click on the menu icon to configure MotionEye.
Things you will want to set up immediately:
- Set a password for “admin” – this account will also become your login if you choose to SSH to the Pi. The admin username and password can be set in the “General Settings” section of the settings.
- Set up WiFi – under “General Settings” enable “Advanced Settings” and a “Network Settings” section will appear where WiFi credentials can be entered.
- Change the hostname – under “General Settings” enable “Advanced Settings” and a field called “Hostname” will appear in the “General Settings” section where you can enter a hostname.
Potential WiFi Issues
After you have established a WiFi connection you can verify its strength by connecting via SSH and entering:
iwconfig wlan0
If you want to see all available WiFi access points and their signal strength you can enter:
iwlist scan
If you are using WiFi for the connection then you may need to use a lower resolution and/or frame rate for the camera depending on how strong your connection is and WiFi congestion.
Installing MotionEye (the harder way)
The TP-Link AC1300 USB WiFi adapter that I chose for this project did not have a native Raspbian driver (at the time of installation). If I had known this beforehand I might have chosen a different WiFi dongle. We therefore cannot download the MotionEyeOS (which is not designed to allow custom drivers to be installed) and instead have to use a standard distribution and add MotionEye to it.
Setup Your OS
I’m using a Raspberry Pi, so I start with Raspbian Lite, a stripped down version of Raspbian which is itself based on Debian. That is installed to an SD card using the imaging tool of your choice (ApplePi-Baker is a nice one on MacOS). Because I’m going to set up the Pi while it’s headless I need to enable SSH (which is disabled by default). So I mount the SD card on my desktop computer and add an empty file called “ssh” to the root directory of the SD card.
Insert the card into the Pi, plug in the ethernet cable and power it up. Using an SSH client, log into the Pi at “raspberry.local” or “raspberry” using the username “pi” and password “raspberry”:
ssh pi@raspberry.local
If you are unable to connect to the Pi then you might have to scan your network using a tool like “Fing” and connect using the ip address instead.
Type sudo rasp-config to start up the configuration tool:
- Set the time zone and other localisation options by selecting Localisation Options .
- Enable camera by selecting Interfacing Options / Camera .
- Expand the partition to make use of the whole SD card Advanced Options / Expand Filesystem .
- You may also want to change the hostname from “raspberry” to something of your choice. The Pi will reboot when you click Finish .
After you reconnect to the Pi via SSH (remember to use the new hostname if you changed it), you may want to change the password from the default “raspberry” by typing passwd .
Changing Default Username (optional)
You may also want to change the user name from “pi” to something else. First enable the root account and set a passwd for it:
sudo passwd root
And enable root login via SSH:
sudo nano /etc/ssh/sshd_config
Find the line starting with PermitRootLogin and change it to PermitRootLogin yes then close and save file. Restart sshd service using:
sudo /etc/init.d/ssh restart
Then logout:
logout
Which will kill the SSH connection, so reconnect as “root” using the password you just set and then change the username from “pi” to “admin” (for example):
usermod -l admin pi
Now disable root login via SSH:
sudo nano /etc/ssh/sshd_config
Find the line starting with PermitRootLogin yes and change it to #PermitRootLogin yes then close and save file. Restart sshd service using:
sudo /etc/init.d/ssh restart
Logout and reconnect over SSH using the new user name.
Download the WiFi Dongle Driver
Because the driver for the TP-Link WiFi dongle is not in the Raspbian mainstream we have to install the version that is matched to the version of Raspbian we installed. A user on the raspberrypi.org forums called MrEngelman has pre-built drivers for various kernel versions and we can install the correct one by first downloading and running a script to identify the correct driver:
sudo wget http://fars-robotics.net/install-wifi -O /usr/bin/install-wifi
sudo chmod +x /usr/bin/install-wifi
sudo install-wifi
Configure WiFi
Edit the WiFi configuration:
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
and add the following to the bottom of the file for each WiFi network you want to be able to use:
network={
ssid="testing"
psk="testingPassword"
}
Reconfigure the interface with:
wpa_cli -i wlan0 reconfigure
After a few seconds the WiFi dongle should connect to your WiFi network. You can verify whether it has successfully connected using ifconfig wlan0 . If the inet addr field has an address beside it, the Raspberry Pi has connected to the network. If not, check that your password and ESSID are correct in the wpa_supplicant.conf file above.
Install MotionEye
First update the package manager:
sudo apt-get update
Install the Python package manager, pip and other dependencies:
sudo apt-get install python-pip python-dev curl libssl-dev libcurl4-openssl-dev libjpeg-dev
Install motion which will force the install of a bunch of dependencies:
sudo apt-get install motion
Install motioneye, which will automatically pull Python dependencies (tornado, jinja2, pillow and pycurl):
pip install motioneye
Prepare the configuration directory:
sudo mkdir -p /etc/motioneye
sudo cp /usr/local/share/motioneye/extra/motioneye.conf.sample /etc/motioneye/motioneye.conf
Change the default port for the web interface by editing the config file:
sudo nano /etc/motioneye/motioneye.conf
and changing line port 8765 to port 80 .
Make sure the camera module is loaded:
sudo nano /etc/modules
and add a line containing bcm2835-v4l2 .
Prepare the media directory:
sudo mkdir -p /var/lib/motioneye
Add an init script, configure it to run at startup and start the motionEye server:
sudo cp /usr/local/share/motioneye/extra/motioneye.systemd-unit-local /etc/systemd/system/motioneye.service
sudo systemctl daemon-reload
sudo systemctl enable motioneye
sudo systemctl start motioneye
You should now be able to go to the Pi using a web browser and see the MotionEye web interface where you can configure the camera. Login using the user name admin with no password.
Upgrading MotionEye
Do not update via the web interface but instead just issue:
pip install motioneye --upgrade
sudo systemctl restart motioneye
References
- Installing out of stream prebuilt Realtek drivers – https://www.raspberrypi.org/forums/viewtopic.php?t=103989&start=150
- Installing MotionEye on Raspbian – https://github.com/ccrisan/motioneye/wiki/Install-On-Raspbian
I would like to have all the files saved on an external web server and be able to stream the latest video as well as view a live stream
You can have MotionEye upload the files automatically as it records them to an FTP server, SFTP server, Google Drive or Dropbox. I have only tested with Google Drive, but that seems to work fine. But it seems like you should be able to get it to FTP the files to your webserver using the FTP or SFTP option.
You can live stream the video cam typically by accessing it using port 8081 on the same network, however if you are talking about live streaming on an external web server from a Pi setup that is behind a firewall, then I think you would either have to:
Hi
having issues installing on my RPI4. But before I spend time on resolving mya I ask how many frames per second of motion can be recorded please using the MMAL CSI OnBoard Camera module?
Sorry for the long delay in responding – somehow I missed this. On my setup the frame rate tops out at around 26fps with 800×600 resolution using a MMAL CSI camera module on a RaspberryPi 2 Model B with no overclocking. I would imagine the Raspberry Pi 4 could manage faster frame rates since much of the speed depends on the gpu speed.
There are various threads on https://www.raspberrypi.org/forums indicating that very high frame rates can be obtained – enough to do high speed photography. However, that requires using a fairly low resolution and some optimization.
Holy Shnikees!! I have been trying to get motioneye to work with my Pi4 for a week. Inevitably no matter how many youtube videos I followed the install would bomb out at varying points. I tried your install method out of desperation. I am excited to report it has been an unqualified success. Thank you. -RJ
You are welcome! Glad it was helpful.