I have a small room, also known as a closet *cough* that I have my printer and some small computers in. Some day I plan to put a 3D printer there as well. It would be wise to set up a simple webcam, so I can check on it. You know, in case of fire and stuff. 😉
Setting up a webcam w/o a UI
It’s easy. Just plug it in. Before you do though, list your usb devices.
Now plug it in, and list them again. This will help you find the BUS and DEVICE_ID of your device.
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 002 Device 002: ID 1e4e:0110 Cubeternet # <-- This one Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 005 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
If the device and driver is correctly initialised, you will have 1-or-more video devices in “/dev”.
ls /dev/video* # with one camera, you should get /dev/video0
To get a whole bunch of data about your device, do a verbose lsusb.
# List everything about -s BUS:DEVICE (i.e. 002:002, 2:2, etc) lsusb -v -s 002:002 # To get the resolutions available, grep it lsusb -v -s 002:002 | egrep "Width|Height"
My cheap camera’s resolutions return this:
wWidth 640 wHeight 480 wWidth 352 wHeight 288 wWidth 320 wHeight 240 wWidth 176 wHeight 144 wWidth 160 wHeight 120 wWidth( 0) 640 wHeight( 0) 480 wWidth( 1) 352 wHeight( 1) 288 wWidth( 2) 320 wHeight( 2) 240 wWidth( 3) 176 wHeight( 3) 144 wWidth( 4) 160 wHeight( 4) 120
Meaning my camera supports 5 resolutions:
- 640×480 (0) ** 4:3
- 352×288 (1) ** 11:9
- 320×240 (2) ** 4:3
- 176×144 (3) ** 11:9
- 160×120 (4) ** 4:3
My camera’s default seems to be 352×288.
Setting up Motion
sudo apt-get install motion # Next, create a config file mkdir ~/.motion nano ~/.motion/motion.conf
At a minimum, you need this in your config:
stream_port 8081 # the port of the web server stream_localhost off # default is on (localhost only)
My config looks like this:
stream_port 8081 # the port of the web server stream_localhost off # default is on (localhost only) stream_quality 95 # default: 50 (%) output_pictures off # default: on, writes jpegs every second width 640 height 480 #videodevice /dev/video0
You can customize it to suit your needs.
NOTE: Be careful with the documentation above! Features like webcam_port are included in the documentation, but they’re actually deprecated and no longer supported. The docs do say this, but you may not notice this unless you read ever word.
Test it by running Motion.
motion # CTRL+C to exit
If you want it always running, you’ll have to set it up as a service.
Now simply visit port 8081 of the machine to view the active webcam.
Motion has some neat features. As the name suggests, it can actually detect motion. Check out the docs to learn more.
Variant: Running it when you’re not home
Here’s a clever idea:
A script that regularly checks if a machine with your phone’s mac address can be reached on the local network: