Authenticate Foscam IP camera for HTTP Post Commands


(Ronny Carr) #1

So, I have the Foscam API documentation, and I’ve started creating a device type for the camera I have. The only thing right now that I am trying to figure out is how to authenticate the camera with SmartThings so that I can send commands to it. How is this done? I can’t for the life of me figure this out. Any help is greatly appreciated as this is something I would really love to add to my current SmartThings setup.


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #2

Can you share that documentation somewhere?

In the meantime, I think, if Foscam does not support oAuth, then you may have no choice but to store the username and password in the Device… when SmartThings rolls out “Device Parameters”.

Prior to Device Parameters, you may need to store them in the Handler (ick)… I wonder if there is a public oAuth storage service available,… let’s search and discuss!


(Ronny Carr) #3

You can grab the document from here- https://dl.dropboxusercontent.com/u/200006/foscam_ip_camera_cgi.pdf

Thanks for the response, this should be interesting. I just can’t believe no one has worked on this within the community yet.


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #4

Thanks, Ronny!

There are so many concurrent “Projects” going on here, and, in reality, a relatively small number of “active” Developers & Makers.
Though the Project list is growing, most of them are stale. It would be interesting to get an activity count by user on the Forum topics!

But there are other brands of IP cam’s that are being tested (e.g., Dropcam, I think).

That Foscam documentation only discusses authentication via CGI (get/post) – probably only in cleartext and definitely no mention of token based authentication. I think many folks with Foscams are not happy with the rather weak security.

Nevertheless, it would be best to avoid making the situation “worse”! For development and testing of non-authentication related functions, I still recommend hard-coding your specific attributes (IP address, port, username, password) into the Device Handler – at least until the aforementioned “Device Parameters” have been rolled out in an upcoming release (soon, I think; but ask build@smartthings.com).

If you / we enjoy the challenge and don’t want to find an expert on oAuth, here are a couple of starting points to research. In my mind, the situation requires finding an oAuth compliant storage service (perhaps something like Google Drive?) where the User can voluntarily store their username and password in a file. The Device Handler can then use the user’s oAuth SmartThings token to access that file (and only that file).

  • http://www.oauth.net
  • http://phpmaster.com/creating-a-php-oauth-server/
  • https://developers.google.com/storage/docs/authentication

A less secure variation on this is to use the “share via unique private link” option of such storage providers. This generates a random URL that can read the file; but if this random URL is leaked or sniffed, the privacy is compromised.

So… oAuth storage is preferred; and though Foscams themselves won’t do token based authentication, at least the possibility exists use oAuth secured storage, with some tinkering.

This service, however, could be offered by SmartThings themselves as a part of the Device Parameters. In other words, while SmartThings prefers not to be in the business of storing usernames and passwords, since tokens are much more secure; they may relent in the case of devices like Foscam that do not offer token authentication. I presume that data entered as Device Parameters, while not encrypted, is not accessible to unauthorized users.


(Solardave1) #5

Shoot an email to danny@smartthings.com. He’s apparently working on Foscam and Dropbox camera integration.


(Snoopbuild) #6

@ronnycarr & @tgauchat:

About two months ago, I wrote some SmartApps that interface with Foscam IP cameras and their published SDK. (Both the SDK for HD cameras and VGA cameras.) A pretty sweet app, actually. The camera tilts/pans when motion is detected so it can grab the action. (It also works when a sensor opens/closes, or the sensor is moved.) After a period of time or inactivity, it moves back to its default viewing preset. I mentioned the SmartApp here: http://build.smartthings.com/forums/topic/using-ethernet-port-on-smartthings-from-a-smartapp/

However, in dealing with this, I discovered an error with the way the SmartThings httpGet method interfaces with the Foscam API/SDK. When running the SmartApp in the IDE, the httpGet would be received by the Foscam, and it would work. In essence, it half-way crashed, but the camera still did was it was programmed to do. However, it would generate an error (“java.io.IOException: Stream closed”) in the console anytime the httpGet method was called. When self-publishing the SmartApp and after installing it via smartphone, when the httpGet would trigger, it would generate an error (“null”) in the console. This httpGet, however, was not received by the Foscam, because it would not respond to the command it was sent. In essence, it completely crashed, and nothing happened.

The exact same code was running in both the IDE and in the installed and configured SmartApp; however, two different responses. I created a support ticket and discussed in detail with both Dan and Danny. Danny asked for my Foscam model and said one was on the way so they could try to duplicate the issue. This was over a month ago, and that’s where we are with that.

What I did as a temporary workaround: I created my own “proxy” on a web server in the cloud that could receive the httpGets from SmartThings, and then it forwards that to my Foscams. My server doesn’t crash when talking to the Foscam SDK/API, so that worked well, but isn’t a great solution, particularly because it requires another machine in the chain. And if/when SmartThings brings LAN communication to the SmartHub, and if they don’t solve this issue with the error in communicating with the Foscam, I’ll then have to bring said webserver/proxy to function inside my LAN. In other words, I’ll need a server in my house for this to work == not desired.

Foscam communications also brings the issue with security, as Foscam credentials are sent in the querystring. I’m currently opening a port on a firewall, and usernames/passwords are being sent in clear text in the querystring in a NON-HTTPS call. The Foscam firmwares are moving toward an HTTPS model, and my Foscam HD camera does support HTTPS, and this might help with securing the http communication, however, these certs are self-signed, and SmartThings doesn’t support self-signed certs. Which then brings us to this forum topic: http://build.smartthings.com/forums/topic/httpget-with-https-and-self-signed-certificate/

So all this to say, yes, some community members (that is, me) are monitoring this and hope to find some responses from the SmartThings team and are looking for community-led solutions as well. Foscams provide a great price point for IP cams are have WiFi connectivity, and I think a SmartThings-Foscam marriage would be a great marriage for both platforms. It is extremely cool watching my app work in the IDE, and I’d love to find an answer to this so more could use this great combination I threw together with pan/tilt cameras and motion sensors and SmartThings.

However, right now, something is going on with the SmartThings httpGet method and the Foscam that is causing the method call to crash, and I can’t put my finger on it.

FYI: My Foscam’s are running up to date firmware. I’ve tested the exact same http verb methods with the Fiddler debugging proxy app, and all works fine.

If either of you (or anyone for that matter) has successfully written a Device Handler to receive http commands and deal with communicating inside a LAN, I would love to see some code. I asked a related question in the “ethernet port on smartthings from a smartapp” post mentioned above, but I haven’t had anyone respond.

Looking forward to collaborating…


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #7

@snoopbuild: (Thanks).

I believe that local IP connectivity (via the LAN attached to the ethernet) is coming very soon by firmware upgrade. (I don’t think the hub has it’s own WiFi; probably doesn’t need it unless standard wifi routers can’t handle “thing” IP packets for some readon).

So… the ability to talk to devices and/or servers on the LAN will be here soon, and that should make this testing of any IP device (and customer IP servers and proxies) easier.


(Ronny Carr) #8

Just FYI, I got this working using device preferences. I’m doing some work on the device type tonight, then I’ll post a link to the code!


(Ronny Carr) #9

Here is the rough start to the device type. I’m sure there’s a lot that could be cleaned up but it works!

https://gist.github.com/ronnycarr/6218175

As you can see lots to do on this to make it fully functional, but this is the main functions I wanted. I’m triggering the motion alarm emails on when I leave and off when I arrive. Next steps, get poll and tiles working for function status and direct control!


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #10

@RonnyCarr: Thanks! Will test soon and post any questions / suggestion here, unless you prefer private message.

…CP.


(Ronny Carr) #11

Looking forward to it! Here is fine, I have also created a project for it.

http://build.smartthings.com/projects/foscam/


(Csader) #12

@ronnycar Do you know what Foscam models this works with? That is, would it work on the FI8908W?


(Csader) #13

Sorry for the double post, meant to tag @RonnyCarr.


(Ronny Carr) #14

@csader, this would work with all of the MJPEG cameras I think. So, the one you mention should work fine. The H.264 cameras use a different CGI kit.

I don’t have a H.264 cam so I’m not that interested in doing all the work, but ideally the device type would eventually be able to handle both.


(Csader) #15

Thanks @RonnyCarr. So this is only functioning as a Motion Detector at this point, correct? Any thoughts about what’s needed to get photo/video capture working?


(Ronny Carr) #16

It’s not so much a motion detector at this point, really you can only toggle the camera’s motion alarm and alarm emails (the ones with photos) from a smartapp. I haven’t had much time to work on this to see what we could actually do with it.

I know there should be a way to post an image to the log in the mobile app, I saw this in a video from SmartThings a while back. Not really the best way to handle it in my opinion. What I would like is a tile that displays the last image taken, and when you tap it would update the image. Not sure if that’s actually possible or not. A live feed may not be possible within the SmartThings app.

I think tiles for this may be a ways off, I have no real idea on how to implement poll() for the camera. I know the CGI command to get all current statuses, just not sure how to parse the return.


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #17

Take a look at the example code for DropCam (ummm… don’t make me look it up; I’m sure it is posted in one of the threads, or IDE Example, or Project, or ?).

They definitely had it taking snapshots that displayed on the UI.


(Zach Naimon) #18

Here’s a sample dropcam app:


/**
 *  Motion Dropcam
 *
 *  Author: zach@beamlee.com
 *  Date: 2013-07-30
 */
preferences {
	section("Pick your motion sensor.") {
		input "motion", "capability.motionSensor", title: "Motion Sensor", multiple: true;
	}
        section("Pick your Dropcam.") {
    	        input "dropcam", "capability.imageCapture", title: "Dropcam", multiple: true;
        }
}

def installed() {
        initialize()
}

def updated() {
	unsubscribe()
	initialize()
}

def initialize() {
	subscribe(motion, "motion.active", motionActive)
}

def motionActive(evt){
	capture()
}

def capture(){
	dropcam.take()
        sendPush("${dropcam.label} just took a picture.")
}

(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #19

Actually looking for the Device Handler for DropCam, please, since that must define “take” (etc.).

thanks…


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #20

Thanks!

As usual, discovering new methods… documentation?

e.g.,
carouselTile("cameraDetails", "device.image", width: 3, height: 2) { }

As a “Developer/Maker/Backer”, documentation of these actually available methods & attributes is the sort of thing that helps avoid the “put SmartThings away on the shelf until it matures or a competitor outruns them” attitude that I’ve said expressed lately.

Doesn’t have to be super detailed, at first, just methods, parameters, attributes, and a couple of lines of description or usage…