2026年10月29日木曜日

CircuitPython おさらい

fseiigojp@fseigojpPC:~$  source circup-env/bin/activate

(circup-env) fseiigojp@fseigojpPC:~$ circup  adafruit_requests

Usage: circup [OPTIONS] COMMAND [ARGS]...

Try 'circup --help' for help.


Error: No such command 'adafruit_requests'.

(circup-env) fseiigojp@fseigojpPC:~$ circup install adafruit_requests

Found device raspberry_pi_pico2_w at /run/media/fseiigojp/CIRCUITPY, running CircuitPython 10.3.1.

Downloading 'py' bundle for adafruit/Adafruit_CircuitPython_Bundle (20261028).

py:

Extracting:  [####################################]  100%          


OK


Downloading '10mpy' bundle for adafruit/Adafruit_CircuitPython_Bundle (20261028).

10.x-mpy:

Extracting:  [####################################]  100%          


OK


Using latest bundle for adafruit/Adafruit_CircuitPython_Bundle (20261028).

Using latest bundle for adafruit/CircuitPython_Community_Bundle (20261008).

Searching for dependencies for: ['adafruit_requests']

Ready to install: ['adafruit_connection_manager', 'adafruit_requests']


Installed 'adafruit_connection_manager'.

Installed 'adafruit_requests'.

(circup-env) fseiigojp@fseigojpPC:~$ 

以後は同一LANでかつ2.5GHz帯であることが重要

ーーーーーーーーーーーーーーーーーーーーーーーーー

settings.toml (設定ファイル)

同じくルート(CIRCUITPY ドライブの直下)に配置してください。これがないとWi-Fiに接続できません。

CIRCUITPY_WIFI_SSID = "あなたのWi-FiのSSID"
CIRCUITPY_WIFI_PASSWORD = "あなたのWi-Fiのパスワード"

ーーーcode.pyでmonitorはthonnyをもちいるーーーーーーーーーーー

import os

import ssl

import wifi

import socketpool

import adafruit_requests

# ----------------------------------------------------

# 1. Wi-Fiへの接続

# ----------------------------------------------------

print("Wi-Fiに接続中...")

try:

    # settings.toml からWi-Fi情報を読み込んで接続

    wifi.radio.connect(

        os.getenv("CIRCUITPY_WIFI_SSID"),

        os.getenv("CIRCUITPY_WIFI_PASSWORD")

    )

    print(f"接続成功! IPアドレス: {wifi.radio.ipv4_address}")

except Exception as e:

    print(f"Wi-Fi接続エラー: {e}")

    raise

# ----------------------------------------------------

# 2. ネットワークセッションのセットアップ

# --------------httpsのみでなくhttpにもアクセスできる--------------------------------------

pool = socketpool.SocketPool(wifi.radio)

ssl_context = ssl.create_default_context()

requests = adafruit_requests.Session(pool, ssl_context)

# ----------------------------------------------------

# 3. HTTP GET リクエストの送信

# ----------------------------------------------------

# テスト用のURL(JSONデータを返すパブリックAPI)

URL = "http://192.168.0.10:3000"

print(f"GETリクエストを送信中: {URL}")

try:

    response = requests.get(URL)

    print(f"ステータスコード: {response.status_code}")

    print("-" * 40)

    # パターンA: レスポンスを「テキスト」としてそのまま表示する場合

    print("【テキストとして表示】")

    print(response.text)

    print("-" * 40)

    # パターンB: レスポンスを「JSON」として解析して特定のデータを抜く場合

    print("【JSONとして解析して表示】")

    data = response.json()

    print(f"送信元IP (origin): {data.get('origin')}")

    print(f"ユーザーエージェント (headers): {data.get('headers', {}).get('User-Agent')}")

    # 使い終わったらクローズ

    response.close()

except Exception as e:

    print(f"リクエストエラー: {e}")

print("-" * 40)

print("処理が完了しました。")


0 件のコメント:

コメントを投稿