Exercise 2

In this exercise, let’s use the LTE Link Controller library in nRF Connect SDK to establish the LTE connection. One advantage of using a high-level library instead of direct serial communication commands is that the library lets you asynchronously set up the LTE connection, which is what we will do in this exercise.

Exercise Steps

1. Open the base exercise code for this exercise in VS Code.

1.1 Clone the GitHub repository for this course.

1.2 In the nRF Connect extension in VS Code, select “Add an existing application“, and open the base code for this exercise, found in Cellular-IoT-Fundamentals/lesson2/cellfund_less2_exer2.

2. Enable the nRF Modem library.

Search for STEP 2 in prj.conf and add the following line:

More on this (lesson 3)

The nRF Modem Library is the library that interfaces with the nRF9160 modem and is necessary for the LTE link controller library to run. We will talk more about the nRF Modem Library in lesson 3, but for now we will just enable it in our application.

3. Enable and configure the LTE link controller library.

Search for STEP 3 in prj.conf and add the following lines

CONFIG_LTE_LINK_CONTROL enables the library and CONFIG_LTE_AUTO_INIT_AND_CONNECT disable the auto initialize and connect functionality.

4. Include the header file for the LTE link controller library.

In the main.c file, search for STEP 4 and add the following line

#include <modem/lte_lc.h>

5. Define the semaphore lte_connected.

Define a semaphore using K_SEM_DEFINE, with initial count 0 and max count 1, to be used when connecting.

Search for STEP 5 and add the following line

static K_SEM_DEFINE(lte_connected, 0, 1);

6. Define the function modem_configure() to initialize an LTE connection.

Search for STEP 6 and add the following lines

static void modem_configure(void)
	int err = lte_lc_init_and_connect_async(lte_handler);
	if (err) {
		LOG_ERR("Modem could not be configured, error: %d", err);

7. In the event handler for the link controller lte_handler().

In lte_handler(), in the event of changed registration status (LTE_LC_EVT_NEW_REG_STATUS), check if the modems registration status is connected (LTE_LC_NW_REG_REGISTERED_HOME or LTE_LC_NW_REG_REGISTERED_ROAMING), and if so, give the semaphore lte_connected.

Search for STEP 7.1 and add the following lines

	if ((evt->nw_reg_status != LTE_LC_NW_REG_REGISTERED_HOME) &&
	     (evt->nw_reg_status != LTE_LC_NW_REG_REGISTERED_ROAMING)) {

	LOG_INF("Network registration status: %s",
		evt->nw_reg_status == LTE_LC_NW_REG_REGISTERED_HOME ?
		"Connected - home network" : "Connected - roaming");

7.2 Upon the event of changed RRC mode (LTE_LC_EVT_RRC_UPDATE), print the RRC mode.

Search for STEP 7.2 and add the following lines

	LOG_INF("RRC mode: %s", evt->rrc_mode == LTE_LC_RRC_MODE_CONNECTED ? 
			"Connected" : "Idle");

8. In main(), call modem_configure() to initiate the LTE connection.

Search for STEP 8 and add the following lines


9. Take the semaphore lte_connected.

Take the semaphore using k_sem_take() with no timeout, which will only be given in the callback function once the modem’s registration status is connected.

Search for STEP 9 and add the following lines

k_sem_take(&lte_connected, K_FOREVER);

After taking the semaphore, the two next lines will only be run when the device is connected to the network at which point the semaphore is given.

10. Turn on the LED status LED

Upon successfully connecting to the network, turn on the LED status LED.

Search for STEP 10 and add the following line


11. Build and flash the application.

11.1 Add a build configuration and select whichever board you are using:

  • nRF9160 DK: nrf9160dk_nrf9160_ns
  • Thingy:91: thingy91_nrf9160_ns

11.2 Build and flash the application to your device.

You should see the following log output on your console

In addition, the connection LED (LED2 on the nRF9160 DK or green LED on the Thingy:91) should be lighted.


Your device can show up as multiple consecutive COM ports. If this is the case, you need to test which COM port is the correct one.

12. For further debugging purposes, let’s enable the AT Host library.

In the prj.conf file, search for STEP 12 and add the following lines to the prj.conf file

CONFIG_AT_HOST_LIBRARY is to enable the AT Host library and CONFIG_UART_INTERRUPT_DRIVEN enables interrupt support for UART so that the AT Host library can print asynchronously to the console.

The AT Host library automatically spawns a thread to print status AT commands.

13. Build and flash the application again.

You should notice intermittent unsolicited AT command notifications +CEREG and +CSCON being printed on the console among the log messages from the application.

14. Enabling the AT Host library also allows us to send AT commands while the application is running.

Open up LTE Link Monitor in nRF Connect for Desktop and select your hardware, just like we did in Exercise 1. Now we can send AT commands to the modem, just like we did in Exercise 1, while the application is running.

Let’s read out the IMEI of our device by sending:

Observe the AT command response containing the 15-digit IMEI (censored here).

The solution for this exercise can be found in lesson2/cellfund_less2_exer2_solution.

Register an account
Already have an account? Log in
(All fields are required unless specified optional)

Forgot your password?
Enter your email address, and we will send a link to reset your password.