# Display the color of a lit LED on a web page
# 2023.09.13 by Seshat
import threading # To perform ON/OFF detection of switches in parallel
import time # To synchronize events by waiting
import RPi.GPIO as GPIO # For handling GPIO
from webob import Request, Response # To communicate via the web
gpio22 = 15 # for Switch
# It makes sense that PIN numbers are sequential
gpio17 = 11 # for G
gpio18 = 12 # for Y
gpio27 = 13 # for R
stat = gpio17 # LED's info [Global]
color = 'GREEN' # LED's info [Global]
# Each time a button is pressed, the three LEDs light up in sequence
# like a traffic light
class Sw(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(gpio22, GPIO.IN)
GPIO.setup(gpio17, GPIO.OUT)
GPIO.setup(gpio18, GPIO.OUT)
GPIO.setup(gpio27, GPIO.OUT)
GPIO.output(gpio17, GPIO.HIGH);
GPIO.output(gpio18, GPIO.LOW);
GPIO.output(gpio27, GPIO.LOW);
self.daemon = True
self.start()
def run(self):
global stat # Pin number to which the lit LED is connected
global color # Color of lit LEDs
# At the moment the button is pressed,
# the LED that was lit turns off and the next LED is turned on.
while True:
# Wait for the switch to be pressed
while GPIO.input(gpio22) == True:
time.sleep(0.01)
# print('Turn ON')
# Wait for the unstable state (chatter) when the
# button is pressed to stabilize
time.sleep(0.02)
# Turn off the currently lit LED
GPIO.output(stat, GPIO.LOW);
# Select the LED to light up
stat += 1
if stat > gpio27:
stat = gpio17
# Change the color of the LED
GPIO.output(stat, GPIO.HIGH);
# Set the color of the currently lit LED
if stat == gpio17:
color = 'GREEN'
elif stat == gpio18:
color = 'YELLOW'
else:
color = 'RED'
# Wait until your hand is off the switch
while GPIO.input(gpio22) == False:
time.sleep(0.01)
# print('Turn OFF')
# Wait for the unstable state (chatter) when the
# button is released to stabilize
time.sleep(0.02)
# Returns the color of the currently lit LED in html in response
# to a web page update request
class WebSrv(object):
def __call__(self, environ, start_response):
global stat
req = Request(environ)
html = "<p>LED color=%s</p>"
resp = Response(html % color)
return resp(environ, start_response)
srv = WebSrv()
sw = Sw()
if __name__ == '__main__':
from wsgiref.simple_server import make_server
port = 8080
httpd = make_server('', port, srv)
print('Serving HTTP on port' , port)
httpd.serve_forever()
プログラムを実行するには
コードを記述したファイル名を”ledClr2web.py”とします。 Raspberry Pi OSなどの端末から、次のように実行します。
^CTraceback (most recent call last):
File "/home/linux/worx/Python/sw_web/ledClr2web.py", line 99, in <module>
httpd.serve_forever()
File "/usr/lib/python3.9/socketserver.py", line 232, in serve_forever
ready = selector.select(poll_interval)
File "/usr/lib/python3.9/selectors.py", line 416, in select
fd_event_list = self._selector.poll(timeout)
KeyboardInterrupt
結局プログラムで何をしているか
以下では、プログラムを短く切り取って詳しい説明をします。
import threading # To perform ON/OFF detection of switches in parallel
import time # To synchronize events by waiting
import RPi.GPIO as GPIO # For handling GPIO
from webob import Request, Response # To communicate via the web
次の機能が使えるようにインポートしています。
スイッチを扱う処理をスレッドにする(threading)
イベント待ちのため「時間」に関する機能(time)を取り込む
GPIOを使用可能にする
Webサービスとしてブラウザの要求に対して応答する
gpio22 = 15 # for Switch
# It makes sense that PIN numbers are sequential
gpio17 = 11 # for G
gpio18 = 12 # for Y
gpio27 = 13 # for R
stat = gpio17 # LED's info [Global]
color = 'GREEN' # LED's info [Global]
# Each time a button is pressed, the three LEDs light up in sequence
# like a traffic light
class Sw(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
# At the moment the button is pressed,
# the LED that was lit turns off and the next LED is turned on.
while True:
# Wait for the switch to be pressed
while GPIO.input(gpio22) == True:
time.sleep(0.01)
# print('Turn ON')
# Wait for the unstable state (chatter) when the
# button is released to stabilize
time.sleep(0.02)
ボタンを離した時も、押したとき同様に不安定な状態になることがあるため、安定するのを待ちます。
# Returns the color of the currently lit LED in html in response
# to a web page update request
class WebSrv(object):
def __call__(self, environ, start_response):
global stat
req = Request(environ)
html = "<p>LED Color=%s</p>"
resp = Response(html % color)
return resp(environ, start_response)
/home/linux/worx/Python/sw_web/ledClr2web.py:26: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(gpio17, GPIO.OUT)
実行したコマンドについて補足説明しておきます。 ioregコマンドの c オプションは、確認したいデバイス等対象のインスタンス名(上記の場合は、”AppleSmartBattery”)があらかじめわかっている場合、それを指定するためのものです。インスタンスを指定して出力を絞り込んでも、多くの情報が出てきてしまうので、grepコマンドでフィルタリングしています。grepの i オプションは、大小文字区別せず検索し、v オプションはヒットする行を除外する指示です。
Windows 11 Proではパスワードを無期限にする設定がGUIからできます。 具体的には、「コンピューターの管理」画面の[システムツール ]>[ローカルユーザーとグループ]>[ユーザー]から、ユーザ名の一覧を表示します。設定対象のユーザ名を右クリックし、[プロパティ]を選択して表示された画面から、下図の通り「パスワードを無期限にする」チェックを入れます。