Back to Picoclaw

📡 MQTT Channel

docs/channels/mqtt/README.md

0.2.94.3 KB
Original Source

📡 MQTT Channel

PicoClaw supports any MQTT client as a chat channel. Devices or services publish requests to a broker; PicoClaw subscribes, processes them, and publishes responses back.

🚀 Quick Start

1. Add the channel to ~/.picoclaw/config.json:

json
{
  "channel_list": {
    "mqtt": {
      "enabled": true,
      "type": "mqtt",
      "settings": {
        "broker": "tcp://localhost:1883",
        "agent_id": "assistant"
      }
    }
  }
}

2. Start the gateway:

bash
picoclaw gateway

3. Send a message from any MQTT client:

bash
mosquitto_pub -t "/picoclaw/assistant/device1/request" \
  -m '{"text": "What is the CPU usage?"}'

4. Subscribe to receive the response:

bash
mosquitto_sub -t "/picoclaw/assistant/device1/response"

📨 Topic Structure

{prefix}/{agent_id}/{client_id}/request    # Client → PicoClaw
{prefix}/{agent_id}/{client_id}/response   # PicoClaw → Client
SegmentDescription
prefixTopic prefix, configured server-side. Default: /picoclaw
agent_idPicoClaw instance identifier, set in agent_id config field
client_idClient-defined session identifier — use a stable ID per device to maintain conversation context

Message Payload (JSON)

json
{ "text": "your message here" }

⚙️ Configuration

config.json

json
{
  "channel_list": {
    "mqtt": {
      "enabled": true,
      "type": "mqtt",
      "settings": {
        "broker": "ssl://your-broker:8883",
        "agent_id": "assistant",
        "topic_prefix": "/picoclaw",
        "client_id": "",
        "keep_alive": 60,
        "qos": 0
      }
    }
  }
}

.security.yml (credentials)

Username and password are stored in ~/.picoclaw/.security.yml, not in config.json:

yaml
channel_list:
  mqtt:
    settings:
      username: your_username
      password: your_password

Configuration Fields

FieldLocationRequiredDefaultDescription
brokersettingsYesMQTT broker URL, e.g. tcp://host:1883, ssl://host:8883
agent_idsettingsYesAgent identifier, used as part of the topic path
topic_prefixsettingsNo/picoclawTopic namespace prefix
username.security.ymlNoBroker authentication username
password.security.ymlNoBroker authentication password
client_idsettingsNoauto-generatedPaho client ID sent to the broker. Auto-generated as picoclaw-mqtt-{agent_id}-{8-char hex} if not set; stays fixed for the process lifetime so reconnects reuse the same ID
keep_alivesettingsNo60MQTT keepalive interval in seconds
qossettingsNo0QoS level for publish and subscribe: 0, 1, or 2

Environment Variables

All fields can be set via environment variables:

VariableField
PICOCLAW_CHANNELS_MQTT_BROKERbroker
PICOCLAW_CHANNELS_MQTT_AGENT_IDagent_id
PICOCLAW_CHANNELS_MQTT_TOPIC_PREFIXtopic_prefix
PICOCLAW_CHANNELS_MQTT_USERNAMEusername
PICOCLAW_CHANNELS_MQTT_PASSWORDpassword
PICOCLAW_CHANNELS_MQTT_CLIENT_IDclient_id
PICOCLAW_CHANNELS_MQTT_KEEP_ALIVEkeep_alive
PICOCLAW_CHANNELS_MQTT_QOSqos

🔄 Reconnection

PicoClaw automatically reconnects to the broker if the connection is lost, with a 5-second retry interval. On reconnect, the subscription is re-established automatically. The broker-side client ID stays the same across reconnects so the broker correctly identifies it as the same session.


⚠️ Notes

  • TLS: SSL/TLS is supported (ssl:// broker URL). Certificate verification is skipped by default.
  • Streaming: Streaming responses send multiple messages to the response topic; concatenate them in order.
  • client_id vs session ID: The client_id in the topic path is set by your client application and identifies the conversation session. It is separate from the broker-level client ID used by PicoClaw's paho connection.
  • Multiple instances: If you run multiple PicoClaw instances against the same broker with the same agent_id, set distinct client_id values to avoid broker-level conflicts.