SmartThings Labs and LAN Devices

idefix:

I had a similar idea. I wanted to trigger my ispy server (on my home lan) to take a snapshot on the camera covering the front porch when motion is detected on the front porch. I hacked the following together using tips from others on the forums and code from other apps. I don’t really understand how it all works but it does seem to work. The hardest part for me was getting the correct syntax of the http get string.

/**
 *  Take a Snapshot When There's Motion
 *
 *  Author: Eric Miller (with much help from many others)
 */
preferences {
	section("When there's movement..."){
		input "motion1", "capability.motionSensor", title: "Where?"
	}
	section("Take a snapshot of..."){
		input "camera1", "camera", title: "Camera number?"
        // Camera number must be the ispy camera number shown on the ispy console
	}
}

def installed()
{
	log.debug "Installed with settings: ${settings}"

	subscribe(motion1, "motion.active", motionActiveHandler)
}

def updated()
{
	log.debug "Updated with settings: ${settings}"
 
	unsubscribe()

	subscribe(motion1, "motion.active", motionActiveHandler)
}

def motionActiveHandler(evt) {

	log.trace "$evt.value: $evt, $settings"
	log.debug "$motion1 detected motion, initiating snapshot on $camera1"

	def deviceNetworkId = "C0A80112:1F90"  //  "192.168.1.18:8080"
	def ip = "192.168.1.18:8080"
	// This is the ip of the PC running the ispy application in the home
    
  	sendHubCommand(new physicalgraph.device.HubAction("""GET /snapshot?ot=2&oid=$camera1 HTTP/1.1\r\nHOST: $ip\r\n\r\n""", physicalgraph.device.Protocol.LAN, "${deviceNetworkId}"))

	log.debug location.hubs*.firmwareVersionString.findAll { it }

}

def locationHandler(evt) {
	def description = evt.description
	def hub = evt?.hubId

	log.debug "cp desc: " + description
	//log.debug description
}

Thanks @eric -I was able to use your example to change my foscam preset based on presence using a local URL. My question now is, I am passing a username and password in my URL - is the Command smartthings is passing to the hub secure?

So I have been playing with this stuff for several hours last night and this morning and I can’t seem to get my server to respond to a request. I can hit my server up from multiple devices on the network however my server is not sending or seeing anything from the ST hub.

Here is the simple code I am starting with just to make sure that communication is working between the hub and my server. I am expecting a JSON return from my server at all times. Unfortunately I am getting nothing returned so I can’t even begin to parse anything and or save the information. My server logs show nothing from smart things as well.

def installed() {
    log.debug "Installed with settings: ${settings}"

    initialize()
}

def updated() {
    log.debug "Updated with settings: ${settings}"

    unsubscribe()
    initialize()
}

def initialize() {
    // TODO: subscribe to attributes, devices, locations, etc.
    subscribe(location, null, locationHandler, [filterEvents:false])
    subscribe(app, changeMode)
}

def changeMode(evt) {
    def ip = "10.0.1.3:3000"
    def deviceNetworkId = "0A000103:0BB8" // "10.0.1.3:3000"
    def uri = "/lights.json"
  
    sendHubCommand(new physicalgraph.device.HubAction("""GET $uri HTTP/1.1\r\nHOST: $ip\r\n\r\n""", physicalgraph.device.Protocol.LAN, "${deviceNetworkId}"))
  
    log.debug location.hubs*.firmwareVersionString.findAll { it }
    
}

def locationHandler(evt) {
    def description = evt.description
    def hub = evt?.hubId

    log.debug "cp desc: " + description

    
    //log.debug description
}

my logs are returning nothing:

edff80ea-d766-4cf4-9573-4929d5d661be 8:06:59 AM CDT: debug [000.011.00231]
edff80ea-d766-4cf4-9573-4929d5d661be 8:06:58 AM CDT: debug Executing GET /lights.json HTTP/1.1 
HOST: 10.0.1.3:3000 

I am trying to learn groovy, smart things and playing with the LIFX SDK for http requests all at the same time as I have several WIFI based devices at home that I would like to integrate into ST and LIFX is the first that I figured I would try. Any thoughts?

Ugh. I am sort of an idiot. I had a rogue process running on my server that was killing the requests before they made it to the right web server. Duh. Still not seeing what is expected in the ST logs but at least the hub is communicating with the server.

So I’m having trouble with this. It seems like it needs a kick in the ass before it starts working .

No matter how I trigger my foscam using this code I have to initate the event twice before it will start working. After that it works fine eveytime. An hour or so later it starts all over again.

This is proving problematic whe trying to make things happen on events.

Did that make any sense?

Anyone?

Jim,
Wish I could help. I am noticing some of the same things with my LIFX code. I am seeing in the Logs that ST is trying to send a message out but it takes a couple of tries before it actually does. If I had to guess the Local LAN stuff is more alpha then beta as I am even seeing some of the sonos stuff act really strange as well.

Good to know its not just me!

Here some code to convert an IP:Port to hex code:

private Long converIntToLong(ipAddress) {
	log.debug(ipAddress)
	long result = 0;;
	def parts = ipAddress.split("\\.")
    for (int i = 3; i >= 0; i--) {
        result |= (Long.parseLong(parts[3 - i]) << (i * 8));
    }

    return result & 0xFFFFFFFF;
}

private String convertIPToHex(ipAddress) {
	return Long.toHexString(converIntToLong(ipAddress));
}

private String getDeviceId() {
	def ip = convertIPToHex(settings.server)
	def port = Long.toHexString(Long.parseLong(settings.port))
	return ip + ":0x" + port
}

This weekend I did some investigation of SmartThings and two thing came out of it:

  • I was annoyed by the fact that I couldn’t search trough code’s example of SmartThings, so I wrote a quick PHP script that allow me to download every Groovy script (Device and apps) from SmartThings Web IDE (I could have put the downloaded code on GitHub but wasnt sure of Licensing… so I choose not to). My scripts can be found here https://github.com/zzarbi/smartthings/tree/master/scripts
    This allow me to found out useufull information, such as in Foscam.groovy they use HubAction this way:
def host = getHostAddress()
	def port = host.split(":")[1]
	def path = (port == "80") ? "/snapshot.cgi?user=${getUsername()}&pwd=${getPassword()}" : "/cgi-bin/CGIProxy.fcgi?usr=${getUsername()}&pwd=${getPassword()}&cmd=snapPicture2"

	def hubAction = new physicalgraph.device.HubAction(
		method: "GET",
		path: path,
		headers: [HOST:host]
	)
	hubAction.options = [outputMsgToS3:true]
	hubAction

They don’t seem to be using an hexadecimal representation of the IP:Port. However I couldn’t make this work but to be fair all the code that you have working doesn’t seem to work with me anymore. It worked once to send data on the LAN and never work after.

1 Like

nicolas,
I unfortunately got caught up in life and have not been able to play for the last few weeks, but did take a few moments to look over your git repo and your script. I hope to be able to get some time tonight to play with smart things if the wife doesn’t get in the way. Thanks for the work it does look good. hope I can add to making this work and work over LAN which is ideal.

Anyone know if it is possible to do a sendHubCommand from a DeviceType?

Cheers, Jonathan

@badgermanus - sendHubCommand() is only required within the context of a smartApp. For device types, you may just return the HubAction object.

Does anyone know how the new Carousel tile works (or even what it’s called)?

The blog noted this on the last iOS update and I’d like to use it for my Foscam:

* Feature: New Carousel tile view to show a live video stream.

Has anyone had any luck with callbacks on a LAN device? I’ve got SmartThings talking to my device, but my device can’t talk back to SmartThings… I am obtaining the IP and port of the hub and sending it to my device, and setting up a mapping in the device type, but I’m not getting any response when I try to connect. Anyone find any examples for this?

I’d suggest starting with this new tutorial:

I have looked at that page, and it seems that the LAN device has to use the internet address (https://graph.api.smartthings.com) and cannot communicate over the LAN directly. Is that correct?

From what I’ve seen, yes, you need to use the address you listed rather than a local ip address for callbacks to a SmartApp. I haven’t seen any examples or documentation using a local hub IP address in callback mappings.

Ok, thats unfortunate, but not a deal breaker I guess. Thanks for your input.

LAN callbacks are possible, according to the docs. You have to register your callback as described here:

http://docs.smartthings.com/en/latest/cloud-and-lan-connected-device-types-developers-guide/building-lan-connected-device-types/building-the-device-type.html