安卓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开关啦。
完整源代码稍后贴出~