Update I just figured out I must use a smart app alongside my device. Sorry folks please ignore my ramblings I’m but a poor noob.
Sorry if this is in the wrong place or I am coming off as the biggest noob ever, but I have been searching and coding for days and cannot figure out how to do what I need to.
Basically I have a device running on Wifi, that when I access its web page all it shows is JSON.
I have written code that runs on my computer in groovy that grabs the information and returns the specific data in the JSON. It runs just fine.
However, I have no idea how to implement it in a device handler. Below is the simple code I used just to test out that this works. I understand it isn’t exactly the best coded, but I am new to groovy, and haven’t done a lot of coding in the past couple of years since I learned Java.
import groovy.json.JsonSlurper
def i=100;
//create thread to handle timer between instances of while loop
Thread y = new Thread();
while(i>0){
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText('http://192.168.1.109:3003/getData?authToken=filvhe'.toURL().text)
println("Motion: " + object.movement);
println("Temp: "+ object.temperature);
println("LightValue: " + object.lightValue);
//timer run for 0.5 seconds
y.sleep(500);
i--;
println "\n";
}
How do I change states of the tiles?
How do I access the URL and receive the JSON data in SmartThings?
Is there a tutorial for noobs? I have read a lot of SmartThings documentation, but I feel like they skipped over some basic things… or I am just completely overlooking something. I get that the def parse(String description) is where I handle the data receieved from the device, but how does that even get the data?
If I could just get an example for the capability of say the motion sensor that would be worth a million hours of trying to decipher partial posts of peoples code. Or if someone could just point out what I am doing wrong, missing, etc that would be awesome.
So right now I have been just trying to get one thing to work. Searching for motion. The JSON object I get back shows true or false for movement, when there is motion or none, but I cannot seem to get anything at all to do anything at all in SmartThings.
I have this so far… and I feel bad that it is all I have, but I have tried many other forms to no avail, and I am positive I need to sit down and read all the documents again and again and again for about a year…
metadata {
definition (name: "Wifi Motion", namespace: "spinn360", author: "Me") {
capability "Motion Sensor"
}
simulator {
// TODO: define status and reply messages here
}
tiles {
standardTile("motion", "device.motion", width: 1, height: 1) {
state "inactive", label:'no motion', icon:"st.motion.motion.inactive", backgroundColor:"#ffffff"
state "active", label:'motion', icon:"st.motion.motion.active", backgroundColor:"#53a7c0"
}
}
def result = new physicalgraph.device.HubAction(
method: "GET",
path: "/getData?authToken=filvhe", //I feel like this is wrong
headers: [
HOST: 'http://192.168.1.109:3003' // and this is wrong too
]
)
def parse(String description) {
log.debug "Parsing '${description}'"
def msg = parseLanMessage(description)
def headersAsString = msg.header // => headers as a string
def headerMap = msg.headers // => headers as a Map
def body = msg.body // => request body as a string
def status = msg.status // => http status code of the response
def json = msg.json // => any JSON included in response body, as a data structure of lists and maps
def xml = msg.xml // => any XML included in response body, as a document tree structure
def data = msg.data // => either JSON or XML in response body (whichever is specified by content-type header in response)
def slurper = new groovy.json.JsonSlurper()
def result = slurper.parseText(json)
assert result.movement =="false"
log.debug("Json received: ${object}")
}