Basic httpGet not working

smartapp
webservices

(Ivan Stanev) #1

I have just started toying around with creating an app for my hub. I just want to periodically make a web request to my server, however it seems that this isn’t working at all because no action is taken (the server isn’t receiving anything). My function is just this (with the server IP hidden):

def initialize() {
	// TODO: subscribe to attributes, devices, locations, etc.
    try {
	while (1 == 1) {
        httpGet('**.**.**.**:****/path')
        
        pause(30000)
    }
    } catch (e) {
        log.error "something went wrong: $e"
    }
}

Any idea what is going on? Do I need special permissions to run this?


#2

Are you using a local IP (i.e. 192.168…")?
httpGet(‘192.168.1.233/path’) - wont work
httpGet(’‘myhome.no-ip.org/path’) - will work.

For local ip try sendhubcommand (or something like that)


(Ivan Stanev) #3

No, it’s a public IP, 54.229.etc…


(Minollo) #4

This may work once, but the initialize() call will quickly fail; your function callbacks in a smartapp (or device handler) have a limited time to perform operations (30 seconds?); your idea of having an infinite loop with requests every 30 seconds will not work.
Your best option is probably to schedule a time based event happening every minute or so (timed events are not too reliable, but timed events with intervals < 60 seconds are totally unreliable).


(Ivan Stanev) #5

Ah I see. Well I reworked my code to this, and it still isn’t calling every 5 minutes or so, not even once.

def handlerMethod() {
    try {
    	httpGet('*.*.*.*:****/path')
        
    } catch (e) {
        log.error "something went wrong: $e"
    }
}

def initialize() {
    runEvery5Minutes(handlerMethod)
}

(Minollo) #6

Never tried httpGet without specifying a closure; maybe you can try something like:
httpGet(url) {response -> log.trace response}

What do you get in the log? Maybe you can add a log.trace just before httpGet, to make sure your method is being called?


(Jim Anderson) #7

Right, the response will be sent to the closure passed to httpGet().

http://docs.smartthings.com/en/latest/smartapp-developers-guide/calling-web-services-in-smartapps.html#handling-the-response


(Ivan Stanev) #8

Thank you very much! It is working now :slightly_smiling: