Created
October 26, 2025 06:36
-
-
Save vestige/39c8ee08b834fb76ab3543e50e6964ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from machine import Pin | |
| import socket | |
| import network | |
| import time | |
| import rp2 | |
| #無線LANへの接続 | |
| def connect_to_wifi(ssid, password): | |
| wlan = network.WLAN(network.STA_IF) | |
| wlan.active(True) | |
| wlan.connect(ssid, password) | |
| max_wait = 10 | |
| while max_wait > 0: | |
| if wlan.status() < 0 or wlan.status() >= 3: | |
| break | |
| max_wait -= 1 | |
| print('接続待ち...') | |
| time.sleep(1) | |
| if wlan.status() != 3: | |
| raise RuntimeError('ネットワーク接続失敗') | |
| else: | |
| print('接続完了') | |
| status = wlan.ifconfig() | |
| print('IPアドレス = ' + status[0]) | |
| #自宅Wi-FiのSSIDとパスワードを入力 | |
| ssid = 'YOUR NETWORK SSID' | |
| password = 'YOUR NETWORK PASSWORD' | |
| # Wi-Fiに接続 | |
| connect_to_wifi(ssid, password) | |
| # サーバーのIPアドレスとポート番号を設定 | |
| server_ip = "192.168.1.90" # サーバーのIPアドレスに変更 | |
| port = 80 # ポート番号は80 | |
| # ソケットを作成 | |
| clientSocket = socket.socket() | |
| # サーバーに接続 | |
| clientSocket.connect((server_ip, port)) | |
| last_sent_time = 0 | |
| while True: | |
| current_time = time.time() | |
| # Pico Wのブートセルボタンが押されたときの処理 | |
| if rp2.bootsel_button() == 1: | |
| Pin('LED', Pin.OUT).on() | |
| # 最後に送信してから1秒以上経過している場合 | |
| if current_time - last_sent_time > 1: | |
| clientSocket.send(b'1') # サーバーに「1」を送信 | |
| last_sent_time = current_time # 最後に送信した時間を更新 | |
| else: | |
| Pin('LED', Pin.OUT).off() | |
| time.sleep(0.1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment