安卓web控制樹莓派LED開關

剛接觸樹莓派,之前在網上看了一個教程,自己做了一點點修改,實現了控制LED開關功能。

先放上原貼。 http://www.cnblogs.com/yafengabc/archive/2016/02/18/5197844.html

服務器用到了微型Python Web框架 Bottle, Bottle 被設計爲僅僅只有一個文件,我們甚至可以不安裝它,直接將 bottle.py 文件下載並複製到我們的應用中就可以使用了。

如果想要安裝,那麼我們可以像安裝其它的 Python 模塊一樣。

#安裝bottle
sudo easy_install -U bottle

如果我們直接將 bottle.py 下載到自己的應用中的話,我們可以建立下面這樣的目錄結構:

#將bottle模塊和應用放在一個目錄中
+ application
+----bottle.py
+----app.py

安裝好bottle模塊後,再編寫源代碼,放在一個目錄中就能使用。

#!/usr/bin/env python3
from bottle import get,post,run,request,template
import wiringpi
pin=0


io=wiringpi.GPIO(wiringpi.GPIO.wpi_MODE_PINS)
io.pinMode(pin,io.OUTPUT)


@get("/")
def index():
    return template("index")
@post("/cmd")
def cmd():
    print ("push the button : "+request.body.read().decode())
    if request.body.read().decode() == 'on':
        io.digitalWrite(pin,io.HIGH)
        print 'LOD ON'
    else:
        io.digitalWrite(pin,io.LOW)
        print 'LED OFF'
    return "OK"
run(host="0.0.0.0")

代碼解釋,這裏原作者做的很好:
1. #!/usr/bin/env python3 ,告訴shell這個文件是Python源代碼,讓bash調用python3來解釋這段代碼
2. .from bottle import get,post,run,request,template ,從bottle框架導入了我用到的方法、對象

下邊幾句是定義了2個路由,一個是“/”一個是“/cmd”,前者是get類型(用@get裝飾),後者是POST類型(用的@post裝飾)

第一個路由很簡單,就是讀取index模版(模版就是個html啦)併發送到客戶端(瀏覽器),因爲路徑是“/”也就是比如樹莓派的IP地址是:192.168.0.10

那用http://192.168.0.10:8080就訪問到了我們的”/”路由(bottle默認端口是8080)

同理,第二個路由的路徑是“/cmd”也就是訪問http://192.168.0.10:8080/cmd就訪問到了第二個路由

最後一句:run(host=”0.0.0.0”)就是調用bottle的run方法,建立一個http服務器,讓我們能通過瀏覽器訪問我們的界面。

這裏我添加了很簡單的控制引腳開關功能,這裏需要安裝wiringpi模塊。
如果沒有安裝,這裏是安裝方式:

安裝git-core

sudo apt-get install git-core

下載winringPi庫


git clone git://git.drogon.net/wiringPi

編譯和安裝庫

cd wiringPi
./build

可以使用下面的命令對庫進行更新

cd wiringPi
git pull origin

歐克~wiringpi模塊安裝成功

下邊解釋一下這些代碼的作用:
第一個路由的作用就是扔給瀏覽器一個HTML(index.tpl)文檔,顯示這個界面:
界面做了些修改

這裏寫圖片描述

這個文件的源代碼如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>遙控樹莓派控制LED</title>
    <link href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" media="screen">
    <script src="http://code.jquery.com/jquery.js"></script>
    <style type="text/css">
        #up {
            margin-left: 55px;
            margin-bottom: 3px;
        }
        #down {
            margin-top: 3px;
            margin-left: 55px;
        }
    </style>
    <script>
        $(function(){
            $("button").click(function(){
                cmd="{cmd:"+this.id+"}"
                //alert(cmd)
                $.post("/cmd",this.id,function(data,status){
                });
            });

        });

    </script>
</head>
<body>
<div id="container" class="container">
    <div>
        <button id="on" class="btn btn-lg btn-primary glyphicon glyphicon-circle-arrow-up">LED ON</button>
    </div>
    <div>
        <button id='left' class="btn btn-lg btn-primary glyphicon glyphicon-circle-arrow-left"></button>
        <button id='stop' class="btn btn-lg btn-primary glyphicon glyphicon-stop">Temperature</button>
        <button id='right' class="btn btn-lg btn-primary glyphicon glyphicon-circle-arrow-right"></button>
    </div>
    <div>
        <button id='off' class="btn btn-lg btn-primary glyphicon glyphicon-circle-arrow-down">LED OFF</button>
    </div>

</div>

<script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</body>
</html>

UI部分用了jquery bootstrap這兩個前端框架,加了5個按鈕,但是現在只添加了LED ON和LED OFF功能。 (之後要擴展,所以,on是開燈,OFF和中間三個按鈕都做成了關燈。只想用開關的可以吧中間三個button刪掉。)
使用 jQuery AJAX,與服務器交換數據。
歐克~代碼實現結束。
只要將這三個文件放在同一目錄下,就可以在同一局域網內,實現瀏覽器控制LED開關啦。
完整源代碼稍後貼出~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章