This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Convert int64_t to string

I'm using NCS 1.8.0 and nRF9160 DK, and I need to convert unix_time from int64_t to string, my problem is exactly what is reported in these cases:

devzone.nordicsemi.com/.../snprintf-int64_t-not-working
https://devzone.nordicsemi.com/f/nordic-q-a/83967/long-long-int-value-sprintf

My conversion attempts can be seen below, but in all of them I don't get the number in value_buf_time, but I get lu.

snprintf(value_buf_time, sizeof(value_buf_time), "%"PRIu64, unix_time_ms);
printk("Publicando o time: %s ",value_buf_time);
or
snprintf(value_buf_timesizeof(value_buf_time), "%ld"unix_time_ms);
printk("Publicando o time: %s ",value_buf_time);
or
snprintf(value_buf_timesizeof(value_buf_time), "%lld"unix_time_ms);
printk("Publicando o time: %s ",value_buf_time);
The output of all printk is:
 
Publicando o time: lu
 
In other cases the solution was to insert CONFIG_NEWLIB_LIBC_NANO=n. But I can't insert this because when I have LIBC_NANO=n I end up getting a HARD_FAULT in the lte_lc_init_and_connect() function call. Is there another solution to make this conversion work?
Thank you.
Parents
  • Hello Gabriel,

    Thabet said:
    So to solve this, following the recommendations found in Devzone I must add CONFIG_NEWLIB_LIBC_NANO=n, so that it is possible to transform int64_t into a string, and when I add this the error occurs.

    I’m still not able to reproduce the hard fault.

    main.c

    void main(void)
    {
    	int err;
    	uint32_t connect_attempt = 0;
    
    	char buf[64];
    	int64_t unix_time_ms;
    
    	LOG_INF("The MQTT simple sample started");
    
    	err = date_time_now(&unix_time_ms);
    	if(err){
    		printk("Couldn't get date time: %d\n",err);
    	}
    
    	printk("Measurement Instant: %lld \n", unix_time_ms);
    	snprintf(buf, sizeof(buf),
        "{"
            "\"ts\":%lld"
    		"}\n",
            unix_time_ms);    
    	printk("%s", buf);	
    
    #if defined(CONFIG_MQTT_LIB_TLS)

    Terminal output

    *** Booting Zephyr OS build v2.7.99-ncs1-1  ***
    Couldn't get date time: -61
    Measurement Instant: 42949768085 
    {"ts":42949768085}
    [00:00:00.631,042] <inf> mqtt_simple: The MQTT simple sample started
    [00:00:00.631,072] <wrn> date_time: Valid time not currently available
    [00:00:00.638,397] <inf> mqtt_simple: Disabling PSM and eDRX
    [00:00:00.645,355] <inf> mqtt_simple: LTE Link Connecting...
    [00:00:02.573,303] <inf> mqtt_simple: LTE Link Connected!
    [00:00:02.883,850] <inf> mqtt_simple: IPv4 Address found 137.135.83.217
    [00:00:02.884,338] <dbg> mqtt_simple.client_id_get: client_id = nrf-352656106118402
    [00:00:03.262,268] <inf> mqtt_simple: MQTT client connected
    [00:00:03.262,268] <inf> mqtt_simple: Subscribing to: my/subscribe/topic len 18
    [00:00:03.414,276] <inf> mqtt_simple: SUBACK packet id: 1234

    What is the current stack size value of the main thread? Have you tried to increase it?

    Regards,

    Markus

  • Ainda não consigo reproduzir a falha grave.

    Did you add the settings below to prj.conf and no errors occurred?

    CONFIG_DATE_TIME=y

    CONFIG_DATE_TIME_MODEM=y

    CONFIG_NEWLIB_LIBC_NANO=n

    I'm using NCS 1.8.0 and just add these lines to the sample for the error to occur! If you did this and the errors did not occur, could you send me your sample?

    In the mqtt_simple sample I have the stack size configured like this:

    CONFIG_MAIN_STACK_SIZE=4096

    CONFIG_HEAP_MEM_POOL_SIZE=2048

    Even if I put the settings below the error still appears!
    CONFIG_MAIN_STACK_SIZE=16384
    CONFIG_HEAP_MEM_POOL_SIZE=16384
Reply
  • Ainda não consigo reproduzir a falha grave.

    Did you add the settings below to prj.conf and no errors occurred?

    CONFIG_DATE_TIME=y

    CONFIG_DATE_TIME_MODEM=y

    CONFIG_NEWLIB_LIBC_NANO=n

    I'm using NCS 1.8.0 and just add these lines to the sample for the error to occur! If you did this and the errors did not occur, could you send me your sample?

    In the mqtt_simple sample I have the stack size configured like this:

    CONFIG_MAIN_STACK_SIZE=4096

    CONFIG_HEAP_MEM_POOL_SIZE=2048

    Even if I put the settings below the error still appears!
    CONFIG_MAIN_STACK_SIZE=16384
    CONFIG_HEAP_MEM_POOL_SIZE=16384
Children
No Data
Related