MQTT simple example code (api based)

Hi Team,

We are using nRF9160 MQTT sample code 'mqtt_simple'. 

1. What is the maximum payload we can send in one single PUBLISH?

2. I want to send 4KB of data in single PUBLISH, is this possible? if not, what is the way to send 4KB in single PUBLISH?

3. Is the nRF9160 supports secure boot?

4. Is the nRF9160 has boot ROM? If yes, is this boot ROM available for us to add some piece of code in the boot ROM?

Regards,

Sravan Rikka

Parents
  • Hi,

    1. What is the maximum payload we can send in one single PUBLISH?

    2. I want to send 4KB of data in single PUBLISH, is this possible? if not, what is the way to send 4KB in single PUBLISH?

    There is a data communication limit in modem due to max size of TLS buffer of 2 KB. You can read about the limit  in the documentation. Furthermore, you can look at MQTT sample overview and this discussion. For sending larger messages, you can split your big MQTT message into smaller ones.

    3. Is the nRF9160 supports secure boot?

    Yes, you can look at features and nRF91 security features.

    4. Is the nRF9160 has boot ROM? If yes, is this boot ROM available for us to add some piece of code in the boot ROM?

    I will check this internally.

    Best regards,
    Deja

  • Hi Dejans,

    For my 1 and 2 queries:

    I have looked at your suggested links and it is talking about TLS but I am not using TLS, I am using nonsecure MQTT.

    For non-secure MQTT also 2KB is the limit in single publish?

    In MQTT simple code I can see tx_buffer and rx_buffer size as 128 bytes, can I make it to 2048 bytes? This is the correct place to increase the MQTT publish buffer size or is there any other place need to change?

    #define CONFIG_MQTT_MESSAGE_BUFFER_SIZE 128
    #define CONFIG_MQTT_PAYLOAD_BUFFER_SIZE 128

    /* Buffers for MQTT client. */
    static uint8_t rx_buffer[CONFIG_MQTT_MESSAGE_BUFFER_SIZE];
    static uint8_t tx_buffer[CONFIG_MQTT_MESSAGE_BUFFER_SIZE];

    Regards,

    Sravan Rikka

  • <\0>*** Booting Zephyr OS build v2.6.99-ncs1  ***<\r><\n>
    Flash regions<9><9>Domain<9><9>Permissions<\r><\n>
    00 01 0x00000 0x10000 <9>Secure<9><9>rwxl<\r><\n>
    02 31 0x10000 0x100000 <9>Non-Secure<9>rwxl<\r><\n>
    <\r><\n>
    Non-secure callable region 0 placed in flash region 1 with size 32.<\r><\n>
    <\r><\n>
    SRAM region<9><9>Domain<9><9>Permissions<\r><\n>
    00 07 0x00000 0x10000 <9>Secure<9><9>rwxl<\r><\n>
    08 31 0x10000 0x40000 <9>Non-Secure<9>rwxl<\r><\n>
    <\r><\n>
    Peripheral<9><9>Domain<9><9>Status<\r><\n>
    00 NRF_P0               Non-Secure<9>OK<\r><\n>
    01 NRF_CLOCK            Non-Secure<9>OK<\r><\n>
    02 NRF_RTC0             Non-Secure<9>OK<\r><\n>
    03 NRF_RTC1             Non-Secure<9>OK<\r><\n>
    04 NRF_NVMC             Non-Secure<9>OK<\r><\n>
    05 NRF_UARTE1           Non-Secure<9>OK<\r><\n>
    06 NRF_UARTE2           Secure<9><9>SKIP<\r><\n>
    07 NRF_TWIM2            Non-Secure<9>OK<\r><\n>
    08 NRF_SPIM3            Non-Secure<9>OK<\r><\n>
    09 NRF_TIMER0           Non-Secure<9>OK<\r><\n>
    10 NRF_TIMER1           Non-Secure<9>OK<\r><\n>
    11 NRF_TIMER2           Non-Secure<9>OK<\r><\n>
    12 NRF_SAADC            Non-Secure<9>OK<\r><\n>
    13 NRF_PWM0             Non-Secure<9>OK<\r><\n>
    14 NRF_PWM1             Non-Secure<9>OK<\r><\n>
    15 NRF_PWM2             Non-Secure<9>OK<\r><\n>
    16 NRF_PWM3             Non-Secure<9>OK<\r><\n>
    17 NRF_WDT              Non-Secure<9>OK<\r><\n>
    18 NRF_IPC              Non-Secure<9>OK<\r><\n>
    19 NRF_VMC              Non-Secure<9>OK<\r><\n>
    20 NRF_FPU              Non-Secure<9>OK<\r><\n>
    21 NRF_EGU1             Non-Secure<9>OK<\r><\n>
    22 NRF_EGU2             Non-Secure<9>OK<\r><\n>
    23 NRF_DPPIC            Non-Secure<9>OK<\r><\n>
    24 NRF_REGULATORS       Non-Secure<9>OK<\r><\n>
    25 NRF_PDM              Non-Secure<9>OK<\r><\n>
    26 NRF_I2S              Non-Secure<9>OK<\r><\n>
    27 NRF_GPIOTE1          Non-Secure<9>OK<\r><\n>
    <\r><\n>
    SPM: NS image at 0x10000<\r><\n>
    SPM: NS MSP at 0x2001df40<\r><\n>
    SPM: NS reset vector at 0x1528d<\r><\n>
    SPM: prepare to jump to Non-Secure image.<\r><\n>
    *** Booting Zephyr OS build v2.6.99-ncs1  ***<\r><\n>
    uart init<\r><\n>
    UART RX buffer request<\r><\n>
    UART2 initialized with baud rate 115200<\r><\n>
    UART RX ready<\r><\n>
    UART RX buffer released<\r><\n>
    UART RX buffer request<\r><\n>
    UART RX ready<\r><\n>
    DATA_CMD<\r><\n>
    UART RX buffer released<\r><\n>
    UART RX buffer request<\r><\n>
    UART RX ready<\r><\n>
    ALARM_CMD<\r><\n>
    UART RX buffer released<\r><\n>
    UART RX buffer request<\r><\n>
    RSL10 data received<\r><\n>
    state machine start<\r><\n>
    STATE_NETWORK_REGISTRATION<\r><\n>
    Success AT_CFUN_READ_CMD before write: +CFUN: 0<\r><\r><\n>
    <\r><\r><\n>
    Success AT_CFUN_OFF_CMD<\r><\r><\n>
    Success AT_CFUN_READ_CMD after write: +CFUN: 4<\r><\r><\n>
    <\r><\r><\n>
    Success AT_CGDCONT_READ_CMD before write: <\r><\r><\n>
    successfully set APN using AT_CGDCONT_WRITE_CMD<\r><\r><\n>
    Success AT_CGDCONT_READ_CMD after write: +CGDCONT: 0,"IPV6","AIRTELMETERV6","",0,0<\r><\r><\n>
    <\r><\r><\n>
    HVPP ----- connecting to network...<\r><\n>
    [00:02:14.131,866] <27>[0m<inf> mqtt_simple: Disabling PSM and eDRX<27>[0m<\r><\n>
    [00:02:14.138,946] <27>[0m<inf> mqtt_simple: LTE Link Connecting...<27>[0m<\r><\n>
    +CEREG: 2,"2B67","0DCC6751",9<\r><\n>
    +CSCON: 1<\r><\n>
    +CEREG: 1,"2B67","0DCC6751",9,,,"11100000","11100000"<\r><\n>
    HVPP ----- connected to network...<\r><\n>
    [00:02:20.484,954] <27>[0m<inf> mqtt_simple: LTE Link Connected!<27>[0m<\r><\n>
    state machine start<\r><\n>
    STATE_INIT<\r><\n>
    username size1=5, password size1=36, username size2=5, password size1=36<\r><\n>
    ai_family = 2<\r><\n>
    IP_type = 1<\r><\n>
    MQTT_hostname = mqtt.tago.io<\r><\n>
    MQTT_username = token<\r><\n>
    MQTT_password = a8c46146-622d-46e7-8b3b-a244bca88648<\r><\n>
    MQTT_portnumber = 0<\r><\n>
    MQTT_topic = devices/HON_HVPP_5G_0001/events<\r><\n>
    broker_portnumber = 1883<\r><\n>
    Client initialized successfully.<\r><\n>
    state machine start<\r><\n>
    STATE_CONNECT<\r><\n>
    Connecting to MQTT broker...<\r><\n>
    ----- connected to MQTT broker --- client connect ...<\r><\n>
    ----- connected to MQTT broker...<\r><\n>
    ----- fds_init pass .....<\r><\n>
    state machine start<\r><\n>
    STATE_WAIT<\r><\n>
    Waiting...<\r><\n>
    mqtt_evt_handler executedstate machine start<\r><\n>
    STATE_SEND_PERIODIC_DATA<\r><\n>
    Sending data to MQTT broker...<\r><\n>
    data_publish size = len1:553, len2:536974336data_publish:{"deviceId":"HVPP100A0000C7","deviceType":"HVPP","eventTime":946684933,"eventType":"SensorData","eventData":{"batteryLevel":{"value":90,"uom":"%"},"ambientTemperature":{"value":0.000000,"uom":"DegC"},"humidity":{"value":0.000000,"uom":"%RH"},"ambientPressure":{"value":335.000000,"uom":"hPa"},"processTemperature":{"value":0.000000,"uom":"DegC"},"processPressureAbsolute":{"value":0.000000,"uom":"Bar"},"processPressureGauge":{"value":0.000000,"uom":"Bar"},"processLevel":{"value":508.979156,"uom":"cm"},"processVolume":{"value":1017.958313,"uom":"L"}}}[00:02:31.597,534] <27>[0m<inf> mqtt_simple: IPv6 Address found 2600:1f10:4526:8003:ea3c:5d6c:ccbd:3940<27>[0m<\r><\n>
    [00:02:31.597,595] <27>[0m<dbg> mqtt_simple.client_id_get: client_id = hvpp_hon1<27>[0m<\r><\n>
    [00:02:32.549,499] <27>[0m<inf> mqtt_simple: MQTT client connected<27>[0m<\r><\n>
    [00:02:32.549,530] <27>[0m<inf> mqtt_simple: Subscribing to: my/subscribe/topic len 18<27>[0m<\r><\n>
    [00:02:32.559,387] <27>[0m<inf> mqtt_simple: Publishing: {"deviceId":"HVPP100A0000C7","deviceType":"HV~<27>[0m<\r><\n>
    [00:02:32.559,417] <27>[0m<inf> mqtt_simple: to topic: devices/HON_HVPP_5G_0001/events len: 31<27>[0m<\r><\n>
    [00:02:32.659,210] <27>[1;31m<err> log: argument 0 in source mqtt_simple log message "to topic: %s len: %u" missinglog_strdup().<27>[0m<\r><\n>
    state machine start<\r><\n>
    STATE_SEND_ALARM_DATA<\r><\n>
    Sending data to MQTT broker...<\r><\n>
    data_publish size = len1:536970239, len2:687data_publish:{"deviceId":"HVPP100A0000C7","deviceType":"HVPP","eventTime":946684934,"eventType":"AlarmData","eventData":{"ambientTemperatureAlarm": {"low":0,"high":1,"value":25.600000,"uom":"DegC"},"humidityAlarm":{"low":0,"high":0,"value":0.000000,"uom":"%RH"},"ambientPressureAlarm":{"low":1,"high":0,"value":906.500000,"uom":"hPa"},"processTemperatureAlarm":{"low":0,"high":0,"value":0.000000,"uom":"DegC"},"processPressureAbsoluteAlarm":{"low":0,"high":0,"value":0.000000,"uom":"Bar"},"processPressureGaugeAlarm":{"low":0,"high":0,"value":0.000000,"uom":"Bar"},"processLevelAlarm":{"low":0,"high":0,"value":0.000000,"uom":"cm"},"processVolumeAlarm":{"low":0,"high":0,"value":0.000000,"uom":"L"}}}[00:02:42.625,885] <27>[0m<inf> mqtt_simple: Publishing: {"deviceId":"HVPP100A0000C7","deviceType":"HV~<27>[0m<\r><\n>
    [00:02:42.625,915] <27>[0m<inf> mqtt_simple: to topic: devices/HON_HVPP_5G_0001/events len: 31<27>[0m<\r><\n>
    [00:02:43.635,711] <27>[1;31m<err> log: argument 0 in source mqtt_simple log message "to topic: %s len: %u" missinglog_strdup().<27>[0m<\r><\n>
    state machine start<\r><\n>
    STATE_DONE ---- MQTT operation completed<\r><\n>
    mqtt_evt_handler executed[00:02:52.706,115] <27>[0m<inf> mqtt_simple: MQTT client disconnected: 0<27>[0m<\r><\n>
    mqtt_disconnect:0<\r><\n>
    Success AT_CGDCONT_READ_CMD Azure Done: +CGDCONT: 0,"IPV6","airtelmeterv6","2401:4900:9829:A7A8:0000:0000:0000:0002",0,0<\r><\r><\n>
    <\r><\r><\n>
    zsock_close:0<\r><\r><\n>
    +CEREG: 0<\r><\n>
    +CSCON: 0<\r><\n>
    Success AT_CFUN_OFF_RADIO_CMD<\r><\r><\n>
    UART RX ready<\r><\n>
    UART RX buffer released<\r><\n>
    UART RX buffer request<\r><\n>
    UART RX ready<\r><\n>
    DATA_CMD<\r><\n>
    UART RX buffer released<\r><\n>
    UART RX buffer request<\r><\n>
    UART RX ready<\r><\n>
    ALARM_CMD<\r><\n>
    UART RX buffer released<\r><\n>
    UART RX buffer request<\r><\n>
    RSL10 data received<\r><\n>
    state machine start<\r><\n>
    STATE_NETWORK_REGISTRATION<\r><\n>
    Success AT_CFUN_READ_CMD before write: +CFUN: 0<\r><\r><\n>
    <\r><\r><\n>
    Success AT_CFUN_OFF_CMD<\r><\r><\n>
    Success AT_CFUN_READ_CMD after write: +CFUN: 4<\r><\r><\n>
    <\r><\r><\n>
    Success AT_CGDCONT_READ_CMD before write: <\r><\r><\n>
    successfully set APN using AT_CGDCONT_WRITE_CMD<\r><\r><\n>
    Success AT_CGDCONT_READ_CMD after write: +CGDCONT: 0,"IPV6","AIRTELMETERV6","",0,0<\r><\r><\n>
    <\r><\r><\n>
    HVPP ----- connecting to network...<\r><\n>
    [00:04:14.120,361] <27>[0m<inf> mqtt_simple: Disabling PSM and eDRX<27>[0m<\r><\n>
    [00:04:14.127,441] <27>[0m<inf> mqtt_simple: LTE Link Connecting...<27>[0m<\r><\n>
    [00:04:14.127,441] <27>[0m<inf> mqtt_simple: Failed to establish LTE connection: -120<27>[0m<\r><\n>
    Retrying Network Regstartion..... (2 retries left)<\r><\n>
    HVPP ----- connecting to network...<\r><\n>
    [00:04:19.135,742] <27>[0m<inf> mqtt_simple: Disabling PSM and eDRX<27>[0m<\r><\n>
    [00:04:19.145,080] <27>[0m<inf> mqtt_simple: LTE Link Connecting...<27>[0m<\r><\n>
    [00:04:19.145,080] <27>[0m<inf> mqtt_simple: Failed to establish LTE connection: -120<27>[0m<\r><\n>
    Retrying Network Regstartion..... (1 retries left)<\r><\n>
    HVPP ----- connecting to network...<\r><\n>
    [00:04:24.153,411] <27>[0m<inf> mqtt_simple: Disabling PSM and eDRX<27>[0m<\r><\n>
    [00:04:24.162,719] <27>[0m<inf> mqtt_simple: LTE Link Connecting...<27>[0m<\r><\n>
    [00:04:24.162,719] <27>[0m<inf> mqtt_simple: Failed to establish LTE connection: -120<27>[0m<\r><\n>
    Retrying Network Regstartion..... (0 retries left)<\r><\n>
    state machine start<\r><\n>
    STATE_ERROR<\r><\n>
    Error occurred in Azure IoT Hub operation<\r><\n>
    state machine start<\r><\n>
    STATE_DONE ---- MQTT operation completed<\r><\n>
    mqtt_disconnect:-128<\r><\n>
    Success AT_CGDCONT_READ_CMD Azure Done: +CGDCONT: 0,"IPV6","AIRTELMETERV6","",0,0<\r><\r><\n>
    <\r><\r><\n>
    zsock_close:0<\r><\r><\n>
    Success AT_CFUN_OFF_RADIO_CMD<\r><\r><\n>
    

    Hi Dejan,

    Please check the attached log.

    Regards,

    Sravan Rikka

  • Hi,

    There is a limitation on a number of connection requests. It could be that you are exceeding number of connection attempts per hour. You can get more information in this discussion.

    Best regards,
    Dejan

  • Hi Dejan,

    I am not making two many calls in an hour, I am making call for every 30minutes, 1st call is successful but second call is not connecting to network. Please suggest how can I resolve this issue.

    Regards,

    Sravan Rikka

  • Hi Sravan,

    Do you use lte_lc library API in your application?

    Can you provide modem trace for further analysis?

    EDIT:
    You must use lte_lc_deinit to turn off the link if you also use lte_lc  to set CFUN=1.

    Best regards,
    Dejan

  • Hi Dejan,

    Yes, I am using "lte_lc library API".

    Can you provide modem trace for further analysis? How to get this, please provide procedure.

    Regards,

    Sravan Rikka

Reply Children
  • Hi,

    Have you seen the edit in my previous reply? Can you double-check if the link is turned off in described way?

    sravan.rikka said:
    Can you provide modem trace for further analysis? How to get this, please provide procedure.

    Please have a look at following resources
    modem trace
    capturing modem trace
    snippets for nrf91 series
    modem trace module
    cellular monitor
    capturing modem trace
    enable tracing in the application
    capturing modem trace using nrfutil

    Best regards,
    Dejan

  • Hi Dejan,

    1. Assume we have chosen QoS as 1 and our device is publishing data but the broker is down then as per QoS1 theory, when there is no ACK from broker client(device) will retry until get the ACK, how many time nRF9160 modem will retry? Is this retry count is configurable? This retry is handled by stack/library files itself right?(I mean nothing to do in application level)

    2. Want to keep nRF9160 in sleep mode and wakeup based on wakeup interrupt, is there any api function to keep in sleep mode? can you share sleep mode example code path in SDK 3.1.0.

    3. We have subscribe for a topic and got the MQTT_EVT_PUBLISH event when received subscribe data, is this the correct event to check subscribe payload? I am searching for SUBSCIBE event but didn't find it.

    As a code flow first we have to publish payload and then wait for the subscribe data, is this correct? or wait for subscribe data and then publish payload?

    Regards,

    Sravan Rikka

  • Hi,

    sravan.rikka said:
    1. Assume we have chosen QoS as 1 and our device is publishing data but the broker is down then as per QoS1 theory, when there is no ACK from broker client(device) will retry until get the ACK, how many time nRF9160 modem will retry? Is this retry count is configurable? This retry is handled by stack/library files itself right?(I mean nothing to do in application level)

    The modem is doing retransmissions on the TCP (not MQTT) level. I will check internally how many retries there could be and get back to you.

    sravan.rikka said:
    2. Want to keep nRF9160 in sleep mode and wakeup based on wakeup interrupt, is there any api function to keep in sleep mode? can you share sleep mode example code path in SDK 3.1.0.

    Modem can be put in low power state by entering PSM or by being turned off. There is lte_lc API for doing this.

    sravan.rikka said:
    3. We have subscribe for a topic and got the MQTT_EVT_PUBLISH event when received subscribe data, is this the correct event to check subscribe payload? I am searching for SUBSCIBE event but didn't find it.

    MQTT_EVT_PUBLISH is publishing event which is received when message is published on a topic client has subscribed to. Payload can be read using mqtt_read_publish_payload().

    sravan.rikka said:
    As a code flow first we have to publish payload and then wait for the subscribe data, is this correct? or wait for subscribe data and then publish payload?

    If device wants to read published payload, it needs to be subscribed beforehand.

    Best regards,
    Dejan

  • Hi,

    dejans said:
    The modem is doing retransmissions on the TCP (not MQTT) level. I will check internally how many retries there could be and get back to you.

    TCP has 10 retransmissions, and this is not configurable in the application.

    Best regards,
    Dejan

Related