This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Sending MQTT messages using the data_publish function from the mqtt_simple sample

I am having some trouble with sending messages using MQTT on nRF9160DK. What I ultimately want to to is read UART and send whatever I receive to an MQTT broker. When running the mqtt_simple sample it is working as described at the fw-nrfconnect-nrf github repository, but when attempting to call data_publish() outside the mqtt_evt_handler() function it either does not send any data or the nRF9160 stops execution without any error messages. More specifically I am attepting to call data_publish() inside of the uart_cb() function from this example code.

I am using the PCA10090 0.8.2 version of the devkit with firmware version v1.0.0, NCS version v0.4.0 and v4.16 of SEGGER. I have also tried with firmware version v0.7.0.

Any and all help is greatly appreciated

Edit: Modified the original question slightly to reflect the actual problem I was having.

Parents
  • Hello, 

    First, please update to the latest version of NCS v1.0.0. This should work better with modem FW v1.0.0.

    What kind of output do you get via LTE Link Monitor in nRF Connect for Desktop?

    Kind regards,
    Øyvind

  • I updated NCS to v1.0.0 following Martin Lesund's instructions here. After building the mqtt_simple sample and downloading the merged.hex file I launched nRF Connect v3.0.0 and the LTE Link Monitor v1.0.0. When connecting the device and pressing the reset button on the devboard it gives this output. 

    I've tried rebuilding and downloading again, both with using the Programmer app v1.2.0 to clear the memory and without it. I have also tried doing this with the at_client sample.

    I have tried using the v1.0.0 tag previously, but it has given me some problems when building the solutions in SEGGER.

  • I tried setting 'zephyr/merged.hex' as the active project by right clicking it in the prject explorer. When rebuilding I get this error, which is the same error as I have been getting previously:

    C:\gnuarmemb\bin\arm-none-eabi-objcopy.exe: 'zephyr.elf': No such file
    Build failed

    The build succeeded after I tried to right click 'zephyr/merged.hex' and select "Build" before building the project again. Having to do these additional steps is what makes me think I have something set up wrong when using v1.0.0 of NCS. None of these problems were present when working with v0.4.0 and v0.4.0-rc3. Additionally, the structure of my project in the project explorer window looks different to yours.

    Edit:

    I upgraded to SEGGER v4.18 and found the correct way to set the merged hex as he active project:

    I deleted the build directory, rebuilt the project and encountered no errors this time. Downloaded the merged.hex through SEGGER and connected to the LTE Link Monitor. Everything seems to be working. When inputting the command 'AT+CGMR' it returns mfw_nrf9160_1.0.0.

  • I upgraded to SEGGER v4.18

    Sorry, I was editing my post from earlier today. It seems to be working now. 

    When inputting the command 'AT+CGMR' it returns mfw_nrf9160_1.0.0.

    Does this confirm that I have correctly installed FW v1.0.0?

  • hfj said:
    Sorry, I was editing my post from earlier today. It seems to be working now. 

    Glad to hear! :)

    hfj said:
    Does this confirm that I have correctly installed FW v1.0.0?

     Yes, this confirms that you have the latest modem FW v1.0.0.

    when attempting to call the data_publish function outside the mqtt_evt_handler function it either does not send any data or the nRF9160 stops execution without any error messages

     Does this work now?


    Kind regards,
    Øyvind

  • I have done some quick testing, putting additional calls to the data_publish function inside the mqtt_evt_handler works now (that also did not work before). Both published messages get picked up by the MQTT broker. It is a step in the right direction! Slight smile

    /* Echo back received data */
    			data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE,
    				"test_message", 12);
                data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE,
    				payload_buf, p->message.payload.len);
     

    Putting calls to this function elsewhere does not seem to work. Since I want to send the data I receive over UART over MQTT I have tried using code from the UART example found here. I've modified the code so that everything sent into COM4 (UART_1 I believe) gets echoed in COM5 (UART_0 I believe). This works. However, if I put a call to data_publish inside the uart_cb function as show below the nRF9160 stops responding and I have to do a reset.

    void uart_cb(struct device *x)
    {
    	uart_irq_update(x);
    	int data_length = 0;
    
    	if (uart_irq_rx_ready(x)) {
    		data_length = uart_fifo_read(x, uart_buf, sizeof(uart_buf));
    		uart_buf[data_length] = 0;
    	}
    
            data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE, "test", 4);// &test, 1);
    
            printk("%s", uart_buf);
    
    }

    Edit:

    Since there are no errors I find it hard to trace what goes wrong. What I did when working with NCS v0.4.0 was following the function calls and just litter the code with calls to printk(), which is also what I tried to do now. The execution seems to stop in the send function inside socket_offload.h at the third printk(): printk("Debug: send 3\n").

    static inline ssize_t send(int sock, const void *buf, size_t len,
    			   int flags)
    {
    
    printk("Debug: send 1\n");
    	__ASSERT_NO_MSG(socket_ops);
    printk("Debug: send 2\n");
    	__ASSERT_NO_MSG(socket_ops->send);
    printk("Debug: send 3\n");
    
            ssize_t stest = socket_ops->send(sock, buf, len, flags);
    
    printk("Debug: send 4\n");
    
    	return stest; // socket_ops->send(sock, buf, len, flags);
    }

    Thank you for all the help so far,

    hfj

Reply
  • I have done some quick testing, putting additional calls to the data_publish function inside the mqtt_evt_handler works now (that also did not work before). Both published messages get picked up by the MQTT broker. It is a step in the right direction! Slight smile

    /* Echo back received data */
    			data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE,
    				"test_message", 12);
                data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE,
    				payload_buf, p->message.payload.len);
     

    Putting calls to this function elsewhere does not seem to work. Since I want to send the data I receive over UART over MQTT I have tried using code from the UART example found here. I've modified the code so that everything sent into COM4 (UART_1 I believe) gets echoed in COM5 (UART_0 I believe). This works. However, if I put a call to data_publish inside the uart_cb function as show below the nRF9160 stops responding and I have to do a reset.

    void uart_cb(struct device *x)
    {
    	uart_irq_update(x);
    	int data_length = 0;
    
    	if (uart_irq_rx_ready(x)) {
    		data_length = uart_fifo_read(x, uart_buf, sizeof(uart_buf));
    		uart_buf[data_length] = 0;
    	}
    
            data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE, "test", 4);// &test, 1);
    
            printk("%s", uart_buf);
    
    }

    Edit:

    Since there are no errors I find it hard to trace what goes wrong. What I did when working with NCS v0.4.0 was following the function calls and just litter the code with calls to printk(), which is also what I tried to do now. The execution seems to stop in the send function inside socket_offload.h at the third printk(): printk("Debug: send 3\n").

    static inline ssize_t send(int sock, const void *buf, size_t len,
    			   int flags)
    {
    
    printk("Debug: send 1\n");
    	__ASSERT_NO_MSG(socket_ops);
    printk("Debug: send 2\n");
    	__ASSERT_NO_MSG(socket_ops->send);
    printk("Debug: send 3\n");
    
            ssize_t stest = socket_ops->send(sock, buf, len, flags);
    
    printk("Debug: send 4\n");
    
    	return stest; // socket_ops->send(sock, buf, len, flags);
    }

    Thank you for all the help so far,

    hfj

Children
No Data
Related