• Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
HomepcDuinoPythonPython: Read pcDuino GPIO through “Web” ...
Previous Next

Python: Read pcDuino GPIO through “Web”

Posted by: Alvin Jin , March 9, 2014

[vc_row][vc_column width=”1/1″][vc_column_text]图片8[/vc_column_text][/vc_column][/vc_row][vc_row][vc_column width=”1/1″][vc_tour][vc_tab title=”Install Request” tab_id=”1394429524-1-28″][vc_column_text]Requests is a Python HTTP client library

$ sudo apt-get install python-requests

[/vc_column_text][/vc_tab][vc_tab title=”Install python-pip” tab_id=”1394429524-2-73″][vc_column_text]Pip is a python install and software management tool which can replace easy_install.

$sudo apt-get update 

$sudo apt-get install python-imaging python-imaging-tk python-pip python-dev git

[/vc_column_text][/vc_tab][vc_tab title=”Install Flask” tab_id=”1394429916471-2-9″][vc_column_text]Flask is a lightweight Web application framework written in Python.

$sudo pip install flask

[/vc_column_text][/vc_tab][vc_tab title=”The rest steps” tab_id=”1394429978693-3-1″][vc_column_text]1. Put “python-pcduino” (download from github) under ubuntu, open folder “Sample”, copy “blink_led” and rename to “hello-gpio”. And change the name “blink_led” to “hello-gpio.py” in the file. The code of hello-gpio.py is shown as below:

from flask import Flask, render_template
import datetime  
import gpio

app = Flask(__name__)

channel = { 0:'gpio0', 1:'gpio1', 2:'gpio2', 3:'gpio3', 4:'gpio4', 
                        5:'gpio5', 6:'gpio6', 7:'gpio7', 8:'gpio8', 9:'gpio9', 
                        10:'gpio10', 11:'gpio11', 12:'gpio12', 13:'gpio13'
                     }

@app.route("/")
def hello():

        now = datetime.datetime.now()
        timeString = now.strftime("%Y/%m/%d  %H:%M:%S")
        templateData = {
                        'title':'HELLO!',
                        'time':timeString
                        }
        return render_template('main.html',**templateData)

@app.route("/readpin/<pin>")
def readPin(pin):

                gpio.pinMode(channel[int(pin)],gpio.INPUT)
                value  = " "

                if  (gpio.digitalRead(channel[int(pin)]) == gpio.HIGH)  :
                        value = "Read GPIO" + pin + " is high !"
                else :
                        value = "Read GPIO" + pin +" is low !"
                templateData = {
                                        'title' : 'Status of GPIO' + pin ,
                                        'value' : value
                                                        }
                return render_template('pin.html',**templateData)

if __name__ == "__main__" :
        app.run (host='0.0.0.0',port=80,debug=True)

2.  Create a folder named “templates” in the same folder with “hello-gpio”. It contains two files: “main.html” and “pin.html”

图片1

图片2

main.html Code:

<!DOCTYPE html>
        <head>
                <title>{{ title }} </title>
        </head>

        <body>
                <center>
                <h1>Welcome to pcDuino !</hl>
                <h2>The date and time on the server is :{{ time }}</h2>
                </center>
        </body>
</html>

pin.html Code:

<!DOCTYPE html>
        <head>
                <title>{{ title }} </title>
        </head>

        <body>
                <center>
                <h1>Pin Status </h1>
                <h2>{{ value }}</h2>
                <hr>
                <a href="http://www.pcduino.org">pcDuino.org</a>
                </center>
        </body>
</html>

3. Input

$sudo python ./hello-gpio.py

to run the code

图片3

4.  View pcduino IP address

$ifconfig

图片4

5. Use another PC which in the same net with pcDuino, input   192.168.1.35 in a browser. You will see:

图片5

Visit: http://192.168.1.35/readpin/0  (Read the status of GPIO0)

图片6

图片7

[/vc_column_text][/vc_tab][/vc_tour][/vc_column][/vc_row]

Tags: Python

Share!
Tweet

Alvin Jin

About the author

Leave a Reply Cancel reply

You must be logged in to post a comment.

Category

  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors