[vc_row][vc_column][vc_column_text]
In this tutorial, we will introduce how to control the states of LEDs through the web browser. This project servers as the template of home automation.[/vc_column_text][vc_tour][vc_tab title=”Install Software Tools” tab_id=”1395105656-1-24″][vc_column_text]1.Update the apt-get.
$ sudo apt-get update
2.Install Request. Requests is the HTTP client library of the Python.
$ sudo apt-get install python-requests
3. Install the python-pip, pip could replace the easy_install and manage the python software tool.
$ sudo apt-get install python-imaging python-imaging-tk python-pip python-dev
4. Install Flask. Flask is a low level Web application framework, use the Python to code.
$ sudo pip install flask
5. Install pcDuino-python sample library
$git clone https://github.com/pcduino/python-pcduino
[/vc_column_text][/vc_tab][vc_tab title=”Test Code” tab_id=”1395105656-2-34″][vc_column_text]1.Change the blink_led.py file in blink_led sample to web-led.py , and input the following code;
import gpio from flask import Flask, render_template, request app = Flask(__name__) pins = { 'gpio0':{'name':'Red_LED','state':False}, 'gpio1':{'name':'Blue_LED','state':True} } for pin in pins: gpio.pinMode(pin,gpio.OUTPUT) gpio.digitalWrite(pin,gpio.LOW) @app.route("/") def main(): for pin in pins : pins[pin]['state'] = gpio.digitalRead(pin) templateData = { 'pins':pins } return render_template('pin.html',**templateData) @app.route("/<changepin>/<value>") def action(changepin,value): setpin = changepin message = " " deviceName = pins[setpin]['name'] if value == "on" : gpio.digitalWrite(setpin,gpio.HIGH) message = "turned " + deviceName + " on." if value == "off" : gpio.digitalWrite(setpin,gpio.LOW) message = "turned " + deviceName + " off." if value == "toggle" : gpio.digitalWrite(setpin,not gpio.digitalRead(setpin)) message = "toggled " + deviceName + " ." for pin in pins: pins[pin]['state'] = gpio.digitalRead(pin) templateData = { 'message' : message , 'pins' : pins } return render_template('pin.html',**templateData) if __name__ == "__main__" : app.run (host='0.0.0.0',port=80,debug=True)
2. Build the templates file at another directory, and use the test editor to build the pin.html in the file,code as follows:
<!DOCTYPE html> <head> <title>Current Status </title> </head> <body > <center> <h1>Device Listing and Status</h1> <font size="5" color="red"> <p>The {{ pins['gpio0'].name }} {% if pins['gpio0'].state == true %} is currently on ! (<a href="/{{ 'gpio0' }}/off">turn off</a>) {% else %} is currently off ! (<a href="/{{ 'gpio0' }}/on">turn on</a>) {% endif %} </p> </font> <font size="5" color="blue"> <p>The {{ pins['gpio1'].name }} {% if pins['gpio1'].state == true %} is currently on ! (<a href="/{{ 'gpio1' }}/off">turn off</a>) {% else %} is currently off ! (<a href="/{{ 'gpio1' }}/on">turn on</a>) {% endif %} </p> </font> {% if message %} <h2>{{ message }}</h2> {% endif %} <hr> <font size="6"> <a href="http://www.pcduino.org">pcDuino.org</a> </font> </center> </body> </html>
[/vc_column_text][/vc_tab][vc_tab title=”Test Result” tab_id=”1395106811207-2-0″][vc_column_text]
$sudo python ./web-led.py
Open the browser on the another PC which share the same network of the pcDuino, and input
The current pcDuino IP address, you will see the information as follows:
[/vc_column_text][/vc_tab][/vc_tour][/vc_column][/vc_row]
Leave a Reply
You must be logged in to post a comment.