忍者ブログ
[262]  [261]  [260]  [259]  [258]  [257]  [256]  [255]  [254]  [253]  [252
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

準備もできたので

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
カレンダー
03 2025/04 05
S M T W T F S
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
フリーエリア
最新CM
[03/10 DORA]
最新TB
プロフィール
HN:
dorabu
性別:
非公開
バーコード
ブログ内検索
P R
Copyright © ドラブーのアンドロイドとIoTなブログ All rights reserved. / Template by 四季. / Material by てんせん.

忍者ブログ [PR]