Saturday, November 24, 2012

Foscam - clicking noise, turning camera on /off

So I bought two Foscam H.264 cameras (Model FI8608w) as well as one MJPEG model (the popular FI8910w) to attach them to my Synology DiskStation DS 413j.

Basically I want to use the cameras not for surveillance but for motion detection. Instead of having a motion sensor inside the house I want to be notified as soon as someone appears in front of the windows that the cameras monitor.

For this I need to turn the cameras ON when I leave the house and back OFF when I return.

I managed to to this in DiskStation's surveillance manager (see above post) using a shell script that I can call from my iPad. I put bookmarks to the scripts on my home screen.

One problem remains: the cameras themselves are still on and even more annoying: when I'm watching TV, the IR filter sometimes clicks on and off which makes a very audible noise.

I found this really impressive insight into the scheduling feature, however this does not seem to be available on my 8608 models, only the 9810 can be turned off as described in this forum post.

I tried everything I could think off to turn the two other cameras off remotely (a remotely controlled outlet would have been my last option, but I did not want to buy more stuff). I found this excellent blog entry and from here my way to the Foscam H.264 cameras CGI API documentation which is actually pretty good and lets you do many things with the camera - but not turning it on and off.

Finally my solution: I still can't turn the camera off, but I can turn it all the way up and against the wall - which for me solves my problems. The clicking stops because the camera is not facing my TV screen any more and the wife does not feel like she's being constantly monitored. :-)

When I turn the cameras on, they swivel back into their guarding positions which looks kind of cool actually, so I like it. :-)

To accomplish this, I used the regular web frontend to program two preset positions into each camera, one facing the wall and one facing the windows.
Then you can get them to go to their presets using this call:

wget -O- --http-user=admin --http-password=PASSWORD "http://CAM-IP/cgi-bin/hi3510/preset.cgi?-act=goto\&-number=1"
number is the preset position to use, they start at 0 so it's easy to try which number you need to set.



Enable / disable cameras on Diskstation from Shell

I finally managed to enable / disable the cameras connected to my Synology DiskStation 413j running DSM 4.1 from a shell script!

The problem with existing scripts I found in forums was, that Synology improved the logon procedure, adding encryption of passwords (using "encryption.cgi") which makes it harder to log in using simple "wget" calls.

The client requests a key from encryption.cgi which is being used inside JavaScript to encrypt the password and send a hash to the server. Cleartext passwords no longer work (at least I couldn't get them to work anymore):

I used WebScarab proxy to monitor the connection between my browser and the DiskStation. This is what the call to encryption.cgi returns:


And this is what the following call to login.cgi looks like:



So it would be necessary to get the JavaScript code that encrypts the password, see what it does and try to use or reimplement this somehow. Too complicated.

However, Synology introduced a nice CGI API that is really straightforward and well documented and allows you to logon using clear text passwords (over SSL encrypted connection if needed) - but lacks the feature to enable / disable the cameras! :-(

Finally, combining a logon call to the new API, saving the cookie, then calling the main page ("index.cgi") and then setting the cameras turned out to work, so this is what works at least for me:

 Turning cameras ON:

wget --keep-session-cookies --save-cookies syno-cookies.txt -O- "http://DISKSTATION:9900/webapi/auth.cgi?api=SYNO.API.Auth&method=Login&version=1&account=admin&passwd=PASSWORD"

wget --load-cookies syno-cookies.txt -O- "http://
DISKSTATION:9900/webman/3rdparty/SurveillanceStation/cgi/index.cgi"

wget --load-cookies syno-cookies.txt -O-  "http://DISKSTATION:9900/webman/3rdparty/SurveillanceStation/cgi/camera.cgi?_dc=1353771235677&action=cameraEnable&UserId=1024&idList=1"

wget wget --load-cookies syno-cookies.txt -O- "http://
DISKSTATION:9900/webman/3rdparty/SurveillanceStation/cgi/camera.cgi?_dc=1353771235677&action=cameraEnable&UserId=1024&idList=2"

Turning cameras OFF:

wget --keep-session-cookies --save-cookies syno-cookies.txt -O- "http://DISKSTATION:9900/webapi/auth.cgi?api=SYNO.API.Auth&method=Login&version=1&account=admin&passwd=PASSWORD"

wget --load-cookies syno-cookies.txt -O- "http://
DISKSTATION:9900/webman/3rdparty/SurveillanceStation/cgi/index.cgi"

wget --load-cookies syno-cookies.txt -O-  "http://DISKSTATION:9900/webman/3rdparty/SurveillanceStation/cgi/camera.cgi?_dc=1353771235677&action=cameraDisable&UserId=1024&idList=1"

wget wget --load-cookies syno-cookies.txt -O- "http://
DISKSTATION:9900/webman/3rdparty/SurveillanceStation/cgi/camera.cgi?_dc=1353771235677&action=cameraDisable&UserId=1024&idList=2"