MQTT Client Configuration

mPython IoT implements MQTT client functionality via the umqtt.simple module. This page describes the MQTT client configuration parameters, connection options, and topic naming conventions.

Constructor

class umqtt.simple.MQTTClient(client_id, server, port=0, user=None, password=None, keepalive=0, ssl=False, ssl_params={})
  • client_id – Unique identifier for the MQTT client; must not be duplicated on the same broker

  • server – IP address or domain name of the MQTT broker

  • port – Server port number, typically 1883; may vary by platform

  • user – Username for MQTT authentication (optional)

  • password – Password for MQTT authentication (optional)

  • keepalive – Connection keepalive interval (seconds). If no communication packets (subscribe, publish, etc.) are sent within this interval, the connection is automatically dropped. 0 means disabled

  • ssl – Whether to enable SSL/TLS encrypted connection, default False

  • ssl_params – SSL connection parameters (dict type)

Configuration Parameters

Broker Address

The server parameter specifies the MQTT broker address. For local networks, use the server IP (e.g. 192.168.1.100); for public platforms, use the domain name.

Port Number

The port parameter specifies the MQTT service port. The standard plaintext port is 1883; the encrypted port is typically 8883. Refer to the platform documentation for specifics.

Client ID

client_id uniquely identifies a client connection. If multiple devices connect to the same broker with the same client_id, they will compete for the connection causing frequent disconnections. Ensure uniqueness.

Attention

In multi-node networking, assign a different client_id to each mPython IoT device to avoid connection conflicts.

Keepalive Interval

keepalive maintains a long-lived connection. Once set, the client must communicate with the server within this interval (can be achieved via periodic ping()); otherwise the connection will be dropped.

QoS Levels

MQTT defines three Quality of Service (QoS) levels:

QoS

Description

0

At most once. Message is sent without acknowledgment and may be lost; highest efficiency

1

At least once. Message is guaranteed to arrive but may be duplicated

2

Exactly once. Message is guaranteed to arrive exactly once; highest overhead

Note

To keep the code footprint small, umqtt.simple only supports QoS 0 and QoS 1; QoS 2 is not supported.

Topic Naming Conventions

Topics use a hierarchical structure with / as the level separator. For multi-node management, a consistent naming convention is recommended:

  • Hierarchical by area/device/data: e.g. classroom/node1/temperature

  • Unique prefix to distinguish projects: e.g. school_project/classroom/data

  • Separate naming for control topics: e.g. classroom/node1/led

Topic Wildcards

Wildcards can be used when subscribing to match multiple topics:

  • + matches a single level, e.g. classroom/+/data matches classroom/node1/data, classroom/node2/data

  • # matches multiple levels, e.g. classroom/# matches all sub-topics under classroom

Attention

Wildcards can only be used for subscribing ( subscribe ), not for publishing ( publish ). Publishing must use a specific topic name.