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

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 that can also serve from HTML templates with Jinja.
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.

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.

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/
Jinja for HTML templating on a CherryPy server: http://www.knight-of-pi.org/better-rpi-remote-controls-cherrypy-and-jinja-for-html-templating/

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.



Time limit is exhausted. Please reload CAPTCHA.