Questions for writing device handler for wifi device


(Spinn360) #1

So, basically I have a device setup that when I go to a specific IP:port address it is running a server that gives JSON info.

What do I do to make a device handler for this device? Do I need to make a smartapp as well?

Obviously code written in groovy on my computer isn’t directly transferable to smartthings, but I have tested it and it works.
Here is the code I can run on my computer and it works fine this way, but I am having trouble figuring out how to make a GET request for the JSON data in smartthings.

//import
import groovy.json.JsonSlurper



//print the url to get full JSON data
//println "http://192.168.2.127:3003/getData?authToken=vhbyvw".toURL().text;

def i = 100
//Timer
Thread y = new Thread();

try{
while(i>0)
{
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText('http://192.168.2.127:3003/getData?authToken=vhbyvw'.toURL().text)
//print the light sensor data
println"Light Sensor: "+object.lightValue
String movement = object.movement
//check if motion is false and print false and also print correct temp data
if(movement=="false"){
  println"No Motion"
}
// check if the motion is true and print true and also print correct temp data
if(movement=="true"){
  println"Motion"
}
println (object.temperature+" F")

//timer run for 1 second
y.sleep(1000)
i--

}
}catch(ConnectException ex){
  println "Caught ConnectException, check that turned on and developer mode is active"
}

I have been trying to figure this out for almost a week and I just feel like I am getting lost. Any help or pointers is greatly appreciated.


#2

Check this out.

http://docs.smartthings.com/en/latest/device-type-developers-guide/other-available-apis.html

Explains how to make http requests


(Spinn360) #3

Thanks Gopack2! I am checking it out… still have yet to get it, but I hope I am getting closer.
Even when I follow the examples I never get anything to show in the logs when I run simulator… I feel discouraged, but I will try try try again.


(Spinn360) #4

Question:
Does the http request go in the parse(){} section of device handler?

Sorry if I sound like an idiot.


(Spinn360) #5

Does this work on internal (LAN) http requests as it is labeled external, I just don’t want to read to far into this book as I have done many other books of documentation for smartthings. Again sorry if I sound like a complete noob/idiot. I am new to creating a device handler. I have only really modified a couple, and half of the document examples for device handlers say the code is from a smartapp… which is confusing to say the least. If there is a tutorial for creating device handlers that goes over creating LAN http requests and where the code should be placed in the device handler that would help a lot… for now I think I am going to take a break.


(Tony Fleisher) #6

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


(Spinn360) #7

Thanks Tony. Now I’m to the part where a service manager is required. I found a generic one online but in order for it to work it needs the device urn. Any info on urn’s that you know of and want to share? I can’t figure out where a URN is informed to anyone on any device. Is it like a MAC address or IP Address??? Thanks again