【轉】PCDuino用python讀取GPIO資料

來源:互聯網
上載者:User

標籤:

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查看pcduino的ip地址:$ifconfig : <ignore_js_op>
8在另外一台與pcDuino共用同一個網路的pc機上開啟瀏覽器輸入 192.168.35 你會看到如下資訊:

【轉】PCDuino用python讀取GPIO資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.