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.

  • Should not give that output unless you have not updated the modem FW. Did you follow all the steps in Martins instructions? Have you ensured that SW5 is set to nRF91?

    Please try to delete all build folders in the at_client folder. When programming the device, can you please describe the steps you have used?


    Using a command line, can you please first issue:

    nrfjprog -e

    Then, still in the command line, find the folder where you have v1.0.0 of at_client e.g. nrf\samples\nrf9160\at_client\build\zephyr

    Standing in that folder, run the following (given that you have merged.hex file):

    nrfjprog --program merged.hex -f nrf91 --sectorerase -r --verify

  • I followed Martins instructions as he wrote them in the link from my original post, using the Programmer v1.2.0 application and the firmware 'mfw_nrf9160_1.0.0.zip'. I went through the same steps again now. In the Programmer app i clicked "Update modem" after clearing the memory with "Erase all". After selecting the firmware and clicking "Write":

    The PROG/DEBUG switch is set to nRF91, I don't think I've ever touched it.

    Before programming the device I made sure all build folders were deleted, the at-client sample folder looked like in the following picture: 

    When building the project in SEGGER I go to "File" and chose "Open nRF Connect SDK Project..." and enter the following:

    After loading the project:

    When first building the project it quite often fails, providing an output (this is just the end of the build log):

    Building ‘spm/zephyr/linker_pass_final.cmd’ from solution ‘build’ in configuration ‘Common’
    1> Combining ‘spm/zephyr/linker_pass_final.cmd’
    Building ‘spm/zephyr/CMakeFiles/spm_linker_pass_final_script_target’ from solution ‘build’ in configuration ‘Common’
    Building ‘spm/zephyr/spm_linker_pass_final_script_target’ from solution ‘build’ in configuration ‘Common’
    Building ‘spm/zephyr/isr_tables.c’ from solution ‘build’ in configuration ‘Common’
    1> Combining ‘spm/zephyr/isr_tables.c’
    Building ‘cmake_object_order_depends_target_spm_zephyr_final’ from solution ‘build’ in configuration ‘Common’
    Building ‘libspmsecureentries.a’ from solution ‘build’ in configuration ‘Common’
    1> Compiling ‘empty_file.c’
    2> Compiling ‘isr_tables.c’
    Building ‘spm/zephyr/zephyr.elf’ from solution ‘build’ in configuration ‘Common’
    1> Linking ‘libspmsecureentries.a’
    1> Post-link command 
    1> C:\gnuarmemb\bin\arm-none-eabi-objcopy.exe: 'zephyr.elf': No such file
    Build failed

    The build succeeded after I tried to "Build zephyr/zephyr.elf" before "Build Solution" again. I am assuming that I am doing something slightly wrong here or that I have some wrong settings. Nonetheless, I now have a merged.hex file. Normally I download this to the nRF9160 by connecting to it with J-link in SEGGER and chose the following before navigating to the corresponding build folder and selecting the merged.hex file:

    This time, however, I tried doing it like you suggested with the command line. I downloaded the latest nRF Command Line Tool here and followed your instructions. 

    PS C:\ncs\nrf\samples\nrf9160\at_client\build_nrf9160_pca10090ns\zephyr> nrfjprog -e
    Erasing user available code and UICR flash areas.
    Applying system reset.
    PS C:\ncs\nrf\samples\nrf9160\at_client\build_nrf9160_pca10090ns\zephyr> nrfjprog --program merged.hex -f nrf91 --sectorerase -r --verify
    Parsing hex file.
    Erasing page at address 0x0.
    Erasing page at address 0x1000.
    Erasing page at address 0x2000.
    Erasing page at address 0x3000.
    Erasing page at address 0x4000.
    Erasing page at address 0x5000.
    Erasing page at address 0x6000.
    Erasing page at address 0x7000.
    Erasing page at address 0xC000.
    Erasing page at address 0xD000.
    Erasing page at address 0xE000.
    Erasing page at address 0xF000.
    Erasing page at address 0x10000.
    Erasing page at address 0x11000.
    Erasing page at address 0x12000.
    Erasing page at address 0x13000.
    Erasing page at address 0x14000.
    Erasing page at address 0x15000.
    Erasing page at address 0x16000.
    Erasing page at address 0x17000.
    Erasing page at address 0x18000.
    Erasing page at address 0x19000.
    Erasing page at address 0x1A000.
    Erasing page at address 0x1B000.
    Erasing page at address 0x1C000.
    Erasing page at address 0x1D000.
    Erasing page at address 0x1E000.
    Erasing page at address 0x1F000.
    Erasing page at address 0x20000.
    Erasing page at address 0x21000.
    Erasing page at address 0x22000.
    Erasing page at address 0x23000.
    Erasing page at address 0x24000.
    Erasing page at address 0x25000.
    Erasing page at address 0x26000.
    Applying system reset.
    Checking that the area to write is not protected.
    Programming device.
    Verifying programming.
    Verified OK.
    Applying system reset.
    Run.
    PS C:\ncs\nrf\samples\nrf9160\at_client\build_nrf9160_pca10090ns\zephyr>

    After power cycling and connecting to the LTE Link Monitor and pressing reset again:

  • hfj said:
    I am assuming that I am doing something slightly wrong here or that I have some wrong settings.

    Yes, I think the issue lies in what project was built in the solution of SES. In SES you have Project 'zephyr/zephyr.elf' as Active when it should be Project 'zephyr/merged.hex'. Can you please Right-click on that project, and click "Set as Active project". This should be handled automatically. 

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

Reply Children
  • 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

Related