, , , , , ,

This is another simple demonstration on how to control a LED light from CherryPy web app.

The connection is exactly the same as the previous post regarding the blinking LED.
LED connection to Raspberry Pi GPIO

A closer look at GPIO side
But now, instead of blinking, I can control it using a “slide switch” from my web app.

Simple Web App

Simple Web App

The web app is done by jQuery. First of all, create a web element (slide switch) called “light_1” as follow:

<label for="light_1">Light 1</label>
<select name="light_1" id="light_1" data-role="slider" data-track-theme="b" data-theme="b">
<option value="off">Off</option>
<option value="on">On</option>

Then write this line with other jQuery scripts:

$("#light_1").change(function () {$.post('/switch',{key_pressed:"power_"+$(this).val()})});

Under Python section, write the following:

    def switch(self, **data):
        import RPi.GPIO as GPIO #Import GPIO library
        import time #Import 'time' library so that we can use 'sleep()'

        # Use physical board pin numbers
        # Set up header pin 11 (GPIO17) as an output
        print "Setup Pin 11"
        GPIO.setup(11, GPIO.OUT)

        key = data['key_pressed']
        if key=="power_on":
            print "switch on light"
            GPIO.output(11, True) #switch on
        elif key=="power_off":
            print "switch off light"
            GPIO.output(11, False) #switch off

Here is the full code: http://pastebin.com/FdKRLne4

Have fun! Next up, combine this servo motor – may need external supply so that both of them will not draw too much current from R-Pi. Test multiple servos as well.

NOTE: From now onwards I have to use sudo python pilldispense.py because only root has access to /dev/mem.