Raspberry Pi Python script sleep

I’m creating a device handler leveraging WEBIOPI which many have done. It works perfect except that I would like to implement a sleep period of 5 seconds in the python which I did. The problem is that when the tile in the smartthings app is pressed it triggers the Pi without issues and performs the sleep() but the device handler never receives a response in the parse method.

If I remove the sleep from the python script smartthings gets the data back to the parse method as expected.

I’m assuming that since there is a delay the parse method is expecting the result back immediately or possibly the sleep kills the response back to the parse method.

I can test the api from postman and it works as expected. If I send a post to webiopi it runs the script, lights up some leds for testing and then returns the appropriate data in a json response.

I was trying to handle the wait on the python side. Should I handle the wait on the smartthings side? The reason I need the wait is because I need the GPIO pin on for a certain amount of time and then turn off.

Are you seeing any errors in ide live logging?

Negative. It shows the hub action run in IDE but if never gets to parse. I saw it work once which I assume was because the request with the RPi was fast enough to not reach a timeout. Because if I take the sleep out of the python script this works with no issues. I remember another thread where someone was asking if hubAction had a timeout of 20 seconds. I’m assuming my sleep in python is causing the hubAction return to timeout.

What method are you using to call the script?

Webiopi, calling via hubAction.

My guess is that you’re device handler is timing out waiting for a response back from the RPi. By adding that sleep statement, your making ST think the device was unresponsive.

Instead of adding the sleep to the Python code on the RPi that the Webiopi is calling, why not have that script spawn off a new python process that does the real heavy lifting (i.e. the sleep command plus other stuff.) This way, the Webiopi Pythin script can return a result immediately to ST after spawning off the new python script in its own process/thread.

I moved the delay into the device handler and it worked. I may also try your approach and and see what fits best.

1 Like