【转】PCDuino用python读取GPIO数据
1 安装Request(Requests 是一个 Python 的 HTTP 客户端库):
$ sudo apt-get install python-requests
2安装python-pip(pip是一个可以替代easy_install的安装和管python软件包的工具 ) :
$sudo apt-get install python-imaging python-imaging-tk python-pip python-dev git
注意:有时候运行这个指令会安装失败,那么需要输入:
$sudo apt-get update 后再输入此指令。
$sudo pip install flask
“blink_led”更名为“hello-gpio.py",”hello-gpio.py“代码如下:
- 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)
5在文件夹”hello-gpio“的目录下新建一个名为”templates“的文件夹,文件夹内包含2个文件:“main.html , pin.html” <ignore_js_op>
main.html内容如下:
- <!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>
- <!DOCTYPE html>
- <head>
- <title>{{ title }} </title>
- </head>
- <body>
- <center>
- <h1>Pin Status </h1>
- <h2>{{ value }}</h2>
- <hr>
- nbsp; <a href="http://www.pcduino.org">pcDuino.org</a>
- </center>
- </body>
- </html>
7查看pcduino的ip地址:$ifconfig : <ignore_js_op>
8在另外一台与pcDuino共用同一个网络的pc机上打开浏览器输入 192.168.35 你会看到如下信息: