I thought I’d follow up on my post of a little while ago about the drivers for the Trendnet TV-IP400W Pan-Tilt-Zoom camera. I wrote a ruby driver for this thing, which makes it trivial to control the camera from any Ruby application. It’s just a single Ruby file, and though I’ve entertained the thought of figuring out how to pour it into a gem and publish it to the plugin repository, I’d probably only do that if there are a significant amount of people interested in using it and/or contributing to it.

As with the old Perl driver for Zoneminder (which I’ve been told doesn’t work anymore with the latest version of ZM, since the interfaces for the PTZ drivers have completely changed), this driver only controls the pan and tilt functions of the camera. Actually, you can also put it in swing mode and store or access preset positions, but none of the available functions actually do anything with the video stream. The video can simply be accessed at:

http://yourcam/image.jpg to grab a single frame of the current camera view
http://yourcam/video.cgi to get the mjpeg stream from the camera

There’s no real zoom control since the cam only has a digital zoom, which I believe is implemented in the client software only (so there’s no way to actually have it stream a zoomed image to your client).

Okay, onto the example script now. The code below demonstrates how to use all the features of the camera driver.

require 'Ip400.rb'
 
# substitute your host IP address or name and port number here.
# If you have turned off access control, you can simply use:
# ptz = Ip400.new("192.168.0.140")
# if your camera is accessible at a port other than 80, simply
# specify the address like this: "192.168.0.140:90"
ptz = Ip400.new("192.168.0.140", "ruby", "ybur")
 
# go to the home position
ptz.home
sleep 3
 
# pan right 3 steps
ptz.pan(3, Ip400::RIGHT)
sleep 3
 
# pan left 5 steps
ptz.pan(5, Ip400::LEFT)
sleep 3
 
# tilt up 3 steps
ptz.tilt(3, Ip400::UP)
sleep 3
 
# tilt down 3 steps
ptz.tilt(3, Ip400::DOWN)
sleep 3
 
# pan and tilt at the same time
ptz.move(4, Ip400::RIGHT, 2, Ip400::DOWN)
sleep 3
 
# go to 1 of the 24 preset positions
ptz.goto_preset(1)
sleep 3
 
# visit all preset positions with about 5 second intervals
ptz.swing_preset
sleep 20
ptz.stop_swing # stop the swing mode again
 
# go into continuous scan mode (scan left and right with 1 second intervals)
ptz.swing_scan
sleep 10
ptz.stop_swing  # don't forget this!
 
# get the current absolute position of the camera
pos = ptz.abs_position
# store this position at preset 15
ptz.set_preset(pos['pan'], pos['tilt'], "myposition", 15)

These examples should get even a Ruby novice going pretty quickly, and the Ip400.rb driver class can be used in or outside of a Rails app. The camera you’re controlling can be in a different room or even in a different country.
I’m releasing it under the MIT license so it’s open for further hacking. If you end up using this code anywhere I’d love to hear from you, and I’ll release it as a real Ruby plugin if enough people show interest.

Have fun!

Download the above example file and the IP400.rb file.