Controlling a Foscam R2 PTZ camera via an AppleScript

Posted on
Sat Aug 20, 2016 4:26 pm
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Controlling a Foscam R2 PTZ camera via an AppleScript

This script controls the Foscam R2 HD PTZ camera. It's pretty simple to implement. Use it as an external script, NOT as an embedded script. Otherwise the stop actions for direction and zoom control will run to their limits.

1. Create a variable name for each camera you want to use. In this example it is named "Camera1_Action". This will need to be changed for each camera you use, and you will also need to create a trigger for each camera so it will fire off the correct variable. Since you probably use the same username and password for every camera, just create "Camera_Password" and "Camera_User" variables and put your password and username in those.

2. Create a control page such that the buttons modify the variable "Camera1_Action"with the appropriate command variable when the button is pressed. Note that you need to use the exact spelling for your commands as are listed in the script.

3. Update this script for each of your cameras with correct IP address. Also update the various instances of "Camera1_Action" in the script with your variable's name. (Could probably do this in a simpler fashion...)

4. Create a trigger for each camera that fires when variable "Camera1_Action" (or your camera's name) changes, with this script as the action. It all should work!

note that by following the schema below it shoud be easy to add new commands. If you do so, please update the script in this thread. Also it should be easy to modify this script for other camera brands.


Code: Select all
(*


This script controls the Foscam R2 HD PTZ camera. It's pretty simple.
1. Create a variable name for each camera you want to use. In this example it is named "Camera1_Action". This will need to be changed for each camera you use, and you will also need to create a trigger for each camera so it will fire off the correct variable. Since you probably use the same username and password for every camera, just create "Camera_Password" and "Camera_User" variables and put your password and username in those.

2. Create a control page such that the buttons modify the variable "Camera1_Action"with the appropriate command variable when the button is pressed. note that you need to use the exact spelling for your names as are listed in the script.

3. Update this script for each of your cameras with correct IP address. Also update the various instances of "Camera1_Action" in the script with your variable's name. Could probably do this in a simpler fashion...

4. Create a trigger for each camera that fires when variable "Camera1_Action" (or your camera's name) changes, with this script as the action. It all should work!

5. Finally, due to timing issues with sticking an "x" in the variable to have it reset for repeated commands (e.g. left, left,left) the IR on/off commands require a separate URL send routine.

note that by following the schema below it should be easy to add new commands. If you do so, please update the script in this thread. Also it should be easy to modify this script for other camera brands.

created by Ham Williams August 2016

*)


using terms from application "IndigoServer"
   tell application "IndigoServer"
      
      set actionOrdered to value of variable "Camera1_Action" as string
      --log actionOrdered
      
      
      --Cardinal Directions. Automatically stops the directional movement via delayed command at the bottom.
      set actionDirections to {"Left", "Right", "Up", "Down"}
      if actionDirections contains actionOrdered is true then
         set theAction to "ptzMove" & actionOrdered & "'"
         --log theAction
         --set value of variable "Camera1_Action" to "x"
      end if
      
      
      --zoom in, out. Automatically stops the zoom via delayed command at the bottom.
      set actionZoom to {"zoomIn", "zoomOut", "zoomStop"}
      if actionZoom contains actionOrdered is true then
         set theAction to actionOrdered & "'"
         --log theAction
         --set value of variable "Camera1_Action" to "x"
      end if
      
      
      --Go to Preset.
      set actionPreSet to {"TopMost", "RightMost", "LeftMost", "BottomMost", "WideView"}
--I added WideView. Modify the list to add your custom preset.
      if actionPreSet contains actionOrdered is true then
         set theAction to "ptzGotoPresetPoint&name=" & actionOrdered & "'"
         --log theAction
         --set value of variable "Camera1_Action" to "x"
      end if
      
      --Need to place all non-IR commands in this list so they will execute with proper timing.
      set actionListAll to {"Left", "Right", "Up", "Down", "zoomIn", "zoomOut", "zoomStop", "TopMost", "RightMost", "LeftMost", "BottomMost", "WideView"}
      if actionListAll contains actionOrdered is true then
         --log actionOrdered & "  the list works"
         
         set theIP to "'http://XX.XX.XX.XXX:80/cgi-bin/CGIProxy.fcgi?" --change your ip and command string here
         set thePW to the value of variable "Camera_Password"
         set theUser to the value of variable "Camera_User"
         
         set theURL to theIP & "usr=" & theUser & "&" & "pwd=" & thePW & "&cmd=" & theAction --rearrange the order of the username/password and command string if necessary for your camera.
         --log theURL --comment this out once it is working
         set curlURL to "curl -f" & " " & theURL
         
         set shellscript to curlURL
         --log shellscript -- commment this out once it is working
         do shell script shellscript
         set value of variable "Camera1_Action" to "x"
      end if
      
      
      --This is a distinct URL send for IR LED open and close. Otherwise it does not work correctly when run as an external script.       
      set actionIRonOff to {"openInfraLed", "closeInfraLed"}
      if actionIRonOff contains actionOrdered is true then
         set theAction_IR_Only to actionOrdered & "'"
         --log theAction

         set theIP to "'http://XX.XX.XX.XXX:80/cgi-bin/CGIProxy.fcgi?" --change your ip and command string here
         set thePW to the value of variable "Camera_Password"
         set theUser to the value of variable "Camera_User"
         
         set theURL to theIP & "usr=" & theUser & "&" & "pwd=" & thePW & "&cmd=" & theAction_IR_Only --rearrange the order of the username/password and command string if necessary for your camera.
         --log theURL --comment this out once it is working
         set curlURL to "curl -f" & " " & theURL
         
         set shellscript to curlURL
         --log shellscript -- commment this out once it is working
         do shell script shellscript
         delay 2
         set value of variable "Camera1_Action" to "x"
      end if
      
      
      
      --Send "stop" URL function for directions and zoom
      delay 0.5
      if actionDirections contains actionOrdered is true then
         set theAction to "ptz" & "StopRun" & "'"
         set theIP to "'http://XX.XX.XX.XXX:80/cgi-bin/CGIProxy.fcgi?" --change your ip and command string here
         set thePW to the value of variable "Camera_Password"
         set theUser to the value of variable "Camera_User"
         
         set theURL to theIP & "usr=" & theUser & "&" & "pwd=" & thePW & "&cmd=" & theAction --rearrange the order of the username/password and command string if necessary for your camera.
         --log theURL --comment this out once it is working
         set curlURL to "curl -f" & " " & theURL
         
         set shellscript to curlURL
         set theAction to "ptzMove" & "Stop" & "'"
         do shell script shellscript
         set value of variable "Camera1_Action" to "x"
         
      end if
      
      
      
      if actionZoom contains actionOrdered is true then
         set theAction to "zoomStop" & "'"
         set theURL to theIP & "usr=" & theUser & "&" & "pwd=" & thePW & "&cmd=" & theAction --rearrange the order of the username/password and command string if necessary for your camera.
         --log theURL --comment this out once it is working
         set curlURL to "curl -f" & " " & theURL
         
         set shellscript to curlURL
         set theAction to "ptzMove" & "Stop" & "'"
         do shell script shellscript
         set value of variable "Camera1_Action" to "x"
         
      end if
      
      
      
   end tell
end using terms from
Last edited by hamw on Sun Aug 28, 2016 8:31 pm, edited 2 times in total.

Posted on
Mon Aug 22, 2016 11:33 am
jay (support) offline
Site Admin
User avatar
Posts: 18185
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Controlling a Foscam R2 PTZ camera via an AppleScript

Thanks for the contribution.

[MODERATOR NOTE] moved to the User Contributions/AppleScripts forum.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sun Aug 28, 2016 8:23 pm
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Controlling a Foscam R2 PTZ camera via an AppleScript

Modified and replaced the script due to some timing issues with the IR command. I believe that since the IR lens is a mechanical process, it needs a couple of seconds to complete prior to issuing the variable = x substitution. Let me know if anyone has success with the script.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 0 guests