×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
準備もできたので
LEDをON OFFしてみましょう
これでONOFFができた
http://xxx.xxx.xxx.xxx:8080でweb表示
#HTMLの記載
------------------------------------------------------------
テキストを入力させたい時は
------------------------------------------------------------
HTMLの部分を書き換えて
LEDをON OFFしてみましょう
これでONOFFができた
http://xxx.xxx.xxx.xxx:8080でweb表示
from webob import Request, Response #WebObの読み込み
import RPi.GPIO as GPIO
#HTMLの記載
html = """<h1> kusoneko:%d</h1>
<form method="post">
<h1>
<input type="submit" name="button" value="ON">
<input type="submit" name="button" value="OFF">
</h1>
</form>
"""
count = 0
#GPIOの設定
bcmled = 12
GPIO.setmode(GPIO.BOARD) #BOARD 12 BCM 18
GPIO.setup(bcmled,GPIO.OUT) #出力
class WebApp(object):
def __call__(self, environ ,start_response):
global html,count
req=Request(environ)
if req.path=='/':
button = req.params.get('button','') #buttonの読み込み
if (button=='ON'):
GPIO.output(bcmled, 1)
count=1
if (button=='OFF'):
GPIO.output(bcmled, 0)
count=0
resp = Response(html % count)
else:
resp = Response()
return resp(environ, start_response)
application = WebApp()
if __name__== '__main__':
from wsgiref.simple_server import make_server
port=8080
httpd= make_server('', port, application)
print('Http on port %s.' % port)
try:
httpd.serve_forever()
except KeyboardInterrupt:
GPIO.cleanup()
テキストを入力させたい時は
------------------------------------------------------------
HTMLの部分を書き換えて
html = """
<form method="post">State:
<input type="text" name = "state" value = "%s">
<input type="submit" name="button" value ="Set">
</form>
"""
形は同じ
形は同じ
req = Request(environ)
if req.path == '/':
state = int(req.params.get('state', '0'))
全ソースは
全ソースは
import time
import RPi.GPIO as GPIO
from webob import Request, Response
bcmled = 12
GPIO.setmode(GPIO.BOARD) #BOARD 12 BCM 18
GPIO.setup(bcmled,GPIO.OUT) #出力
#htmlteigi
html = """
<form method="post">State:
<input type="text" name = "state" value = "%s">
<input type="submit" name="button" value ="Set">
</form>
"""
class WebApp(object):
def __call__(self, environ, start_response):
req = Request(environ)
# print ('req: %s...' % req.path )
if req.path == '/':
state = int(req.params.get('state', '0'))
GPIO.output(bcmled, state)
resp = Response(html % str(state))
else:
resp = Response()
return resp(environ ,start_response)
application = WebApp()
if __name__ == '__main__':
from wsgiref.simple_server import make_server
port = 8080
httpd = make_server('',port,application)
print ('serving http port %s...' % port)
try:
httpd.serve_forever() #serverkidou
except KeyboardInterrupt:
GPIO.cleanup()
PR