【转】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 后再输入此指令。

 

3安装Flask(Flask是是一个轻量级的 Web应用框架, 使用 Python编写):
$sudo pip install flask

 

4 GitHub 下载“ python-pcduino”这个库文件放到 ubuntu 下,打开 Sample ,复制“ blink_led ”更名为“ hello-gpio ”,然后在里面将
blink_led ”更名为“ hello-gpio.py" ,” hello-gpio.py “代码如下:
 
 

 

  1. from flask import Flask, render_template
  2. import datetime  
  3. import gpio
  4. app = Flask(__name__)
  5. channel = { 0:'gpio0', 1:'gpio1', 2:'gpio2', 3:'gpio3', 4:'gpio4',
  6.                         5:'gpio5', 6:'gpio6', 7:'gpio7', 8:'gpio8', 9:'gpio9',
  7.                         10:'gpio10', 11:'gpio11', 12:'gpio12', 13:'gpio13'
  8.                      }
  9. @app.route("/")
  10. def hello():
  11.         now = datetime.datetime.now()
  12.         timeString = now.strftime("%Y/%m/%d  %H:%M:%S")
  13.         templateData = {
  14.                         'title':'HELLO!',
  15.                         'time':timeString
  16.                         }
  17.         return render_template('main.html',**templateData)
  18. @app.route("/readpin/<pin>")
  19. def readPin(pin):
  20.                
  21.                 gpio.pinMode(channel[int(pin)],gpio.INPUT)
  22.                 value  = " "
  23.                 if  (gpio.digitalRead(channel[int(pin)]) == gpio.HIGH)  :
  24.                         value = "Read GPIO" + pin + " is high !"
  25.                 else :
  26.                         value = "Read GPIO" + pin +" is low !"
  27.                 templateData = {
  28.                                         'title' : 'Status of GPIO' + pin ,
  29.                                         'value' : value
  30.                                                         }
  31.                 return render_template('pin.html',**templateData)
  32. if __name__ == "__main__" :
  33.         app.run (host='0.0.0.0',port=80,debug=True)  
复制代码

5在文件夹”hello-gpio“的目录下新建一个名为”templates“的文件夹,文件夹内包含2个文件:“main.html ,  pin.html <ignore_js_op>
<ignore_js_op>

main.html内容如下:
  1. <!DOCTYPE html>
  2.         <head>
  3.                 <title>{{ title }} </title>
  4.         </head>
  5.         <body>
  6.                 <center>
  7.                 <h1>Welcome to pcDuino !</hl>
  8.                 <h2>The date and time on the server is :{{ time }}</h2>
  9.                 </center>
  10.         </body>
  11. </html>
复制代码
pin.html内容如下:
  1. <!DOCTYPE html>
  2.         <head>
  3.                 <title>{{ title }} </title>
  4.         </head>
  5.         <body>
  6.                 <center>
  7.                 <h1>Pin Status </h1>
  8.                 <h2>{{ value }}</h2>
  9.                 <hr>
  10.  nbsp;               <a href="http://www.pcduino.org">pcDuino.org</a>
  11.                 </center>
  12.         </body>
  13. </html>
复制代码
6输入: $sudo python ./hello-gpio.py 运行代码: <ignore_js_op>

7查看pcduinoip地址:$ifconfig <ignore_js_op>

8在另外一台与pcDuino共用同一个网络的pc机上打开浏览器输入 192.168.35 你会看到如下信息:

转载于:https://www.cnblogs.com/qvbrgw/p/4501396.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值