The Location sample demonstrates how you can retrieve the location of a device using GNSS, cellular or Wi-Fi positioning method. This sample uses the Location library.
The sample supports the following development kits:
When built for an
_ns build target, the sample is configured to compile and run as a non-secure application.
Therefore, it automatically includes Trusted Firmware-M that prepares the required peripherals and secure services to be available for the application.
See also the requirements in Location library document.
Modem version v1.3.x or later is also required.
Your device may ship with modem firmware older than v1.3.x, so verify that you have the latest installed.
For instructions on updating your device’s modem firmware, see the following documents:
To obtain and register a valid signing key, you can do one of the following:
Provision your device on nRF Cloud using Just-In-Time Provisioning (JITP) (detailed below).
Provision your device on nRF Cloud with preconnect provisioning (detailed in nRF Cloud Provisioning).
Install or generate a private key on your device and register its public key with nRF Cloud (detailed in Securely Generating Credentials on the nRF9160).
To provision your device on nRF Cloud using JITP, complete the following steps:
Ensure valid nRF Cloud credentials are installed. nRF9160 DK and Thingy:91 have credentials pre-installed, but some may be shipped with RSA256 credentials; JWT signing requires ES256 credentials. These devices need new credentials to be installed before they can perform REST requests.
Install a sample or application that supports JITP. You can use the Asset Tracker V2 application for this purpose.
Allow your device to connect to nRF Cloud and add it to the nRF Cloud portal. When successfully provisioned, the device appears in the devices list of the nRF Cloud portal.
Once your device is provisioned, you may reinstall this sample and the provisioning persists.
The Location sample retrieves the location multiple times to illustrate the different ways of retrieving the location of a device. Each individual location request has been implemented in a separate function within the sample. In addition to the Location library, this sample uses LTE link controller to control the LTE connection.
See Configuring your application for information about how to permanently or temporarily change the configuration.
Refer to the Location library document for configuring the location retrieval behavior, including supported location methods and services.
If you use an external GNSS antenna, add the following configuration:
CONFIG_MODEM_ANTENNA_GNSS_EXTERNAL- Selects an external GNSS antenna.
The sample provides predefined configuration files for typical use cases.
You can find the configuration files in the
The following files are available:
esp_8266_nrf9160ns.overlay- DTC overlay for ESP8266 Wi-Fi chip support.
overlay-esp-wifi.conf- Config overlay for ESP8266 Wi-Fi chip support.
overlay-pgps.conf- Config overlay for P-GPS support.
This sample can be found under
samples/nrf9160/location in the nRF Connect SDK folder structure.
When built as a non-secure firmware image for the
_ns build target, the sample automatically includes the Trusted Firmware-M (TF-M).
To build the sample with Visual Studio Code, follow the steps listed on the Building nRF Connect SDK application quick guide page in the nRF Connect for VS Code extension documentation. See Building and programming an application for other building and programming scenarios and Testing and debugging an application for general information about testing and debugging in the nRF Connect SDK.
To build the Location sample with ESP8266 Wi-Fi chip support, use the
west build -p -b nrf9160dk_nrf9160_ns -- -DDTC_OVERLAY_FILE=esp_8266_nrf9160ns.overlay -DOVERLAY_CONFIG=overlay-esp-wifi.conf
See Providing CMake options for more instructions on how to add these options.
To build the Location sample with P-GPS support, use the
west build -p -b nrf9160dk_nrf9160_ns -- -DOVERLAY_CONFIG=overlay-pgps.conf
See Providing CMake options for more instructions on how to add this option.
After programming the sample to your development kit, complete the following steps to test it:
Connect the kit to the computer using a USB cable. The kit is assigned a COM port (Windows) or ttyACM device (Linux), which is visible in the Device Manager.
Connect to the kit with a terminal emulator (for example, PuTTY). See How to connect with PuTTY for the required settings.
Observe that the sample prints to the terminal.
An example output of the sample:
Location sample started Connecting to LTE... Connected to LTE Requesting location with short GNSS timeout to trigger fallback to cellular... [00:00:06.481,262] <wrn> location: Timeout occurred [00:00:06.487,335] <wrn> location: Failed to acquire location using 'GNSS', trying with 'Cellular' next Got location: method: cellular latitude: 12.887095 longitude: 55.580397 accuracy: 1250.0 m Google maps URL: https://maps.google.com/?q=12.887095,55.580397 Requesting location with the default configuration... Got location: method: GNSS latitude: 12.893736 longitude: 55.575859 accuracy: 4.4 m date: 2021-10-28 time: 13:36:29.072 UTC Google maps URL: https://maps.google.com/?q=12.893736,55.575859 Requesting location with high GNSS accuracy... Got location: method: GNSS latitude: 12.893755 longitude: 55.575879 accuracy: 2.8 m date: 2021-10-28 time: 13:36:32.339 UTC Google maps URL: https://maps.google.com/?q=12.893755,55.575879 Requesting Wi-Fi location with GNSS and cellular fallback... Got location: method: GNSS latitude: 12.893770 longitude: 55.575884 accuracy: 4.5 m date: 2021-10-28 time: 13:36:45.895 UTC Google maps URL: https://maps.google.com/?q=12.893770,55.575884 Requesting 30s periodic GNSS location... Got location: method: GNSS latitude: 12.893765 longitude: 55.575912 accuracy: 4.4 m date: 2021-10-28 time: 13:36:47.536 UTC Google maps URL: https://maps.google.com/?q=12.893765,55.575912 Got location: method: GNSS latitude: 12.893892 longitude: 55.576090 accuracy: 8.4 m date: 2021-10-28 time: 13:37:17.685 UTC Google maps URL: https://maps.google.com/?q=12.893892,55.576090
This sample uses the following nRF Connect SDK libraries:
In addition, it uses the following secure firmware component: