MojoScale Studio Docs
API Reference

mqtt

MQTT native Berry module.

Studio Docs Networking

import mqtt

The mqtt module connects firmware scripts to a broker supplied by the script. Studio does not provide a managed MQTT profile; pass at least host and port when calling mqtt.connect().

Quick example

import mqtt
mqtt.connect("broker.example.com", 1883)
mqtt.publish("temperature", "72")
mqtt.subscribe("led", 0)

importmqtt

MQTT native Berry module.

The mqtt module connects firmware scripts to a broker supplied by the script. Studio does not provide a managed MQTT profile; pass at least host and port when calling mqtt.connect().

API 6 available

mqtt.connect(host: str, port: int, username: str = None, password: any = None, client_id: any = None, cert_pem: str = None, keepalive: any = None, clean_session: any = None) -> any

Connect to an MQTT broker.

Parameters
Name Type Pass as Required Description
host str positional or keyword Yes Broker host name or IP address.
port int positional or keyword Yes Broker TCP/TLS port.
username str positional or keyword No Optional broker username.
password any positional or keyword No Optional broker password.
client_id any positional or keyword No Optional MQTT client id.
cert_pem str positional or keyword No Optional certificate PEM placeholder accepted by firmware compatibility calls.
keepalive any positional or keyword No Optional keepalive in seconds.
clean_session any positional or keyword No Optional clean session flag.
Returns

any Native connection result.

mqtt.publish(topic: any, qos: any = None, retain: any = None, *payload: any) -> any

Publish an MQTT message.

Parameters
Name Type Pass as Required Description
topic any positional or keyword Yes MQTT topic.
qos any positional or keyword No Optional quality of service level.
retain any positional or keyword No Optional retain flag.
payload any extra positional No Message payload.
Returns

any Native publish result.

mqtt.subscribe(topic: any, qos: any = None) -> any

Subscribe to an MQTT topic.

Parameters
Name Type Pass as Required Description
topic any positional or keyword Yes MQTT topic filter.
qos any positional or keyword No Optional quality of service level.
Returns

any Native subscribe result.

mqtt.on_connect(callback: callback) -> str

Register a callback for MQTT connection.

Parameters
Name Type Pass as Required Description
callback callback positional or keyword Yes Function name or callable accepted by firmware.
Returns

str Native registration result.

mqtt.on_disconnect(callback: callback) -> str

Register a callback for MQTT disconnection.

Parameters
Name Type Pass as Required Description
callback callback positional or keyword Yes Function name or callable accepted by firmware.
Returns

str Native registration result.

mqtt.on_message(callback: callback) -> str

Register a callback for inbound MQTT messages.

Parameters
Name Type Pass as Required Description
callback callback positional or keyword Yes Function name or callable accepted by firmware.
Returns

str Native registration result.