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

  • Hi Dejan,

    I am using tago.io broker.

    I am saying that snprintf itself not copying the float value into buffer when I use %f.

    Regards,

    Sravan Rikka

  • Hi Dejan,

    Please respond to below queries I am waiting for your responses. Thank you.

    1. In our application we will store 5000 records and each record of size 1429, hence total bytes=5000 *      1429=7145000. Hence, we are planning to transfer 7.145MB in single MQTT publish. It is possible right?

    2. When I am copying float value into buf using sprintf I am not seeing any value in the buffer, why? whereas %d is working but %f is not working. Am I missing anything, please advice.

    float ambientTemperature = 24.5;
    len = snprintf(buf, sizeof(buf), "ambientTemperature=%f", ambientTemperature);
    printk(buf);

    Regards,

    Sravan Rikka

  • Hi Sravan,

    sravan.rikka said:
    1. In our application we will store 5000 records and each record of size 1429, hence total bytes=5000 *      1429=7145000. Hence, we are planning to transfer 7.145MB in single MQTT publish. It is possible right?

    Have you checked if there are any limitations on the broker side?

    sravan.rikka said:

    2. When I am copying float value into buf using sprintf I am not seeing any value in the buffer, why? whereas %d is working but %f is not working. Am I missing anything, please advice.

    float ambientTemperature = 24.5;
    len = snprintf(buf, sizeof(buf), "ambientTemperature=%f", ambientTemperature);
    printk(buf);

    I have used the following sample which correctly displays ambientTemperature value from the buffer buf.

    #include <stdio.h>
    
    char buf[200];
    int len;
    
    int main(void)
    {
        float ambientTemperature = 24.5;
        len = snprintf(buf, sizeof(buf), "ambientTemperature=%f", ambientTemperature);
        printf("buffer value is %s", buf);
    }

    Best regards,
    Dejan

  • Hi Dejan,

    1. Not yet, I will check broker side limitations.

    2. For me it is not working, I am using 'mqtt simple' example code. Please suggest me how to resolve this. Below are the header files in the main file. Do we need to add any other header file?

    #include <zephyr.h>
    #include <stdio.h>
    #include <drivers/uart.h>
    #include <string.h>
    #include <random/rand32.h>
    #include <net/mqtt.h>
    #include <net/socket.h>
    #include <modem/at_cmd.h>
    #include <modem/lte_lc.h>
    #include <logging/log.h>
    #if defined(CONFIG_MODEM_KEY_MGMT)
    #include <modem/modem_key_mgmt.h>
    #endif
    #if defined(CONFIG_LWM2M_CARRIER)
    #include <lwm2m_carrier.h>
    #endif
    #include <dk_buttons_and_leds.h>

    #include "certificates.h"

    Regards,

    Sravan Rikka

  • Hi Sravan,

    You can try to add this header file

    #include <zephyr/sys/printk.h>

    and these Kconfig options 
    CONFIG_PICOLIBC=y
    CONFIG_PICOLIBC_IO_FLOAT=y
    

    Best regards,
    Dejan

Reply Children
  • Hi Dejan,

    I have added #include <zephyr/sys/printk.h> but got compilation error as "zephyr/sys/printk.h: No such file or directory". then searched for printk.h and found that it was added like #include <sys/printk.h> and added same and no compilation error but still not seeing float values in snprintf buffer.

    Tried adding CONFIG_PICOLIBC=y and CONFIG_PICOLIBC_IO_FLOAT=y in .prj but code is not loading after adding these configuration, after removing able to load code. So, unable to add these to config, is there any other way to add it? please guide and help to resolve this issue.

    Regards,

    Sravan Rikka

  • Hi Sravan,

    I have tested in NCS v3.0.2, not in NCS v1.7.0. I am sorry that I forgot to mention this. In your case, it should be correct to use #include <sys/printk.h> instead of 
    #include <zephyr/sys/printk.h>.
    You could try to use Newlib. You can look at types of Newlib and its formatted output.

    Best regards,
    Dejan

  • Hi Dejan,

    Thank you for the support to resolving float value issue.

    I have added CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y in the .prj as you suggested, now I can see the float values in the buffer.

    Queries:

    1. I am connecting to the network only at power ON and making continuous calls for every 30min to the MQTT broker successfully. Now I am implementing that connect to the network and publish the data then disconnect from the network for every call. Hence, I have implemented socket close as below and got response as 'ret=0' for "ret = zsock_close(socketfd);"  but when I connect to the network again for the second call getting error as "mqtt_simple: Failed to establish LTE connection: -120". (#define EALREADY 120 /* Socket already connected */).

    Can you suggest, how to resolve this issue.

    function()

    {

    err = mqtt_disconnect(&client);
    k_sleep(K_MSEC(2000u));
    printk("mqtt_disconnect:%d\n", err);
    current_state = STATE_EXIT;

    k_sleep(K_MSEC(200u));

    close_socket();

    }

    void close_socket(void)
    {
    int err;
    int socketfd = 0;

    int ret;

    socketfd = zsock_socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
    ret = zsock_close(socketfd);

    printk("zsock_close:%d\r\n", ret);

    err = at_cmd_write(AT_CFUN_OFF_RADIO_CMD, NULL, 0, NULL);
    k_sleep(K_MSEC(20u));
    if(err < 0)
    printk("Failed AT_CFUN_OFF_RADIO_CMD\r\n");
    else
    printk("Success AT_CFUN_OFF_RADIO_CMD\r\n");
    }

    Regards,

    Sravan Rikka

  • Hi,

    Can you share complete log that shows the error ""mqtt_simple: Failed to establish LTE connection: -120". (#define EALREADY 120 /* Socket already connected */)."?

    Best regards,
    Dejan

  • <\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

Related