2015年5月29日 星期五

Raspberry Pi B+ 使用網頁控制LED

編譯python檔在Pi上使用指令控制LED【w=亮;s=滅】

vim led_str_ctrl.py

按i並貼上底下程式碼:
====================================================================
#!/usr/bin/python
import RPi.GPIO as G # reference the GPIO library
G.setmode(G.BCM) # use the 'BCM' numbering scheme for the pins
G.setup(18, G.OUT) # Set pin 18 as an Output
while (True): # keep going around this loop until we're told to quit
key = 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 on
elif key == "s":
G.output(18, False) # Turn it off
else:
break # leave our loop
G.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 libraries
import RPi.GPIO as G
from flup.server.fcgi import WSGIServer
import sys, urlparse
 
# set up our GPIO pins
G.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 receive
def app(environ, start_response):
# start our http response
start_response("200 OK", [("Content-Type", "text/html")])
# look for inputs on the URL
i = urlparse.parse_qs(environ["QUERY_STRING"])
yield ('&nbsp;') # 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 on
elif 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 it
WSGIServer(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/

沒有留言:

張貼留言