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:


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

leds = {"red":11, "green": 12}

# Init GPIO and switch LEDs off
def setup():
        for led in leds:

# Clean up everything
def destroy():
        for led in leds:

# 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
def index():
        return template('index')

# API to control leds
def led(led):
        if led in allowed_values:

# Run the server
if __name__ == '__main__':
                run(host="", port=8080)

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:


    <title>Dual color LED control</title>
    <script src=""></script>
    <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>

Now let’s run the server:


And we will see the following output:

Bottle v0.12.9 server starting up (using WSGIRefServer())...
Listening on
Hit Ctrl-C to quit.

Now open the web interface in a brother:


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