Board is restarting when trying to convert double to uint8_t*

Hi

I am currently merging mqtt_simple and location example using my custom board based on nrf9160. Currently. I am able to connect to mqtt and also retrieving location using Cellular Service (maybe GNSS timeout because my friend test it at indoor location). I also able to publish ping message to free broker (broker.emqx.io) for testing mqtt. Ping message is condition that my board sending '1' to the broker, just for indicate that the board works properly.

And then, when I try to publish the location, suddenly the board is restarting. Below is the way I publish it.

			char buffer_lattitude[CONFIG_MQTT_MESSAGE_BUFFER_SIZE];
			sprintf(buffer_lattitude, "%.06f", event_data->location.lattitude); // it is restarting when the board at this step
			data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE, buffer_lattitude, sizeof(buffer_lattitude), TOPIC_LOC_CELLULER_LAT);

Anybody know the way to do that? Really appreciate for your help. Thanks.

  • Hi,
    Are you sure CONFIG_MQTT_MESSAGE_BUFFER_SIZE is big enough? Can't see any other obvious reasons why it should fail based on these lines. Do you not have to convert the message after filling the buffer_lattitude? My memory might fail me, but it could be a hint. Maybe its failing on the next line? Try this:

    char buffer_lattitude[CONFIG_MQTT_MESSAGE_BUFFER_SIZE];
    sprintf(buffer_lattitude, "%.06f", event_data->location.lattitude);
    struct mqtt_utf8 message;
    message.utf8 = (uint8_t *)buffer_lattitude;
    message.size = (uint32_t)strlen(buffer_lattitude)
    data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE, message.utf8, message.size, TOPIC_LOC_CELLULER_LAT);
    Edit: Its not just a typo? "location.lattitude" should be "location.latitude"?
  • "%.06f" is for a single precision ("float") type, you'll want "%.06lf" for a double.  Not sure if this could possibly cause a fault or exception.

  • Thanks for your contribution. For CONFIG_MQTT_MESSAGE_BUFFER_SIZE, I set it as 128 bytes, it is larger than the message. So not sure if that is the issue.

    Yes, probably it is a memory fail.

    For lattitude, yes it is like that, no issue with typo.

    Anyway, thanks for the solution, really appreciate it, but still doesnt work. Looks like it is restarting when executing sprintf.

  • Hi guys,

    I manage to convert double to char using sprintf. The issue is size is too big, now I change it to 32 now it works.

    But I got an issue, there is a time that my value result is 0.000000 I dont know why. When I print previous value, there is no issue, but when I double check the result, it is 0.00000. Also another thing, there is a time that I can convert value properly. So there is a time the value is weird, and also there is a time that looks correct.

    below is the code that I use

    			printk("cellular service.\n");
    
    			printk("Publishing longitude %f ", event_data->location.longitude);
    			sprintf(temp_lon, " %f ", event_data->location.longitude);
    			printk("value : %s ", temp_lon);
    			err = data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE, temp_lon, sizeof(temp_lon), TOPIC_LOC_CELLULER_LON);
    			if (err) {printk("ERR data_publish (longitude) : %d", err);} else {printk(" -- success.\n");}
    
    			printk("Publishing latitude %f ", event_data->location.latitude);
    			sprintf(temp_lat, " %f ", event_data->location.latitude);
    			printk("value : %s ", temp_lat);
    			err = data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE, temp_lat, sizeof(temp_lat), TOPIC_LOC_CELLULER_LAT);
    			if (err) {printk("ERR data_publish (longitude) : %d", err);} else {printk(" -- success.\n");}
    
    			printk("Publishing both information ");
    			sprintf(temp_all, " %f,%f ", event_data->location.latitude, event_data->location.longitude);
    			printk("value : %s ", temp_all);
    			err = data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE, temp_all, sizeof(temp_all), TOPIC_LOC_CELLULER);
    			if (err) {printk("ERR data_publish (both) : %d", err);} else {printk(" -- success.\n");}	

    And below is the result. You can see there is a time that I got 0.000000 and also there is a time that I got a right value.

    Publishing Location using cellular service.
    Publishing longitude 46.693770 value :  0.000000   -- success.
    Publishing latitude  24.791375 value :  0.000000   -- success.
    Publishing both information value :  0.000000,46.693370   -- success.
    Requesting location with the default configuration...

    Anybody have idea about this? thank you so much for your help.

Related