99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀使用Python簡(jiǎn)單的實(shí)現(xiàn)樹莓派的WEB控制
使用Python簡(jiǎn)單的實(shí)現(xiàn)樹莓派的WEB控制
2018-01-29
收藏

使用Python簡(jiǎn)單的實(shí)現(xiàn)樹莓派的WEB控制

這篇文章主要介紹了使用Python簡(jiǎn)單的實(shí)現(xiàn)樹莓派的WEB控制的相關(guān)資料,需要的朋友可以參考下


先給大家展示下效果如圖,感覺還很滿意請(qǐng)繼續(xù)閱讀全文:
 

用到的知識(shí):Python Bottle HTML Javascript JQuery Bootstrap AJAX 當(dāng)然還有 linux

我去,這么多……我還是一點(diǎn)一點(diǎn)說起吧……

先貼最終的源代碼:

#!/usr/bin/env python3
from bottle import get,post,run,request,template
@get("/")
def index():
return template("index")
@post("/cmd")
def cmd():
print("按下了按鈕: "+request.body.read().decode())
return "OK"
run(host="0.0.0.0")



沒錯(cuò),就10句,我一句一句解釋:

1. # !/usr/bin/env python3 ,告訴shell這個(gè)文件是Python源代碼,讓bash調(diào)用python3來解釋這段代碼

2. from bottle import get,post,run,request,template ,從bottle框架導(dǎo)入了我用到的方法、對(duì)象

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

第一個(gè)路由很簡(jiǎn)單,就是讀取index模版(模版就是個(gè)html啦)并發(fā)送到客戶端(瀏覽器),因?yàn)槁窂绞恰?”也就是比如樹莓派的IP地址是:192.168.0.10

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

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

最后一句: run(host = " 0.0.0.0 " )就是調(diào)用bottle的run方法,建立一個(gè)http服務(wù)器,讓我們能通過瀏覽器訪問我們的界面。

下邊我詳細(xì)的解釋一下這些代碼的作用:

第一個(gè)路由的作用就是扔給瀏覽器一個(gè)HTML(index.tpl)文檔,顯示這個(gè)界面:
 

這個(gè)文件的源代碼如下:


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>遙控樹莓派</title>
<link 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(){
$.post("/cmd",this.id,function(data,status){});
});
});
</script>
</head>
<body>
<div id="container" class="container">
<div>
<button id="up" class="btn btn-lg btn-primary glyphicon glyphicon-circle-arrow-up"></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"></button>
<button id='right' class="btn btn-lg btn-primary glyphicon glyphicon-circle-arrow-right"></button>
</div>
<div>
<button id='down' class="btn btn-lg btn-primary glyphicon glyphicon-circle-arrow-down"></button>
</div>
</div>
<script src="http://cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</body>
</html>

這個(gè)內(nèi)容有點(diǎn)多,不過很簡(jiǎn)單,就是引用了jquery bootstrap這兩個(gè)前端框架,加了5個(gè)按鈕(<body></body>之間的代碼)。當(dāng)然我用了bootstrap內(nèi)置的上下左右停止這幾個(gè)圖標(biāo),這5個(gè)按鈕的id分辨定義成up,down,left,right,stop,然后寫了如下的關(guān)鍵代碼:    
$(function(){
$("button").click(function(){
$.post("/cmd",this.id,function(data,status){});
});
});


沒錯(cuò),就這三句代碼……

第1,2行給所有的按鈕(button)綁定了一個(gè)點(diǎn)擊的事件,第三行調(diào)用jquery的post方法把this.id(被單擊按鈕的id),發(fā)送到“/cmd”這個(gè)路徑下,這時(shí),我們python代碼的第二個(gè)路由起作用了,接收到了網(wǎng)頁上被單擊按鈕的id,并打印出了“按下了按鈕: XXX”

當(dāng)然,在這里寫幾個(gè)if語句判斷,就可以按照實(shí)際的需求做一些實(shí)際的控制了,嗯,比如調(diào)用wiringpi2 for python控制樹莓派的GPIO。

關(guān)于使用Python簡(jiǎn)單的實(shí)現(xiàn)樹莓派的WEB控制的相關(guān)內(nèi)容就給大家介紹這么多,希望對(duì)大家有所幫助!



數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }