Tested on:RPi 3; Raspbian Jessie; Python 2
Prerequisites:Raspberry Pi with Raspbian Jessie and internet connection (see steps 1-7 in the Introduction)
Host computer with network connection
0

CherryPy: an elegant Python webserver for Raspberry Pi remote controls

Web servers are a great way to build remote controls for Raspberry Pi devices, but they come with a burden: most of them are difficult to setup and configure. CherryPy is a great alternative: a minimalist webserver programmed in Python.
In this tutorial, you’ll learn how to setup a minimal CherryPy web server for triggering an LED. Since it’s Python, the GPIO can be accessed directly without JavaScript interfacing. An other way for building web interfaces for a Raspberry Pi would be WebIOPi, which is more difficult to use, but is built specifically for the Raspberry Pi and includes a GPIO interface.

Installation
In the terminal, enter

Running the sample server
Find the IP address of your Raspberry Pi by entering

and note it. Connect an LED to pin 13 in BOARD numbering (27 in BCM). Then, enter

and browse to the Raspberry Pi by entering

in the web browser of a computer connected to the same network as the RPi and with the IP noted earlier for RASPIIP. Clicking the button will blink the LED attached to pin 13.

Code details
With the decorator @cherrypy.expose, a function is marked as website and can be browsed at, as long as the function returns valid HTML.

The HTML generated by the function index performs a GET if the submit button is clicked with blink as target, which is another Python function. Input field values can be passed with GET as named parameters, pin_str here.

The function blink expects the variable pin_str and lights the LED accordingly.

With the default configuration, CherryPy only answers to localhost. With

the CherryPy server is available for all machines in the network.

Full script
Click on Full Script to show all of the code.

Links
CherryPy website: http://cherrypy.org/
Webinterface WebIOPi tutorial: http://www.knight-of-pi.org/webiopi-a-simple-but-great-web-api-for-the-raspberry-pi/

Knight of Pi

Johannes Bergs aka Knight of Pi. Diploma in Bioinformatics, some Webdesign and Python coding then. Living in the beautiful city of Vienna.

Comment

Time limit is exhausted. Please reload CAPTCHA.