A wiki page I wrote a while ago for domoticz, saving it for a rainy day here.
This chapter describes how to set up motion detection using your camera and raspbian/debian/ubuntu box. Works on Raspberry Pi but read notes on performance in the “Some notes” paragraph. Howto by safi78.
Motion is a package that’s been around for a long time, it’s very good, very well documented (find it here)
You can configure your ‘video’-devices (ip, usb, tvcards, etc) in different config-files and include them in the main-config file. You can put your central settings in one file and overrule or specify different values in extra config-files for individual cameras. This leads to a nice centralized set up server / ip-cam configuration.
As a little bonus, I added a ‘dummy’ motion sensor in Domoticz for each ip-cam and switch that with the use of the api-commands to “On” if there’s motion detected and “Off” when the motion is stopped. You basically get (VERY) reliable motion-sensors for further logic in the basic webinterface, blockly or LUA.
Basically you point motion to your ip-cam’s snapshot URL (different for each brand, model, so not going in to that ‘standard’), motion looks into the snapshots, compares them, determines if there’s motion based on your thresholds and can glue the relevant snapshots together as a recording to a directory you specify.
Install the motion package
ssh into your debian/ubuntu/raspbian box and run:
sudo apt-get install motion
This will install the package and all it’s dependencies.
After this the daemon will be disabled by default in /etc/default/motion so you’ll have to change that.
sudo nano /etc/default/motion
change the configuration to this and save the file:
# set to 'yes' to enable the motion daemon start_motion_daemon=yes
Now the daemon can be started & will be started at boot time:
sudo /etc/init.d/motion start
and now it does… nothing. Continue with “Configure the config files”.
Configure the config files
The files that are actually important for configuration are:
- The main configuration file motion.conf. If you only use one ip-cam all configuration goes in here:
- The extra configuration files, if you use more than one ip-cam, you put all the central configuration in ‘motion.conf’ and include these at the end for each cam you want to setup, these files are more or less the same and will override the settings you specify explicitly for each cam in each file.
By default these files are created:
and can be included at the end of the central motion.conf. You can specify your own names (which is very handy if you have more than a few files, you will always know which ip-cam you are configuring if you give the files a useful name like: cam_frontdoor.conf or something like that). But make sure to include these files in motion.conf with the correct names or it will not work of course.
The config files are very well documented, so they look huge, but don’t be scared, it’s mostly useful comments about the settings you are changing.
Just start with motion.conf and change everything you need, except the individual ip-cam settings. For instance if you want to use the same motion detection settings or folders where motion will output the files, put them in here.
For each individual setting, put it in a separate config file and just include them all the at the end of the file.
For example, you want to use the same video_codec for each output file, but you want to specify different resolutions for the individual webcams you put it like this:
width 640 height 480
width 1280 height 720
Make sure that after every change of a config-file you issue:
sudo /etc/init.d/motion restart
to load the new settings.
Of course you will need a lot of other stuff in your config file, ipaddres of your ip-cams, username & password to access your snapshot URL’s, directories where to put the recordings, but that’s all pretty self-explanatory.
Also see the URL at the beginning a lot of settings you’ll find out just reading the config files, but the Wiki is very useful as well.
You can fiddle around with the sensitivity, light bursts, tell motion how many pixels need to change before actual motion is detected (e.g. find out how much pixels your cat is, put that in the file and it will not trigger motion on your cat ;)), exclude certain areas, specify how long a minimal motion-event should be, how many frames before and after should be included, how long after motion the motion-event should end, etc etc.
Setup a new dummy motion-sensor in Domoticz
This would be the (really cool) part:
If you add the ‘Dummy’ hardware to ‘settings -> hardware’ in Domoticz you are able to add dummy switches and sensors.
Add a dummy motion-sensor and find the ‘ID’ in ‘settings -> devices’ in Domoticz, you’ll need that to switch the virtual motion-sensor through the API-commands.
Now find one of your ip-cams you want to communicate with the dummy motion-sensor and add the following two lines and change the bold parts to suit your needs:
on_event_start /usr/bin/curl -s "http://api_username:api_password@domoticz_server:domoticz_port/json.htm? type=command¶m=switchlight&idx=your_ID&switchcmd=On" on_event_end /usr/bin/curl -s "http://api_username:api_password@domoticz_server:domoticz_port/json.htm? type=command¶m=switchlight&idx=your_ID&switchcmd=Off"
The on_event_start will switch your motion-sensor on at motion detection.
The on_event_end will switch it off again.
And it all works together very well, because Domoticz & motion will ‘access’ the IP-camera at the URL’s you specify for the needed screenshots, but they will not interfere:
1. You can still use your cam in Domoticz (e.g. take a snapshot of your Doorbell-event and send it with mail).
2. The cam streams can be saved to a NAS if you want that, with the settings you specify.
3. The motion-sensors will switch on and off for use within Domoticz as well. You can use it in the normal web-interface, blockly or LUA, endless possibilities.
- CPU: I use a dedicated, pretty good server for multiple webcams. I think a raspberry can handle one ip-cam, but it’s very CPU-hungry, so I wouldn’t recommend a pi for this kind of load. (100Mbps, CPU slow, etc).
- I know of a motion ‘light’ package that exists for the Raspberry Pi Cam you hook into the motherboard that’s tuned for the rasp, if I can find that again, I’ll add it to the guide later on. I did test that, worked very well, if I recall correctly it had a 20% CPU load.
- Storage: next to that, use a NAS or a big drive to store your files, because the recordings can add up and eat away your drive space pretty fast!
- Bandwith: ofcourse it’s going to use a constant stream per cam, which can really up the traffic on your network.
- Pets: it’s very reliable and tunable and will detect motion if there is. That also means dogs, canaries and goldfish. And don’t have cats as a pet, I hate cats.