Need help connecting to MQTT using nRF9160

I can't figure out what I'm missing. I'm not able to send messages using MQTT.

Here's the link to my code: https://github.com/pranav-ether/test/tree/main/mqtt_pub

This are the logs:

[00:02:58.031,768] <inf> LTE: Network registration status: Connected - home network
[00:02:58.031,890] <inf> LTE: Connected to LTE network
[00:02:58.031,921] <inf> LTE: ====== Cell Network Info ======
[00:02:58.033,477] <inf> LTE: Signal strength: 42
[00:02:58.034,210] <inf> LTE: Current LTE band: 8
[00:02:58.034,912] <inf> LTE: Supported LTE bands: (1,2,3,4,5,8,12,13,17,19,20,25,26,28,66)
[00:02:58.035,705] <inf> LTE: Tracking area code: 8CA1
[00:02:58.036,468] <inf> LTE: Current mode: 0
[00:02:58.037,200] <inf> LTE: Current operator name: 65501
[00:02:58.038,146] <inf> LTE: Cell ID of the device: 0E446D21
[00:02:58.039,337] <inf> LTE: IP address of the device: 100.67.202.179
[00:02:58.039,886] <inf> LTE: Modem firmware version: mfw_nrf9160_1.3.5
[00:02:58.040,618] <inf> LTE: LTE-M support mode: 0
[00:02:58.041,259] <inf> LTE: NB-IoT support mode: 1
[00:02:58.041,900] <inf> LTE: GPS support mode: 0
[00:02:58.042,633] <inf> LTE: Mobile network time and date: 24/08/13,06:33:37+08
[00:02:58.042,633] <inf> LTE: ===============================
[00:02:58.043,121] <inf> MQTT: Connection to broker using mqtt_init
[00:04:23.972,839] <err> mqtt_helper: Cloud MQTT keepalive ping failed: -128

Parents
  • Did you make original NCS MQTT sample works MQTT (nordicsemi.com)? Which NCS version are you based on?

  • The mqtt sample did not work for me. I'm using v2.6.1
    Here are the logs for the mqtt sample:

    [00:00:00.295,349] <inf> network: Bringing network interface up and connecting to the network
    [00:00:00.528,198] <inf> nrf_modem_lib_trace: Trace thread ready
    [00:00:00.535,827] <inf> nrf_modem_lib_trace: Trace level override: 2
    +CEREG: 2,"8CA1","0E241F17",9
    +CSCON: 1
    +CSCON: 0
    +CSCON: 1
    +CGEV: ME PDN ACT 0,0
    +CNEC_ESM: 50,0
    +CEREG: 1,"8CA1","0E241F17",9,,,"11100000","00000110"
    [00:01:47.830,474] <inf> network: Network connectivity established
    [00:03:30.036,132] <err> mqtt_helper: Cloud MQTT keepalive ping failed: -128
    [00:03:30.036,376] <err> mqtt_helper: Library is in the wrong state (MQTT_STATE_CONNECTING), MQTT_STATE_DISCONNECTED required
    [00:03:30.036,407] <err> transport: Failed connecting to MQTT, error code: -95
    [00:04:30.037,017] <err> mqtt_helper: Library is in the wrong state (MQTT_STATE_CONNECTING), MQTT_STATE_DISCONNECTED required
    [00:04:30.037,048] <err> transport: Failed connecting to MQTT, error code: -95
    +CSCON: 0
    [00:05:30.046,081] <err> mqtt_helper: Library is in the wrong state (MQTT_STATE_CONNECTING), MQTT_STATE_DISCONNECTED required
    [00:05:30.046,112] <err> transport: Failed connecting to MQTT, error code: -95
    +CSCON: 1
    [00:06:30.046,875] <err> mqtt_helper: Library is in the wrong state (MQTT_STATE_CONNECTING), MQTT_STATE_DISCONNECTED required
    [00:06:30.046,905] <err> transport: Failed connecting to MQTT, error code: -95
    +CSCON: 0
    [00:07:30.055,938] <err> mqtt_helper: Library is in the wrong state (MQTT_STATE_CONNECTING), MQTT_STATE_DISCONNECTED required
    [00:07:30.055,969] <err> transport: Failed connecting to MQTT, error code: -95
    [00:08:30.065,032] <err> mqtt_helper: Library is in the wrong state (MQTT_STATE_CONNECTING), MQTT_STATE_DISCONNECTED required
    [00:08:30.065,063] <err> transport: Failed connecting to MQTT, error code: -95
    *** Booting nRF Connect SDK 3758bcbfa5cd ***
    [00:00:00.300,537] <inf> network: Bringing network interface up and connecting to the network
    [00:00:00.533,386] <inf> nrf_modem_lib_trace: Trace thread ready
    [00:00:00.541,015] <inf> nrf_modem_lib_trace: Trace level override: 2
    +CEREG: 2,"8CA1","0E241F17",9
    +CSCON: 1
    +CSCON: 0
    +CSCON: 1
    +CGEV: ME PDN ACT 0,0
    +CNEC_ESM: 50,0
    +CEREG: 1,"8CA1","0E241F17",9,,,"11100000","00000110"
    [00:01:49.906,158] <inf> network: Network connectivity established
    [00:03:32.047,851] <err> mqtt_helper: Cloud MQTT keepalive ping failed: -128
    [00:03:32.048,095] <err> mqtt_helper: Library is in the wrong state (MQTT_STATE_CONNECTING), MQTT_STATE_DISCONNECTED required
    [00:03:32.048,095] <err> transport: Failed connecting to MQTT, error code: -95
    [00:04:32.048,706] <err> mqtt_helper: Library is in the wrong state (MQTT_STATE_CONNECTING), MQTT_STATE_DISCONNECTED required
    [00:04:32.048,736] <err> transport: Failed connecting to MQTT, error code: -95
    +CSCON: 0
    [00:05:32.057,800] <err> mqtt_helper: Library is in the wrong state (MQTT_STATE_CONNECTING), MQTT_STATE_DISCONNECTED required
    [00:05:32.057,830] <err> transport: Failed connecting to MQTT, error code: -95

  • Yes, only JIO network is found by your device according to AT%COPS=? feedback.

    MCC MNC PLMN Region Country ISO Operator Brand TADIG Bands
    405 861 405861 Asia and the Middle East India IN Karnataka Jio INDJ9 LTE 850 / LTE 1800 / TD-LTE 2300
  • I was able to get Jio SIMs and I'm able to connect to the network. But my MQTT program still doesn't work. It works only for brokers without username & password. The broker I want to connect to has username, password & port (My program is in the body of this post).

    Also the network is getting connected on the nRF9160DK but not my custom board. What could be the reason?

  • 1) For the original sample  docs.nordicsemi.com/.../description.html, if you want to use username & password to connect with MQTT broker, you need to add username & password parameters here:

    https://github.com/nrfconnect/sdk-nrf/blob/2bae3437305bc8f5eb29260ca545fb8033b2b80b/samples/net/mqtt/src/modules/transport/transport.c#L187

    struct mqtt_helper_conn_params {
    /* The hostname must be null-terminated. */
    struct mqtt_helper_buf hostname;
    struct mqtt_helper_buf device_id;
    struct mqtt_helper_buf user_name;
    struct mqtt_helper_buf password;
    };

    2) For your custom board connection issue, can you attach the log and modem trace?

  • How do I get modem trace through RTT? My custom board does not have free UART ports for logging.

  • I was able to get MQTT working with Jio in India. But the same firmware isn't working in South Africa with Vodacom NB-IoT. Here's the log and trace. I want to get the firmware working with the DK. I have opened another issue for my custom board connection problems.

    trace-2024-10-04T06-22-12.925Z.mtrace

    *** Booting nRF Connect SDK 3758bcbfa5cd ***
    [00:00:00.496,185] <inf> nrf_modem_lib_trace: Trace thread ready
    [00:00:00.503,814] <inf> nrf_modem_lib_trace: Trace level override: 2
    [00:00:00.508,941] <inf> LTE: Connecting to LTE network
    [00:00:00.511,077] <inf> LTE: lte_lc_system_mode_set: 0
    [00:00:00.537,811] <inf> LTE: lte_lc_connect_async: 0
    [00:01:54.258,575] <inf> LTE: Network registration status: Connected - home network
    [00:01:54.258,697] <inf> LTE: Connected to LTE network
    [00:01:54.258,728] <inf> LTE: ====== Cell Network Info ======
    [00:01:54.260,284] <inf> LTE: Signal strength: 53
    [00:01:54.261,047] <inf> LTE: Current LTE band: 8
    [00:01:54.261,749] <inf> LTE: Supported LTE bands: (1,2,3,4,5,8,12,13,17,19,20,25,26,28,66)
    [00:01:54.262,542] <inf> LTE: Tracking area code: 8CA1
    [00:01:54.263,214] <inf> LTE: Current mode: 0
    [00:01:54.263,977] <inf> LTE: Current operator name: 65501
    [00:01:54.264,923] <inf> LTE: Cell ID of the device: 0E241F17
    [00:01:54.266,082] <inf> LTE: IP address of the device: 100.xx.xx.xxx
    [00:01:54.266,632] <inf> LTE: Modem firmware version: mfw_nrf9160_1.3.5
    [00:01:54.267,364] <inf> LTE: LTE-M support mode: 0
    [00:01:54.268,005] <inf> LTE: NB-IoT support mode: 1
    [00:01:54.268,676] <inf> LTE: GPS support mode: 0
    [00:01:54.269,256] <inf> LTE: Mobile network time and date:
    [00:01:54.269,256] <inf> LTE: ===============================
    [00:01:54.269,714] <inf> MQTT: Connection to broker using mqtt_init
    [00:02:06.240,570] <inf> MQTT: Successfully connected to MQTT broker
    [00:03:06.240,753] <err> mqtt_helper: Cloud MQTT keepalive ping failed: -128

Reply
  • I was able to get MQTT working with Jio in India. But the same firmware isn't working in South Africa with Vodacom NB-IoT. Here's the log and trace. I want to get the firmware working with the DK. I have opened another issue for my custom board connection problems.

    trace-2024-10-04T06-22-12.925Z.mtrace

    *** Booting nRF Connect SDK 3758bcbfa5cd ***
    [00:00:00.496,185] <inf> nrf_modem_lib_trace: Trace thread ready
    [00:00:00.503,814] <inf> nrf_modem_lib_trace: Trace level override: 2
    [00:00:00.508,941] <inf> LTE: Connecting to LTE network
    [00:00:00.511,077] <inf> LTE: lte_lc_system_mode_set: 0
    [00:00:00.537,811] <inf> LTE: lte_lc_connect_async: 0
    [00:01:54.258,575] <inf> LTE: Network registration status: Connected - home network
    [00:01:54.258,697] <inf> LTE: Connected to LTE network
    [00:01:54.258,728] <inf> LTE: ====== Cell Network Info ======
    [00:01:54.260,284] <inf> LTE: Signal strength: 53
    [00:01:54.261,047] <inf> LTE: Current LTE band: 8
    [00:01:54.261,749] <inf> LTE: Supported LTE bands: (1,2,3,4,5,8,12,13,17,19,20,25,26,28,66)
    [00:01:54.262,542] <inf> LTE: Tracking area code: 8CA1
    [00:01:54.263,214] <inf> LTE: Current mode: 0
    [00:01:54.263,977] <inf> LTE: Current operator name: 65501
    [00:01:54.264,923] <inf> LTE: Cell ID of the device: 0E241F17
    [00:01:54.266,082] <inf> LTE: IP address of the device: 100.xx.xx.xxx
    [00:01:54.266,632] <inf> LTE: Modem firmware version: mfw_nrf9160_1.3.5
    [00:01:54.267,364] <inf> LTE: LTE-M support mode: 0
    [00:01:54.268,005] <inf> LTE: NB-IoT support mode: 1
    [00:01:54.268,676] <inf> LTE: GPS support mode: 0
    [00:01:54.269,256] <inf> LTE: Mobile network time and date:
    [00:01:54.269,256] <inf> LTE: ===============================
    [00:01:54.269,714] <inf> MQTT: Connection to broker using mqtt_init
    [00:02:06.240,570] <inf> MQTT: Successfully connected to MQTT broker
    [00:03:06.240,753] <err> mqtt_helper: Cloud MQTT keepalive ping failed: -128

Children
No Data
Related