Manage D-Link DCS-960L motion detection using a RaspberryPi

Installing and configuring a surveillance camera is becoming an operation very easy and very common, strong technical skills are not required anymore. The question is: can we control a "home camera"  with our RaspberryPi ? The short answer is: YES ! I'll show how to use the RaspberryPi to control, using jus the linux shell, a D-Link DCS-960L . Required hardware
  • RaspberryPi
  • D-Link DCS-960L
  • a smartphone (only required to initially setup the camera)
  • a wifi router already configured
  • an internet connection
Initial setup
  • Make sure your internet connection is functioning
  • Configure your wifi network using WPA2 encryption and make sure your network name (SSID) is not hidden
  • Assemble and cofigure your RaspberryPi and check that:
    • curl is installed
    • ssh service  is enabled
    • wifi connection to the router is functioning
  • Connect your smarphone to the local WiFi network you have setup up
  • Configure your camera using your smartphone (following the instructions)
  • Configure you WiFi router and/or DHCP server to always give the same IP address to che camera
Data to collect
  • IP address given to the camera
  • camera's admin username
  • camera's admin password
Now it's time to create our control script. Login, via SSH, to your RaspberryPi and create file cam.sh in a directory you want, putting inside this new file the following text:
#!/bin/bash

#
# DAVIDE AIRAGHI
#
# DLINK DCS-960L console motion detection manager
# License GPLv3 - see https://www.gnu.org/licenses/gpl-3.0.en.html
#
# This script allows you to manage (query, enable, disable) 
# motion detection of your DCS-960L camera from console
#
# Requirements: 
# - bash shell
# - curl
# - same network used for camera and computer
#

# camera authentication info
USER="YOUR_ADMIN_USERNAME"
PASS="YOUR_ADMIN_PASSWORD"

# generic info
# seconds to wait before enabling motion detection
TIMEOUT_BEFORE_ENABLE=15
# seconds to wait before disabling motion detection
TIMEOUT_BEFORE_DISABLE=0

# camera base url
URL="http://YOUR_CAMERA_IP_ADDRESS:80"

# camera video capture params
CAPTURE_SENSITIVITY="60"
CAPTURE_ENABLE_MASK="FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
CAPURE_DISABLE_MASK="000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
CAPTURE_ENABLE_URL="/config/motion.cgi?enable=yes"
CAPTURE_DISABLE_URL="/config/motion.cgi?enable=no"
INFO_URL="/config/motion.cgi"

if [ "$ACTION" = "" ]; then
    ACTION=$1
fi

case $ACTION in
	info) 
		ACTION=""
		IS_ENABLED=$( curl -u ${USER}:${PASS} ${URL}${INFO_URL} 2>/dev/null | grep 'enable=yes' )
		if [ "${IS_ENABLED}" != "" ]; then
			echo enabled
		else
			echo disabled
		fi
		;;
	enable)
		ACTION=""
		sleep ${TIMEOUT_BEFORE_ENABLE}
		curl -u ${USER}:${PASS} "${URL}${CAPTURE_ENABLE_URL}&sensitivity=${CAPTURE_SENSITIVITY}&mbmask=${CAPTURE_ENABLE_MASK}" 2>/dev/null >/dev/null || exit 1
		;;
	disable)
		ACTION=""
		sleep ${TIMEOUT_BEFORE_DISABLE}
		curl -u ${USER}:${PASS} "${URL}${CAPTURE_DISABLE_URL}" 2>/dev/null >/dev/null|| exit 1
		;;
	toggle)
		ACTION=""
		IS_ENABLED=$( curl -u ${USER}:${PASS} ${URL}${INFO_URL} 2>/dev/null | grep 'enable=yes' )
		if [ "${IS_ENABLED}" != "" ]; then
			sleep ${TIMEOUT_BEFORE_DISABLE}
			curl -u ${USER}:${PASS} "${URL}${CAPTURE_DISABLE_URL}" 2>/dev/null >/dev/null || exit 1
		else
			sleep ${TIMEOUT_BEFORE_ENABLE}
			curl -u ${USER}:${PASS} "${URL}${CAPTURE_ENABLE_URL}&sensitivity=${CAPTURE_SENSITIVITY}&mbmask=${CAPTURE_ENABLE_MASK}" 2>/dev/null >/dev/null || exit 1
		fi
		;;
	*) 
		ACTION=""
		;;
esac

exit 0
Before saving the file replace the following placeholders :
  • YOUR_ADMIN_USERNAME : camera's admin username
  • YOUR_ADMIN_PASSWORD : camera's admin password
  • YOUR_CAMERA_IP_ADDRESS : camera's ip address
After saving the file make it executable ( chmod +x cam.sh ). Now, if everything went well and there are no other issues (network, firewall, ...), you should be able to manage your camera's motion detection status using the linux shell:
  • cam.sh info : get motion detection status (enabled disabled)
  • cam.sh enable : enable motion detection
  • cam.sh disable : disable motion detection
  • cam.sh toggle :enable motion detection if it's disabled or disable motion detection if it's enabled