Can't read time correctly using nrf_modem lib and AT CCLK

Hi, 

I have been experimenting with the different nrf_modem functions to get AT responses. I want to receive the current time (don't need to be accurate) using AT+CCLK?, but get some strange output as shown:

AT+CFUN?
Activating LTE... 
OK!
rc: -77
Modem response: èß 
Turning off LTE
SHUTDOWN.

Only one time I got a reasonable answer. Here is my code, can you see what is wrong?

void main(void)
{
	int err;
	char response[32];
	int rc;
	
	k_sleep(K_MSEC(2000)); //Just to get time to open LTE Link monitor.

	printk("Activating LTE... \n");
	err = lte_lc_init_and_connect(); //BLOCKS until connection.
	if (err) {
		printk("Failed to connect to the LTE network, err %d\n", err);
		return;
	}
	printk("OK!\n");
	rc = nrf_modem_at_scanf("AT+CCLK?", &response);
	printk("rc: %d\n", rc);

	printf("Modem response: %s \n",response);

	nrf_modem_lib_shutdown();
	
	printk("Turning off LTE\n");
	lte_lc_power_off();
	
	printk("SHUTDOWN.\n");
}

  • void main(void)
    {
    	int err;
    	char response[513];
    	int rc;
    	
    	k_sleep(K_MSEC(2000)); //Just to get time to open LTE Link monitor.
    
    	printk("Activating LTE... \n");
    	err = lte_lc_init_and_connect(); //BLOCKS until connection.
    	if (err) {
    		printf("Failed to connect to the LTE network, err %d\n", err);
    		return;
    	}
    	printk("OK!\n");
    
    	k_busy_wait(1000000);
    	rc = nrf_modem_at_scanf("AT+CCLK?", "+CCLK: %s", &response);
    	
    	printf("rc: %d\n", rc);
    	printf("Modem response: %s \n",response);
    
    	nrf_modem_lib_shutdown();
    	
    	printk("Turning off LTE\n");
    	lte_lc_power_off();
    	
    	printk("SHUTDOWN.\n");
    }

    This solved it, by implementing a busy_wait.

  • Yes, you'll need to get the time from the network, which will happen after initial attach.

    k_busy_wait() will block and use the CPU for waiting, and it is commonly used when you're waiting for only several micro-seconds, where entering sleep does not give any advantage. k_sleep() will use the RTC for waiting and lower your power consumption.

Related