I have seen a lot of projects which claim to control the GPIO pins, but I want something a bit different, for example, to be able to blink an LED.
Is there a system out there where, via a web interface, I can click and execute a python script, for example "blink.py" on my raspberry.
3 Answers 3
You could make this happen in any number of ways using CGI or other server side script. One problem will be permissions to accessing GPIO pins. There doesn't seem to be a clean solution. Currently it might be easiest to chown the gpio files to the user that runs the web server, call a (suid) program that can access the pins or have a separate daemon with access to the pins that you can send messages to (signal, pipe/socket, other ipc...).
For "executing a python script via a web interface", you might want to look at web.py. It's a very neat little module that lets you write a single file standalone "web service" that could do anything. Very handy for this kind of thing IME. Requires effectively no configuration or special software (apart from common python install and web.py itself). Just write handlers for urls in python, optionally with html templates and run. Point a client (browser, other script, wget..) at the right port and it just works. :)
Edit: A new project spotted, serpint seems to allow wiggling gpio from a socket or possibly fake char device interface.
-
4
-
erm, for GPIO access just add the user to the gpio group. Or start the server as root, which most people do for port 80 anyway. Flask examples can call scripts or use time to add sleep between toggles. The main feature is {{ }} in the templates which I suspect the OP did not need.mckenzm– mckenzm2019年06月26日 05:30:54 +00:00Commented Jun 26, 2019 at 5:30
-
Thanks for the update. For the "erm", though, this answer is from 2012 when GPIOs were notoriously root only. I see there's a gpio group now, which is fantastic progress and a more correct way of accessing hardware.XTL– XTL2019年07月16日 10:34:15 +00:00Commented Jul 16, 2019 at 10:34
If you are just starting with webdevelopment, have a look at Bottle. Bottle is simpler than flask in the sense that it is a complete web-framework within a single file. In contrast, Flask aims to reuse sound code from different libraries and might therefore be more solid, but also more complex.
Here is the Hello World with Bottle:
from bottle import route, run, template
@route('/hello/:name')
def index(name='World'):
return template('<b>Hello {{name}}</b>!', name=name)
run(host='localhost', port=8080)
Run it with:
python HelloBottle.py
And open in a browser: http://localhost:8080/hello/world
To make your website available from other computers, set host
to 0.0.0.0
in the run
method. The last line of the above Hello World should then read:
run(host='0.0.0.0', port=8080)
You should now be able to access your website via the Pi's IP address, like this: http://192.168.0.123:8080/hello/world
See the bottle documentation on deployment for further details.
-
On my Raspberry Pi, if I execute
curl http://localhost:8080/hello/world
I receive the expected results. However I want to be able to access this on my regular computer over the lan. After usingifconfig
for my IP address, the following URL on my regular computer is unable to establish a connection:http://192.168.1.102:8080/hello/matthew
. I am able to access another webpage I made at `192.168.1.102/home.php'. Would you have any idea on how I can to my bottle page?Matthew Moisen– Matthew Moisen2013年09月03日 03:21:33 +00:00Commented Sep 3, 2013 at 3:21 -
Your home.php is implicitely accessed via port 80 (http) try running bottle on that port and if it succedes, make sure there are no Firewalls between or on your desktop and the pi blocking port 8080.Bengt– Bengt2013年09月04日 15:09:42 +00:00Commented Sep 4, 2013 at 15:09
-
It turns out that I needed to change my IP to either localhost or 0.0.0.0Matthew Moisen– Matthew Moisen2013年09月04日 20:44:00 +00:00Commented Sep 4, 2013 at 20:44
-
True, as the documentation states, setting the IP address of the app to
0.0.0.0
makes bottle listen on any address, including the Pi's.Bengt– Bengt2013年09月08日 11:32:06 +00:00Commented Sep 8, 2013 at 11:32
Here is a tutorial how this can be achieved: https://roderickvella.wordpress.com/2017/01/04/control-a-separate-running-script-from-a-web-server-python-rpi/
-
1Kindly summarize the tutorial in your answer, in case the link disappears someday or the contrent changes.tlhIngan– tlhIngan2017年01月07日 23:49:39 +00:00Commented Jan 7, 2017 at 23:49
-
We're trying a new policy with regard to informationless link-only answers here. If this post is not edited to contain information that can stand as an answer, however minimal, in 48 hours it will be converted to Community Wiki to simplify having it corrected by the community.Steve Robillard– Steve Robillard2017年01月08日 00:21:41 +00:00Commented Jan 8, 2017 at 0:21