MQTT output scrambled

Hello everyone,

I am trying to send some data over to the MQTT broker using the Thingy91 and the example of 'Cellular IoT fundamentals - Lesson 4'. The example itself works well when not adjusted and publishes the string message to the MQTT broker. The adjustment that I am trying to implement has to send data of type 'double' over to the MQTT broker. To achieve this, I first convert the 'double' to 'char'. A simplified version of the altered code section looks like this: 

double accel_double = 10;   // A random-valued double

char acc[9];                        // 'char' variable

sprintf(acc, "%f", accel_double);    // double to char conversion

 

int err = data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE,

    acc, sizeof(acc)-1);            // The data publish function directly from the example, only difference is that 'acc' is now the input

 

The rest of the code is unaltered from the example and it builds without error. However, the output in the MQTT broker is scrambled:

topic = �[1]����

I expect that there is a problem with the data formatting, but I cannot find out what exactly. Can anyone help?

Parents
  • Hi,

    Have you tried to print acc on the device as well, to see if it is encoded correctly on the device?

    It might be that your application haven't enabled support for floats in the string library. Please see the attached project for how you can enable support for floats and doubles in strings.

    3323.hello_float.zip

    Best regards,

    Didrik

  • Thank you for the quick reply. 

    I tried the project you sent and it works fine on its own, every variation prints the number.

    However, when I copy/paste the lines of code into the main section of 'Cellular IoT fundamentals - Lesson 4', the following error happens:

    *** Booting Zephyr OS build v3.2.99-ncs1 ***
    
    
    
    
    Hello World! thingy91_nrf9160
    
    
    
    
    printk: 46.692345
    
    
    
    
    printf: 
    
    
    
    
    
    
    * buffer overflow detected *
    
    
    
    
    
    
    [00:00:01.510,894] 
    
    
    [1;31m<err> os: r0/a1:  0x00000003  r1/a2:  0x00000000  r2/a3:  0x00000001
    
    
    [0m
    
    
    
    
    [00:00:01.510,925] 
    
    
    [1;31m<err> os: r3/a4:  0x2000fcb8 r12/ip:  0x0000000a r14/lr:  0x0001f451
    
    
    [0m
    
    
    
    
    [00:00:01.510,955] 
    
    
    [1;31m<err> os:  xpsr:  0x61000000
    
    
    [0m
    
    
    
    
    [00:00:01.510,955] 
    
    
    [1;31m<err> os: Faulting instruction address (r15/pc): 0x0001d1f6
    
    
    [0m
    
    
    
    
    [00:00:01.510,986] 
    
    
    [1;31m<err> os: >>> ZEPHYR FATAL ERROR 3: Kernel oops on CPU 0
    
    
    [0m
    
    
    
    
    [00:00:01.511,016] 
    
    
    [1;31m<err> os: Current thread: 0x2000cf08 (unknown)
    
    
    [0m
    
    
    
    
    [00:00:01.555,664] 
    
    
    [1;31m<err> fatal_error: Resetting system
    
    
    [0m

    Alternatively, I tried placing it in the function that only activates when the button is pushed. This is also where I had placed my 'acc' variable initially. Then it only returns empty values after the second line:

    Hello World! thingy91_nrf9160
    
    
    
    
    printk: 46.692345
    
    
    
    
    printf: 
    
    
    
    
    
    
    printk sprintf: 
    
    
    
    
    printf sprintf: 
    
    
    
    
    
    
    the string is: 
    
    
  • Did you also copy the config options from prj.conf?

Reply Children
Related