Skip to content

MQTT

The nRF9151 modem supports MQTT natively via Nordic’s AT#XMQTT* command set. The Modem class wraps these commands with validation and error handling.

Nordic reference: MQTT client AT commands — nCS 3.0.0

The modem must be on with network access enabled:

from modem import Modem
m = Modem()
m.CFUN(1) # full functionality — wait for network registration
m.mqtt_cfg(
client_id="my-device-01",
keep_alive=60, # seconds, default 60
clean_session=1, # 0 = persistent session, 1 = clean
)
connected = m.mqtt_conn(
op=1, # 1 = connect IPv4, 2 = connect IPv6, 0 = disconnect
username="user",
password="secret",
url="broker.example.com",
port=1883,
# sec_tag=1, # optional TLS security tag
)
print(connected) # True on success
print(m.is_mqtt_conn()) # True if currently connected
m.mqtt_publish(
topic="sensors/temp",
msg="23.4",
qos=0, # 0, 1, or 2
retain=0, # 0 or 1
)
m.mqtt_subscribe(topic="commands/#", qos=1)

mqtt_unsubscribe is not yet implemented in the Modem class. Use send_cmd directly:

m.send_cmd('AT#XMQTTUNSUB="sensors/#"', expected="#XMQTTEVT: 8,0", timeout_ms=5000)
m.mqtt_conn(op=0, username="", password="", url="", port=1)
print(m.get_mqtt_cfg()) # returns the raw #XMQTTCFG response line

See the Modem API reference for the full #XMQTTEVT event table and parameter details.