jpskill.com
📦 その他 コミュニティ

mqtt

Lightweight pub/sub protocol for efficient IoT communication and telemetry

⚡ おすすめ: コマンド1行でインストール(60秒)

下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。

🍎 Mac / 🐧 Linux
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o mqtt.zip https://jpskill.com/download/22233.zip && unzip -o mqtt.zip && rm mqtt.zip
🪟 Windows (PowerShell)
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/22233.zip -OutFile "$d\mqtt.zip"; Expand-Archive "$d\mqtt.zip" -DestinationPath $d -Force; ri "$d\mqtt.zip"

完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。

💾 手動でダウンロードしたい(コマンドが難しい人向け)
  1. 1. 下の青いボタンを押して mqtt.zip をダウンロード
  2. 2. ZIPファイルをダブルクリックで解凍 → mqtt フォルダができる
  3. 3. そのフォルダを C:\Users\あなたの名前\.claude\skills\(Win)または ~/.claude/skills/(Mac)へ移動
  4. 4. Claude Code を再起動

⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。

🎯 このSkillでできること

下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。

📦 インストール方法 (3ステップ)

  1. 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
  2. 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
  3. 3. 展開してできたフォルダを、ホームフォルダの .claude/skills/ に置く
    • · macOS / Linux: ~/.claude/skills/
    • · Windows: %USERPROFILE%\.claude\skills\

Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。

詳しい使い方ガイドを見る →
最終更新
2026-05-18
取得日時
2026-05-18
同梱ファイル
1

📖 Skill本文(日本語訳)

※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。

mqtt

目的

このスキルは、AIが軽量なpub/subプロトコルであるMQTTと連携し、効率的なIoT通信を可能にします。テレメトリーデータの公開やデバイスの更新購読に使用し、制約のある環境でのリアルタイムなインタラクションを実現します。

使用する場面

このスキルは、IoTデバイスからのセンサーデータ送信、リモートデバイス制御、テレメトリー監視など、低帯域幅でリアルタイムなメッセージングが必要なシナリオで使用します。ファイル転送のような高スループットが必要な場合は、HTTPの方が適しているため避けてください。

主な機能

  • 設定可能なQuality of Service (QoS) レベル (0, 1, または 2) でトピックにメッセージを公開します。
  • トピックを購読し、受信メッセージを非同期で処理します。
  • リテインドメッセージ、ラストウィルアンドテスタメント、ワイルドカード購読(例: すべてのサブトピックに対する"#")をサポートします。
  • TLSおよびユーザー名/パスワードなどの認証方法によるセキュアな接続をサポートします。

使用パターン

MQTTを使用するには、まずクライアントライブラリを使用してブローカーに接続します。認証情報で接続を確立し、その後公開または購読を行います。例えば、ループ内で接続、購読、メッセージ処理、切断を行います。ブローカーアドレスの$MQTT_BROKER_URLのような機密データには環境変数を使用します。不安定なネットワークの場合には、常に再接続を処理してください。

一般的なコマンド/API

PythonでのインタラクションにはPaho MQTTライブラリを使用します。以下のようにインポートします。

import paho.mqtt.client as mqtt

ブローカーに接続するには:

  • コマンド: client.connect(broker_address, port=1883)
  • 認証付きの例: client.username_pw_set(username='$MQTT_USERNAME', password='$MQTT_PASSWORD')

メッセージを公開するには:

  • API: client.publish(topic, payload, qos=1)
  • 例: client.publish('sensors/temp', '22.5', qos=1)

購読するには:

  • API: client.subscribe(topic, qos=1)
  • 例: client.subscribe('commands/device1', qos=1)

設定形式: 設定にはJSONファイルを使用します。例: {"broker": "$MQTT_BROKER_URL", "topic": "sensors/temp"}mosquitto_pubのようなCLIツールはサブプロセス経由で呼び出すことができます。例: subprocess.run(['mosquitto_pub', '-h', '$MQTT_BROKER_URL', '-t', 'sensors/temp', '-m', '22.5'])

統合に関する注意点

MQTTをAIエージェントのコードにラップして統合します。キーには環境変数を設定します。例えば、トークンベースの認証には$MQTT_API_KEYをエクスポートします。複数のサービスをセットアップする場合は、HiveMQやMosquittoのようなブローカーを使用し、client.connect('$MQTT_BROKER_URL')で接続します。セキュアなエンドポイントにはTLSを確保してください(例: client.tls_set())。データベースのような他のスキルと組み合わせる場合は、成功時にデータを公開します。例えば、DBに挿入した後、client.publish('db/updates', json.dumps(data))を実行します。

エラー処理

一般的なエラーは明示的に処理してください。接続の問題については、ConnectionRefusedErrorをキャッチし、指数関数的バックオフで再試行します。例: time.sleep(5)。公開の失敗については、戻りコードを確認し(例: client.publish()が0より大きい値を返した場合、ログに記録して再試行)、購読エラーは無効なトピックが原因である可能性があります。if '/' not in topic: raise ValueError('Invalid topic')で検証します。on_messageハンドラーのように、メッセージエラーにはコールバックを使用します。例: if error: print('Error: ', error)。finallyブロックでclient.disconnect()を使用して常にクリーンアップしてください。

具体的な使用例

例1: センサーデータの公開 MQTTトピックに温度測定値を公開するには、まず環境変数を設定します。export MQTT_BROKER_URL='broker.example.com'。次に、コード内で以下のようにします。

client = mqtt.Client()
client.connect(os.environ['MQTT_BROKER_URL'])
client.publish('sensors/temperature', '25.0', qos=1)
client.disconnect()

これはIoTダッシュボードの更新のためにブローカーにメッセージを送信します。

例2: デバイスコマンドの購読 コマンドを購読して反応するには、以下を使用します。

def on_message(client, userdata, message):
    print(f"Received: {message.payload.decode()}")
client = mqtt.Client()
client.on_message = on_message
client.connect(os.environ['MQTT_BROKER_URL'])
client.subscribe('commands/device1', qos=1)
client.loop_forever()

これは、IoTデバイスを制御するためのコマンドをクライアントがリッスンし続けるようにします。

グラフの関係

  • クラスターに関連: iot
  • タグに関連: iot, mqtt
  • 接続: データ処理やデバイス管理など、他のiotスキルと統合できます。
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開

mqtt

Purpose

This skill allows the AI to interact with MQTT, a lightweight pub/sub protocol, for efficient IoT communication. Use it to publish telemetry data or subscribe to device updates, enabling real-time interactions in constrained environments.

When to Use

Use this skill for scenarios requiring low-bandwidth, real-time messaging, such as sending sensor data from IoT devices, remote device control, or telemetry monitoring. Avoid it for high-throughput needs like file transfers, where HTTP is more suitable.

Key Capabilities

  • Publish messages to topics with configurable Quality of Service (QoS) levels (0, 1, or 2).
  • Subscribe to topics and handle incoming messages asynchronously.
  • Support for retained messages, last will and testament, and wildcard subscriptions (e.g., "#" for all subtopics).
  • Secure connections via TLS and authentication methods like username/password.

Usage Patterns

To use MQTT, first connect to a broker using a client library. Establish a connection with credentials, then publish or subscribe. For example, in a loop: connect, subscribe, process messages, and disconnect. Use environment variables for sensitive data, like $MQTT_BROKER_URL for the broker address. Always handle reconnections for unstable networks.

Common Commands/API

Use the Paho MQTT library for Python interactions. Import it as follows:

import paho.mqtt.client as mqtt

To connect to a broker:

  • Command: client.connect(broker_address, port=1883)
  • Example with auth: client.username_pw_set(username='$MQTT_USERNAME', password='$MQTT_PASSWORD')

To publish a message:

  • API: client.publish(topic, payload, qos=1)
  • Example: client.publish('sensors/temp', '22.5', qos=1)

To subscribe:

  • API: client.subscribe(topic, qos=1)
  • Example: client.subscribe('commands/device1', qos=1)

Config format: Use a JSON file for settings, e.g., {"broker": "$MQTT_BROKER_URL", "topic": "sensors/temp"}. CLI tools like mosquitto_pub can be invoked via subprocess: subprocess.run(['mosquitto_pub', '-h', '$MQTT_BROKER_URL', '-t', 'sensors/temp', '-m', '22.5']).

Integration Notes

Integrate MQTT by wrapping it in your AI agent's code. Set environment variables for keys, e.g., export $MQTT_API_KEY for token-based auth. For multi-service setups, use a broker like HiveMQ or Mosquitto; connect via client.connect('$MQTT_BROKER_URL'). Ensure TLS for secure endpoints (e.g., client.tls_set()). If combining with other skills, like a database, publish data on success: e.g., after inserting into a DB, run client.publish('db/updates', json.dumps(data)).

Error Handling

Handle common errors explicitly. For connection issues, catch ConnectionRefusedError and retry with exponential backoff: e.g., time.sleep(5). For publish failures, check return codes (e.g., if client.publish() returns >0, log and retry). Subscription errors might involve invalid topics; validate with if '/' not in topic: raise ValueError('Invalid topic'). Use callbacks for message errors, like in on_message handler: if error: print('Error: ', error). Always clean up with client.disconnect() in a finally block.

Concrete Usage Examples

Example 1: Publish sensor data To publish a temperature reading to an MQTT topic, first set env vars: export MQTT_BROKER_URL='broker.example.com'. Then, in code:

client = mqtt.Client()
client.connect(os.environ['MQTT_BROKER_URL'])
client.publish('sensors/temperature', '25.0', qos=1)
client.disconnect()

This sends a message to the broker for IoT dashboard updates.

Example 2: Subscribe to device commands To subscribe and react to commands, use:

def on_message(client, userdata, message):
    print(f"Received: {message.payload.decode()}")
client = mqtt.Client()
client.on_message = on_message
client.connect(os.environ['MQTT_BROKER_URL'])
client.subscribe('commands/device1', qos=1)
client.loop_forever()

This keeps the client listening for commands to control an IoT device.

Graph Relationships

  • Related to cluster: iot
  • Related to tags: iot, mqtt
  • Connections: Can integrate with other iot skills, such as for data processing or device management.