皆さんこんにちは、ITAです。
今回はRaspberry Pi Pico Wを使って、Wi-Fi経由でLEDを遠隔操作するプロジェクトに挑戦しました。
今回は、LEDのON/OFFをWebブラウザから操作できる仕組みを作ります。
MicroPythonを使い、簡単なWebサーバーを立てて操作できるようにします。特に、スマートフォンからも操作できるようにUIを工夫しました。
必要なもの
- Raspberry Pi Pico W
- LED(内蔵LEDも使用可)
- ジャンパーワイヤー(外部LEDを使用する場合)
- Wi-Fi接続環境
- MicroPython(インストール済み)
- パソコン(Thonnyなどの開発環境)
コード
from machine import Pin
import network
import socket
import time
# 内蔵LED(GPIO16)
led = Pin(16, Pin.OUT)
# Wi-Fi設定
ssid = 'あなたのSSID'
password = 'あなたのパスワード'
# Wi-Fi接続
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
print("Wi-Fiに接続中...")
while not wlan.isconnected():
time.sleep(1)
ip = wlan.ifconfig()[0]
print('接続成功! IPアドレス:', ip)
# HTMLページ(LED制御ボタン付き)
html = """<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body { text-align:center; font-family:sans-serif; }
button {
width: 80%%; padding: 20px; font-size: 20px; margin: 10px;
border-radius: 10px; border: none;
}
.on { background: green; color: white; }
.off { background: red; color: white; }
</style>
</head>
<body>
<h1>LED Control</h1>
<form action="/led/on"><button class="on">LED ON</button></form>
<form action="/led/off"><button class="off">LED OFF</button></form>
</body>
</html>
"""
# Webサーバー起動
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
server = socket.socket()
server.bind(addr)
server.listen(1)
print('Webサーバー起動中... http://%s' % ip)
while True:
conn, addr = server.accept()
print('接続:', addr)
request = conn.recv(1024)
request_str = request.decode()
request_line = request_str.split('\n')[0]
print("リクエスト行:", request_line)
# LED制御
if 'GET /led/on' in request_line:
print("LED ON")
led.value(1)
elif 'GET /led/off' in request_line:
print("LED OFF")
led.value(0)
# HTMLレスポンス送信
conn.send('HTTP/1.1 200 OK\n')
conn.send('Content-Type: text/html\n')
conn.send('Connection: close\n\n')
conn.sendall(html)
conn.close()
やってみた
main.pyで保存して自動実行されるようにしました。
手順
1.WIFI設定
Wi-Fi設定をするために、ご自分のネットワークのSSIDとパスワードを入力します。
ssid = 'あなたのSSID'
password = 'あなたのパスワード'
2.HTMLとWebページ作成
Pico WがWi-Fiに接続できたら、Webサーバーを立てて、HTMLでLEDを制御するためのインターフェースを作成します。
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body { text-align:center; font-family:sans-serif; }
button {
width: 80%; padding: 20px; font-size: 20px; margin: 10px;
border-radius: 10px; border: none;
}
.on { background: green; color: white; }
.off { background: red; color: white; }
</style>
</head>
<body>
<h1>LED Control</h1>
<form action="/led/on"><button class="on">LED ON</button></form>
<form action="/led/off"><button class="off">LED OFF</button></form>
</body>
</html>
これで、ボタンをクリックすることでLEDを制御できるWebページが作成されます。
3.ソケットサーバーを作成
Webサーバーを起動し、接続を待ち受けます。ユーザーがLEDを操作するためのリクエストを受け取ったら、LEDをON/OFFに切り替えます。
# Webサーバー起動
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
server = socket.socket()
server.bind(addr)
server.listen(1)
print('Webサーバー起動中... http://%s' % ip)
while True:
conn, addr = server.accept()
print('接続:', addr)
request = conn.recv(1024)
request_str = request.decode()
request_line = request_str.split('\n')[0]
print("リクエスト行:", request_line)
# LED制御
if 'GET /led/on' in request_line:
print("LED ON")
led.value(1)
elif 'GET /led/off' in request_line:
print("LED OFF")
led.value(0)
# HTMLレスポンス送信
conn.send('HTTP/1.1 200 OK\n')
conn.send('Content-Type: text/html\n')
conn.send('Connection: close\n\n')
conn.sendall(html)
conn.close()
動作確認
Pico WがWi-Fiに接続されると、Pico WのIPアドレスが表示されます。このIPアドレスをブラウザに入力すると、LEDをON/OFFするためのボタンが表示されます。ボタンを押すことで、LEDの状態を簡単に切り替えることができます。
まとめ
今回のプロジェクトで、Raspberry Pi Pico Wを使ってWi-Fi経由でLEDを遠隔操作する方法を学びました。
簡単なHTMLとMicroPythonを使うことで、スマートフォンやiPadからも操作可能なLEDコントロールシステムを作ることができました。
今後は、LEDの状態をWebページに表示したり、他のデバイスと連携させるアイデアも試してみたいと思います。
コメント