編譯python檔在Pi上使用指令控制LED【w=亮;s=滅】
vim led_str_ctrl.py
按i並貼上底下程式碼:
====================================================================
#!/usr/bin/pythonimport RPi.GPIO as G # reference the GPIO libraryG.setmode(G.BCM) # use the 'BCM' numbering scheme for the pinsG.setup(18, G.OUT) # Set pin 18 as an Outputwhile (True): # keep going around this loop until we're told to quitkey = raw_input("Enter 'w' for On, 's' for Off and any other key to quit. You'll need to press enter after each character: ")if key == "w":G.output(18, True) # Turn it onelif key == "s":G.output(18, False) # Turn it offelse:break # leave our loopG.cleanup() # Tidy up after ourselves so we don't generate warnings next time we run this
====================================================================
按Ecs輸入:wq,存檔並離開
輸入 sudo python led_str_ctrl.py 執行程式碼
輸入w LED亮,輸入s LED滅。
安裝套件
sudo apt-get install lighttpd sudo service lighttpd start輸入 vim /var/www/index.html
按 i 並貼上底下程式碼
====================================================================
====================================================================<html><head><title>Hello from the Pi</title></head><body><h1>Hello world from the Raspberry Pi</h1></body></html>
此時在網頁上輸入自己的ip就可以看到訊息了
可以使用ifconfig看自己的IP
http://192.168.1.164
輸入後可看到剛剛設定的訊息
安裝套件
sudo apt-get install python-flup在 /var/www/ 內新增 doStuff.py 檔案並輸入程式碼:
vim /var/www/doStuff.py
====================================================================
#!/usr/bin/pythonRoot# bring in the librariesimport RPi.GPIO as Gfrom flup.server.fcgi import WSGIServerimport sys, urlparse# set up our GPIO pinsG.setmode(G.BCM)G.setup(18, G.OUT)# all of our code now lives within the app() function which is called for each http request we receivedef app(environ, start_response):# start our http responsestart_response("200 OK", [("Content-Type", "text/html")])# look for inputs on the URLi = urlparse.parse_qs(environ["QUERY_STRING"])yield (' ') # flup expects a string to be returned from this function# if there's a url variable named 'q'if "q" in i:if i["q"][0] == "w":G.output(18, True) # Turn it onelif i["q"][0] == "s":G.output(18, False) # Turn it off#by default, Flup works out how to bind to the web server for us, so just call it with our app() function and let it get on with itWSGIServer(app).run()
====================================================================
修改該檔案的屬性:
sudo chmod 755 /var/www/doStuff.py
複製檔案並修改權限:
sudo cp /usr/bin/python2.7 /usr/bin/pythonRoot sudo chmod u+s /usr/bin/pythonRoot編輯檔案:
sudo vim /etc/lighttpd/lighttpd.conf
在server.modules新增一行"mod_fastcgi",
在最底下空白處新增
fastcgi.server = (
".py" => (
"python-fcgi" => (
"socket" => "/tmp/fastcgi.python.socket",
"bin-path" => "/var/www/doStuff.py",
"check-local" => "disable",
"max-procs" => 1)
)
)
重啟伺服器:
sudo service lighttpd restart
http://192.168.1.164/doStuff.py?q=s
http://192.168.1.164/doStuff.py?q=w
編輯index.html檔案:
vim /var/www/index.html
====================================================================
====================================================================<head><title>Hello from the Pi</title><script src="//ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js"></script></head><body><h1>Hello world from the Raspberry Pi</h1><form><input type="button" value="On" onclick="go('w')" style="font-size:200%;"><br /><input type="button" value="Off" onclick="go('s')" style="font-size:200%;"></form><script type="text/javascript">function go(qry) {new Ajax.Request('doStuff.py?q=' + qry,{method: 'GET'});}</script></body></html>
重啟伺服器:
sudo service lighttpd restart
再次輸入網址
http://192.168.1.164
可以看到新增了按鈕,可以直接控制LED
====================================================================
可將HTML檔放在PI以外的地方
在電腦上新增一個.html檔
<head>
<title>Hello from the Pi</title>
<script src="http://ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js"></script>
</head>
<body>
<h1>Hello world from the Raspberry Pi</h1>
<form>
<input type="button" value="On" onclick="go('w')" style="font-size:200%;"><br />
<input type="button" value="Off" onclick="go('s')" style="font-size:200%;">
</form>
<script type="text/javascript">
function go(qry) {
new Ajax.Request('http://pi.hong/doStuff.py?q=' + qry,
{method: 'GET'}
);
}
</script>
</body>
</html>
將PI.HONG換成該PI版的IP存檔後開啟即可使用
====================================================================
原始教學網站:
http://davstott.me.uk/index.php/2013/03/17/raspberry-pi-controlling-gpio-from-the-web/
沒有留言:
張貼留言