We’re going to explore how to control an IP Camera using PHP. By leveraging the power of PHP, you can automate several tasks such as camera movement, setting up alarms, and adjusting the speed of your camera movements.
Let’s jump into it!
Understanding the Camera Class
The PHP script revolves around a class named Camera. This class holds all the methods and properties that you need to interact with the IP camera. Here’s a quick rundown of the properties:
adress: This holds the IP address of the camera.auth: The authentication string for the camera.positions: An array holding the positions the camera should go to.speed: The speed at which the camera should move.
class Camera
{
private $adress;
private $auth;
private $positions;
public $speed;
...
}
The __construct() method initializes the properties with default values. The positions are set as an array of preset positions.
Controlling Camera Positions
To make the camera move to specific positions, we have the method Go_To_preset($position, $time). This method sends a command to the camera to move it to a preset position and then waits for the specified amount of time before moving to the next position.
public function Go_To_preset($position, $time)
{
$this->y=$this->y+1;
echo ($this->Debug == 1 ? "$this->y. Position $position for $time sec. \n" : NULL); // Debug
$this->Send_CMD_To_Cam("decoder_control.cgi?command=$position");
sleep($time);
}
Setting Camera Speed
To control the speed of the camera, we have the Set_Speed($speed) method. This method sends a command to the camera to set its movement speed.
public function Set_Speed($speed)
{
$this->Send_CMD_To_Cam("set_misc.cgi?ptz_patrol_rate=$speed");
}
Activating Alarms
The class also includes methods for toggling alarms. There are two types of alarms: Motion Alarms and Sound Detection Alarms. Each type has its own method for toggling the alarm and setting the sensitivity.
public function Toggle_Alarm($toggle, $sensitivity)
{
$this->Send_CMD_To_Cam("set_alarm.cgi?&motion_armed=$toggle&motion_sensitivity=$sensitivity");
}
public function Toggle_Alarm_On_Sound($toggle,$sensitivity)
{
$this->Send_CMD_To_Cam("set_alarm.cgi?sounddetect_armed=$toggle&sounddetect_sensitivity=$sensitivity");
}
All of these methods utilize the Send_CMD_To_Cam($command) function, which uses the curl function to send commands to the IP camera.
Utilizing the Camera Class
To use the Camera class, you simply create a new instance of it and call its methods to control the camera.
$cam = new Camera;
$cam->Set_Speed(1);
$cam->Toggle_Alarm(0,6);
$cam->Toggle_Alarm_On_Sound(0,5);
$cam->Activate_Camera_Macro(1);
$cam->Go_To_preset(39, 1);
$cam->Go_To_preset(31, 3);
In conclusion, this PHP class provides a simple and effective way to control an IP camera.
Full script
class Camera
{
private $adress;
private $auth;
private $username;
private $password;
private $positions;
public $speed;
function __construct()
{
$this->adress = "http://1.2.3.4:8080";
$this->auth ="CAMPWD";
$this->wait_until_next = 4;
$this->Debug = 1;
/*
Preset 1 to 32 starts from 31 to 93, & time in seconds on each preset.
Where 30 is the set preset, same for 32,34,36,38 etc..
*/
$this->positions = @array('31','33','35','37');
}
public function Activate_Camera_Macro($times)
{
for($x=1; $x<=$times; $x++)
{
echo ($this->Debug == 1 ? "This is loop nr: $x \n" : NULL); // Debug
$this->y=0;
foreach($this->positions as $pos)
{
$this->Go_To_preset($pos, $this->wait_until_next);
}
}
}
public function Go_To_preset($position, $time)
{
$this->y=$this->y+1;
echo ($this->Debug == 1 ? "$this->y. Position $position for $time sec. \n" : NULL); // Debug
$this->Send_CMD_To_Cam("decoder_control.cgi?command=$position");
sleep($time);
}
public function Set_Speed($speed)
{
$this->Send_CMD_To_Cam("set_misc.cgi?ptz_patrol_rate=$speed");
}
public function Toggle_Alarm($toggle, $sensitivity)
{
$this->Send_CMD_To_Cam("set_alarm.cgi?&motion_armed=$toggle&motion_sensitivity=$sensitivity");
}
public function Toggle_Alarm_On_Sound($toggle,$sensitivity)
{
$this->Send_CMD_To_Cam("set_alarm.cgi?sounddetect_armed=$toggle&sounddetect_sensitivity=$sensitivity");
}
private function Send_CMD_To_Cam($command)
{
$ch = curl_init("$this->adress/$command&$this->auth");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
}
}
$cam = new Camera;
$cam->Set_Speed(1);
$cam->Toggle_Alarm(0,6);
$cam->Toggle_Alarm_On_Sound(0,5);
$cam->Activate_Camera_Macro(1);
$cam->Go_To_preset(39, 1);
$cam->Go_To_preset(31, 3);
Buy Me a Coffee