[BETA RELEASE] URI Switch -- Device Handler for controlling items via HTTP calls

can i use this to run a python script thats on a Pi on my LAN? I have a webserver and a page built with HTML and PHP. on the webpage, when you click/touch a button, the PHP code runs a python script. it works great on the web page, but im trying to integrate it with smartthings. Is there any way to run a python script, either from the page, or by the directory its saved in, with this device handler? ive configured it with the IP of my Pi. I created a URI button in the IDE. the python scripts are saved here:

/var/www/html/scripts and i want to run script relay1.py

i assume this goes in the internal path section, but using that path doesnt work (using /var/www/html/scripts/relay1.py).

Any input? I think this handler should work, but maybe im not setting it up correctly. any ideas?

Thanks!

As long as you can curl your php script that kicks off the Python script, or if you can curl your Python script directly, you can use this tool to do what you’re wanting to do. Keep in mind though, in order for something to be curlable it has to be served up by apache.

For instance, let’s say I have a server running apache with IP 192.168.0.50. Apache is serving up the files & directories in /var/www/html/. So, say I have a file in /var/www/html/scripts/myscript.php, I can test to make sure apache is serving up the file by opening a browser on a computer on the network and go to http://192.168.0.50/scripts/myscript.php

The script should show what it returns and do the things with the stuff. If you can do this, then you would simply put all the correct info in the URI Switch settings. Internal address: 192.168.0.50 On Path: /scripts/myscript.php

Hope this helps.

Hey Mark,
So I ran some tests and found that when I changed the method to POST it did actually POST the data to the script. I am working on changing the script to allow different methods in the settings as well as an “ON” payload and “OFF” payload. I should have it done shortly, just been hectic getting caught back up on work and what not.

Sorry for the delay in response, yes this can be made to do this. As I just mentioned to Mark, I’m working on a new version of the URI switch that will allow for POST and PUT methods.

1 Like

awesome, thanks. i tried this:

10.0.0.11/scripts/relay1.py

and the browser wanted to open the python file. which i assume is a good sign.

so after i installed the device handler, i created a URI switch and configured it with the internal IP of 10.0.0.11 (my Pi) and the internal path is

/scripts/relay1.py

that should be all there is to it? I can only do limited testing here at work, so ill have to test when i get home to make sure. it doesnt look like the pin is changing state, but if the configuration looks right, its probably something related to curling the script, right?

Thanks!

When you say that it wanted to open the python file, does that mean that you saw the source code of the script in the browser? Cuz that would mean you need to configure apache to execute python in the scripts directory.

Something like this might help: http://editrocket.com/articles/python_apache_windows.html
Or this: https://www.linux.com/blog/configuring-apache2-run-python-scripts

It opened a box asking if i wanted to open or save the file, i guess like its trying to download a file. ill check those links and see what i need to do to get it working. i installed apache a while ago, but im sure i didnt configure it to execute the scripts. ill work on that this afternoon. thanks!!

i think im really close. i have a script hello.py saved in the same folder as the rest of my scripts, where it just prints “Hello World”. when i browse to 10.0.0.11/scripts/hello.py, it loads a page that just prints Hello World in the browser. I think thats a success, right?

so then when i change it to 10.0.11/scripts/relay1.py, i get an internal server error.

they are in the same folder so the permissions are the same. both scripts have this at the beginning:

#!/usr/bin/env python
#-*- coding: UTF-8 -*-

#enable debugging

import cgitb
cgitb.enable()

then the hello script just says to print “Hello World”. it works in the terminal and in the browser.
the relay1.py script works in the terminal, but not in the browser. any tips on what to look for? it has to be the contents of the script, right? since they are in the same folder, being run from the same browser on the same computer, everything except the specific script is the same, one works and the other doesnt.

im going to research it more this afternoon, but if you have any tips or tricks to pass along, i would really appreciate it!

One possibility is that you might have two versions of Python installed. Internal Server Error means the script has something in it that is breaking. May want to tail Python logs, not sure where those are.

Change your hello world to:

import sys
print(sys.version)

Go to that page and see what version it is.

Then from terminal run python --version
See if they are the same.

they are both 2.7.9

but after i edited the hello.py script, that will no longer work in the browser. i restarted apache and that didnt help. i rebooted the pi and that didnt help either. im more confused now than i was 15 minutes ago.

I got nothing… I’m a PHP coder myself. Haven’t done too much in Python, and haven’t run any apache scripts via python only PHP.

fair enough. i appreciate all the help so far. im close, i just have to figure out whats different between the first hello script and the others and i should be there, since that one worked.

Hi Troy, I’m a newbie of ST coming from a few years of using Vera. I just set up your URI switch and it’s calling my php great in general. One thing I noticed is it doesn’t seem to keep its state. When I click on the “On” or “Force On” button, it will do a HTTP Get to my php but after it’s done executing, the button switched back to “Off” instead of persisting at “On.”

Is there a way for me to maintain its last state?

Thank you.

Jeff

for what its worth, it looks to be related only to the GPIO commands. i created a new hello script that shows up in the browser fine. then i added the contents from another working script that turns on a pin to activate a relay for 3.5 seconds and the script went back to the same error i had before. then i commented out the commands i just put in for the GPIO events, and it works again. i need to do some more research and see if the logs will tell me anything, but its getting close, i just need to see if theres a fix i can find.

anyone else have some input?

Thanks!

if anyone is interested, i figured it out!

the problem was that the apache user, www-root, doesnt have permissions to the /dev/mem folder, which you need in order to access and control the GPIO pins. then i thought about the webpage that i already had, that would let me run the scripts within the php code. so i thought there had to be a way. a quick php script:

<?php
exec ('sudo python /var/www/html/scripts/relay1.py')
?>

and wouldnt you know it, it actually works. that just uses the PHP exec command to run the python script with sudo, so that script can control the pins.

then i added it to the app and it all works great! time to create a bunch more buttons, write some little PHP scripts and finish the programming. then ill throw it in a smarttiles page, and ill FINALLY have ST integration to control the bar!!

thanks for all the help along the way, i wouldnt have been able to iron everything out in apache without it!

I had this issue when I first started playing around with the script. I don’t remember what I added to make sure that it retains its state, but for me it does retain the state. Is there anything in the Live Logs?

So glad you got it working, congrats!

Hi Troy - just checking in to see if you were able to make any progress.

Regards, Mark

Been a crazy holiday. I’ll have something for you soon, so sorry!

Hey Mark… Just finished testing. I was able to get it to POST internally. I have updated my github with the new version here: https://github.com/tguerena/SmartThings/blob/master/devicetypes/tguerena/uri-switch.src/uri-switch.groovy

In order to POST you must change the Internal method to POST and add in the on key & value pair. Should be self explanatory once you implement, but hit me up if you have any questions. This thing was a pain in the arse.