SunFounder 37 Modules Sensor Kit V2.0 for Raspberry PI 2: Web controlled dual color LED

20151115_dual_colorThe first module in the SunFounder 37 Modules Sensor kit is a dual color LED. I will show how to control it using a simple web interface and a python server. There will be a simple website with 3 buttons: red, green and off which will be used to control the LED connected to the Raspberry PI.



There are several python frameworks to build lighweight server applications in python. Here we will use Python bottle. So let’s install it:

sudo pip install bottle


Now we build a simple server:

./01_dual_color.py

#!/usr/bin/env python
from bottle import route, run, template
import RPi.GPIO as GPIO
import time

leds = {"red":11, "green": 12}
allowed_values=["red","green","off"]

# Init GPIO and switch LEDs off
def setup():
        GPIO.setmode(GPIO.BOARD)
        for led in leds:
                GPIO.setup(leds[led],GPIO.OUT)
                GPIO.output(leds[led],GPIO.LOW)

# Clean up everything
def destroy():
        for led in leds:
                GPIO.output(leds[led],GPIO.LOW)
        GPIO.cleanup()

# LED defined as on will be on, all other off
def updateLeds(on):
        for led in leds:
                GPIO.output(leds[led],GPIO.HIGH if led == on else GPIO.LOW)

# Load user interface
@route('/')
def index():
        return template('index')

# API to control leds
@route('/<led>')
def led(led):
        if led in allowed_values:
                updateLeds(led)

# Run the server
if __name__ == '__main__':
        setup()
        try:
                run(host="0.0.0.0", port=8080)
        finally:
                destroy()

For the user-interface we need a simple template which contains the HTML to show the 3 buttons and some Javascript to call the API. The name of this template is „index.tpl“ and it needs to be stored in the subfolder „views“. We will use jquery to call the API and switch on and off the LED:

./views/index.tpl

<html>
  <head>
    <title>Dual color LED control</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  </head>
  <body>
    <h1>Dual color LED control</h1>
    <button type="button" onclick="$.get('/green', function(){})" name = "green" value = "green">green</button>
    <button type="button" onclick="$.get('/red', function(){})" name = "red" value = "red">red</button>
    <button type="button" onclick="$.get('/off', function(){})" name = "off" value = "off">off</button>
  </body>
</html>

Now let’s run the server:

python 01_dual_color_web.py

And we will see the following output:

Bottle v0.12.9 server starting up (using WSGIRefServer())...
Listening on http://0.0.0.0:8080/
Hit Ctrl-C to quit.

Now open the web interface in a brother:

http://ip.of.your.raspberry:8080

We now have a website with 3 buttons:

  • red: switches the LED to red
  • green: switches the LED to green
  • off: switches the LED off

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.