电竞比分网-中国电竞赛事及体育赛事平台

分享

第四章:pcDuino上用Python編程應(yīng)用(I)

 xiaofenglib 2013-08-21
本帖最后由 zhao?J 于 2013-8-8 14:23 編輯

前面我們已經(jīng)簡單地介紹了如何使用Python語言來對pcDuino的硬件進行控制編程,現(xiàn)在具體介紹幾個在pcDuino上的Python編程應(yīng)用實例。

一、將你的pcDuino設(shè)置為web服務(wù)器


1、安裝Request(Requests 是一個 Python 的 HTTP 客戶端庫):
  1. $ sudo apt-get install python-requests
復(fù)制代碼

2、安裝python-pip(pip是一個可以替代easy_install的安裝和管python軟件包的工具 ) :
  1. $sudo apt-get install python-imaging python-imaging-tk python-pip python-dev git
復(fù)制代碼
注意:有時候運行這個指令會安裝失敗,那么需要輸入:$sudo apt-get update 后再輸入此指令。

3、安裝Flask(Flask是是一個輕量級的Web應(yīng)用框架, 使用Python編寫):
  1. $sudo pip install flask
復(fù)制代碼

4、 示例代碼:

  1. from flask import Flask
  2. app = Flask(__name__)
  3. @app.route("/")
  4. def hello():
  5. return " Welcome to pcDuino ! "
  6. if __name__ == "__main__":
  7. app.run(host='0.0.0.0', port=80, debug=True)
復(fù)制代碼

保存文件為hello-flask.py ,運行 $sudo python ./hello-flask.py


                               
登錄/注冊后可看大圖



5、 輸入指令 $ifconfig 查看pcDuino的ip地址:


                               
登錄/注冊后可看大圖


6、 在另外一臺與pcDuino共用同一個網(wǎng)絡(luò)的pc機上打開瀏覽器輸入 192.168.35 你會看到如下信息:


                               
登錄/注冊后可看大圖



同時,在pcDuino的終端你會看到如圖信息:


                               
登錄/注冊后可看大圖

表示有ip:192.168.1.25的客戶端在連接pcDuino服務(wù)器。



二、如何通過網(wǎng)頁來讀取pcDuino的GPIO狀態(tài)


1、 安裝Request(Requests 是一個 Python 的 HTTP 客戶端庫):
  1. $ sudo apt-get install python-requests
復(fù)制代碼

2、安裝python-pip(pip是一個可以替代easy_install的安裝和管python軟件包的工具 ) :
  1. $sudo apt-get install python-imaging python-imaging-tk python-pip python-dev git
復(fù)制代碼
注意:有時候運行這個指令會安裝失敗,那么需要輸入:$sudo apt-get update 后再輸入此指令。

3、安裝Flask(Flask是是一個輕量級的Web應(yīng)用框架, 使用Python編寫):
  1. $sudo pip install flask
復(fù)制代碼
4、GitHub下載“python-pcduino”這個庫文件放到ubuntu下,打開Sample,復(fù)制“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)  
復(fù)制代碼

5、在文件夾”hello-gpio“的目錄下新建一個名為”templates“的文件夾,文件夾內(nèi)包含2個文件:“main.html ,  pin.html”


                               
登錄/注冊后可看大圖



                               
登錄/注冊后可看大圖


main.html內(nèi)容如下:
  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>
復(fù)制代碼
pin.html內(nèi)容如下:
  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.                 <a href="http://www.">pcDuino.org</a>
  11.                 </center>
  12.         </body>
  13. </html>
復(fù)制代碼
6、輸入: $sudo python ./hello-gpio.py 運行代碼:

                               
登錄/注冊后可看大圖


7、查看pcduino的ip地址:$ifconfig :

                               
登錄/注冊后可看大圖


8、在另外一臺與pcDuino共用同一個網(wǎng)絡(luò)的pc機上打開瀏覽器輸入 192.168.35 你會看到如下信息:

訪問:http://192.168.1.35/

                               
登錄/注冊后可看大圖


訪問:http://192.168.1.35/readpin/0  (讀取gpio0的狀態(tài))

                               
登錄/注冊后可看大圖

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多