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
Prerequisites
Section titled “Prerequisites”The modem must be on with network access enabled:
from modem import Modem
m = Modem()m.CFUN(1) # full functionality — wait for network registrationConfigure the MQTT client
Section titled “Configure the MQTT client”m.mqtt_cfg( client_id="my-device-01", keep_alive=60, # seconds, default 60 clean_session=1, # 0 = persistent session, 1 = clean)Connect to a broker
Section titled “Connect to a broker”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 successCheck connection status
Section titled “Check connection status”print(m.is_mqtt_conn()) # True if currently connectedPublish a message
Section titled “Publish a message”m.mqtt_publish( topic="sensors/temp", msg="23.4", qos=0, # 0, 1, or 2 retain=0, # 0 or 1)Subscribe to a topic
Section titled “Subscribe to a topic”m.mqtt_subscribe(topic="commands/#", qos=1)Unsubscribe
Section titled “Unsubscribe”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)Disconnect
Section titled “Disconnect”m.mqtt_conn(op=0, username="", password="", url="", port=1)Read current MQTT configuration
Section titled “Read current MQTT configuration”print(m.get_mqtt_cfg()) # returns the raw #XMQTTCFG response lineSee the Modem API reference for the full #XMQTTEVT event table and parameter details.