0
0

ミーティング中を検知して周囲の人間に知らせる(2)

Posted at

ミーティング中を検知して周囲の人間に知らせる(2)

今回はGoを利用して、LEDを光らしていく―――
予定だったのですが、以前作成したRaspberryPI開発用のDockerではPythonでしかGPIOをエミュレートできなさそうでした。
なので、今回はPythonを利用してLEDを光らしていきます。
(また番外編でGo+DockerでGPIOをエミュレートするのに挑戦したいと思います)

構成

PythonでWebサーバーを立ち上げ、GETリクエストでLEDをON/OFFする

回路

以下回路図です。
誰がなんと言おうと回路図です。

image-8.png

ラズパイから1000Ωの抵抗を挟んでLEDに通しているだけです。

コード

さすがPython、直感的でスマートですね。

from gpiozero import LED
from http.server import BaseHTTPRequestHandler, HTTPServer
import threading

class RequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == '/led/on':
            led.on()
            self.send_response(200)
            self.end_headers()
            self.wfile.write(b"LED ON")
        elif self.path == '/led/off':
            led.off()
            self.send_response(200)
            self.end_headers()
            self.wfile.write(b"LED OFF")
        else:
            self.send_response(404)
            self.end_headers()
            self.wfile.write(b"Not Found")

def run_server():
    server_address = ('', 8000)
    httpd = HTTPServer(server_address, RequestHandler)
    httpd.serve_forever()

led = LED(17)

server_thread = threading.Thread(target=run_server)
server_thread.daemon = True
server_thread.start()

print("Server started at http://localhost:8000")
print("Send GET request to http://localhost:8000/led/on to turn LED on")
print("Send GET request to http://localhost:8000/led/off to turn LED off")

server_thread.join()

動作確認

上記で建てたサーバーに以下の感じでCURLでリクエストするとLEDが光ります。

curl http://localhost:8000/led/on # LEDオン
curl http://localhost:8000/led/off # LEDオフ

IMG_2971.gif

きれいですね。

終わり

Goで全部やれると思っていたが、甘かった。
必ずリベンジしてやる、、、

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0