0

I'm making a app for my website for visitors to look at the camera. I own a PT( pan-tilt) camera which can be operated by using url's. I want my camera to move randomly at prefixed times ( like every 5 seconds a different position) and in the background, so i will move without any operator but i can't seem to figure out how to make it movable automatically. The manufacturer works with CGI commands like:

myip:myport/decoder_control.cgi?command=39&user=user&pwd=password

(this code makes it go to preset 1).
How can i make the camera move with this command using serverside php, making it move after 5 seconds?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • What have you tried so far? Is that URL of public access? Are the users supposed to be able to control the camera, or you want the server to do it automatically? – Pietro Saccardi Jul 12 '14 at 20:00
  • I am not an experienced javascript developer and i have only tried to make the user control the camera, wich failed too. The url is of public acces, via port forwarding. you should be able to see my cam on weahercam.bugs3.com. And i want the server to do it automatically. thanks for the reply btw. – MetNL Development team Jul 12 '14 at 22:12
  • So you are looking for a way to "visit" that command URL via JavaScript, in such a way that users can control the cam, is that correct? JS runs (basically) client side, so for the server you'll need another solution. – Pietro Saccardi Jul 12 '14 at 22:29
  • I see now i migth made some confusion in my post, so i'm going to edit it to make it more clear. – MetNL Development team Jul 13 '14 at 11:33
  • I hope the answer is useful, but if I may give you a suggestion, you should try to break your problem into smaller, elementary tasks, and solve each technical challenge independently. All the pieces you need (HTTP requests in JS/PHP, Cron jobs, scheduling) are very well documented online; there's plenty about running CGI script also (which is actually your problem: whether it controls a camera or an chopper isn't relevant). Finding the program flow for a given task and simplifying the problem is among the very first operations one does before starting coding. – Pietro Saccardi Jul 13 '14 at 12:30

1 Answers1

0

Running the CGI script from PHP.
You can perform an HTTP request from PHP, that would load the URL corresponding to the command, causing the camera to change position. Some ways of achieving this:

If you just need to perform a GET request, and the response is empty (e.g. you just need to check the +200 OK code) or contains some very simple data (e.g. a string), then file_get_contents is more than enough.
If you don't have any background on how HTTP requests work, Wikipedia could be a good introduction; especially if later on you have more complex CGI commands to send to your PT Cam.

Make the camera move every 5 sec.
This is a completely different matter. The problem here is run PHP code periodically and automatically.

  • You can schedule the PHP script to be executed, using a Cron job (Cron, crontab) and this questions explains you how. BUT Cron's minimal time resolution is one minute; also moving a camera every 5 seconds doesn't really sound like schedule a job, sounds more like it should be handled by a system service.
  • What you could do, is moving the camera from the PHP script users use to watch: store the last update time on a file/database, and if the elapsed time is >5s, run the CGI script.
    This would keep your camera still unless someone is actually watching. Other problems might arise, for example what if many users are visiting the same page and your server serves the request simultaneously? You might get several consecutive commands sent to the camera. Moreover, while the users are watching, staying on your PHP page, you must again find a way of moving the camera every 5".

A possible solution.

  1. Create a PHP script that, when loaded, runs the CGI command only if at least 5s have passed since the last call (by storing the time of the last call).
  2. Create a client page for your users, that, via JavaScript, loads the PHP script every 5s. Look for JavaScript GET request, you will find enough information to fill a book.

Again, this would generate a lot of traffic on your webserver, just for those five seconds of panning. My suggestion is that the movement should be handled by some server side program, not script.

Community
  • 1
  • 1
Pietro Saccardi
  • 2,602
  • 34
  • 41