nRF9160 schedule connect and disconnect LTE connection

I am only want to send and MQTT message every day. Because of this I think it is better to turnoff the modem and reconnect when needed. I tried to alter the LTE Link example a bit but it does not work as expected. I think it has something to do with the timing of the work thread. I schedule the connect when the device is turned on. When this function is successful a new work item is scheduled to disconnect the modem. 

The fist connect does not respond. 

#include <zephyr/kernel.h>
#include <modem/lte_lc.h>

/* Semaphore used to block the main thread until the link controller has
 * established an LTE connection.
 */
K_SEM_DEFINE(lte_connected, 0, 1);

static struct k_work_delayable connectLTE_work;
static struct k_work_delayable disconnectLTE_work;

static void lte_handler(const struct lte_lc_evt *const evt)
{
     switch (evt->type) {
     case LTE_LC_EVT_NW_REG_STATUS:
             if ((evt->nw_reg_status != LTE_LC_NW_REG_REGISTERED_HOME) &&
             (evt->nw_reg_status != LTE_LC_NW_REG_REGISTERED_ROAMING)) {
                     break;
             }

             printk("Connected to: %s network\n",
             evt->nw_reg_status == LTE_LC_NW_REG_REGISTERED_HOME ? "home" : "roaming");

             k_sem_give(&lte_connected);
             break;
     case LTE_LC_EVT_PSM_UPDATE:
     case LTE_LC_EVT_EDRX_UPDATE:
     case LTE_LC_EVT_RRC_UPDATE:
     case LTE_LC_EVT_CELL_UPDATE:
     case LTE_LC_EVT_LTE_MODE_UPDATE:
     case LTE_LC_EVT_TAU_PRE_WARNING:
     case LTE_LC_EVT_NEIGHBOR_CELL_MEAS:
     case LTE_LC_EVT_MODEM_SLEEP_EXIT_PRE_WARNING:
     case LTE_LC_EVT_MODEM_SLEEP_EXIT:
     case LTE_LC_EVT_MODEM_SLEEP_ENTER:
             /* Callback events carrying LTE link data */
             break;
     default:
             break;
     }
}

static void connectLTE(struct k_work *work) 
{
    int err;
	printk("Connecting to LTE network. This may take a few minutes...\n");

    err = lte_lc_init_and_connect_async(lte_handler);
    if (err) {
            printk("lte_lc_init_and_connect_async, error: %d\n", err);
            return;
	}

	k_sem_take(&lte_connected, K_FOREVER);

	printk("Connected!\n");
	
	//Disconnect after 30 seconds
	k_work_schedule(&disconnectLTE_work, K_SECONDS(30));		

}

static void disconnectLTE(struct k_work *work) 
{
    int err;
	printk("Disconnecting...\n");

    err = lte_lc_offline();
    if (err) {
            printk("Failed to discconet\n");
            return;
	}

	printk("Disconnected!\n");

	//Connect after 30 seconds
	k_work_schedule(&connectLTE_work, K_SECONDS(30));		
}



static void init_work(void)
{
	k_work_init_delayable(&connectLTE_work, connectLTE);
	k_work_init_delayable(&disconnectLTE_work, disconnectLTE);	

}

void main(void)
{
	init_work();
	//Schedule connect now
	k_work_schedule(&connectLTE_work, K_NO_WAIT);	
}

Output

14:32:07.540 -> *** Booting Zephyr OS build v3.1.99-ncs1-1  ***
14:32:07.540 -> Connecting to LTE network. This may take a few minutes...

What would be the correct way to archive something like this? 

Parents
  • Hi!

    If you can use PSM on your network, it would probably be better to stay connected in PSM than disconnecting every day. Reconnecting requires renegotiation with the network, and this usually costs more power than just staying connected. I saw in some Nordic video that a general rule of thumb is that it's better to stay in PSM for up to 68 hours.

    I can also tip about this thread. Not knowing if exactly 30 seconds is the right time, you could try something like that.

    I think I would use one thread that loops. Connect, send the msg (including polling etc), disconnect, sleep for 24 hours (perhaps adjust for the time is used if exact times are important)

    I see that your connecting/disconnecting every 30 seconds. Perhaps I misunderstood your post, but I interpreted it as you want to connect and send data once per day?

    Kind regards,

    Torje

  • Hi Torje, 

    Thanks for your message! I am using PSM now, consumption is ok. I have some issues now with config_serial=no but I will open another thread for it. I still don't understand why my application is not working, if anyone in the meantime has an idea let me know. 

Reply Children
Related