nRF Connect SDK 中的 Cellular: Location示例演示了如何使用 nRF Cloud 提供的不同定位服务。
让我们测试一下 nRF Cloud Wi-Fi 定位服务如何在室内环境中工作。
硬件:nRF9160 DK、nRF7002 EK、PPK2。
软件:nRF Connect SDK v2.4.2、MFW v1.3.5、nRF Connect for Desktop 中的 Power Profiler 应用程序。
硬件连接设置如下图所示。在 Power Profiler 应用程序中,配置 PPK2 源表模式,为 DK 上的 nRF9160模组供电并记录其电流消耗变化。
Cellular: Location 示例中最初的 location_wifi_get() 函数可用于请求 Wi-Fi 定位服务。启用附加日志配置有助于了解请求过程。
Add the following lines to the end of prj.conf
CONFIG_LOCATION_LOG_LEVEL_DBG=y
CONFIG_NRF_CLOUD_REST_LOG_LEVEL_DBG=y
CONFIG_REST_CLIENT_LOG_LEVEL_DBG=y
CONFIG_NRF_CLOUD_LOG_LOG_LEVEL_DBG=y
CONFIG_NRF_CLOUD_LOG_LEVEL_DBG=y
让我们来了解一下如何使用 location_wifi_get() 请求 Wi-Fi 定位服务。我们将这一过程分为三个步骤来解释 Wi-Fi 定位服务请求过程。
步骤 1:扫描 Wi-Fi 接入点信息
在此步骤中,配置 Wi-Fi 定位服务,设备开始扫描附近的 Wi-Fi 接入点。结果显示,设备在奥斯陆Nordic Semiconductor办公楼附近或内找到了 14 个 Wi-Fi 接入点。
Requesting Wi-Fi location with GNSS and cellular fallback...
[00:01:19.903,594] location: location_core_config_log: Location configuration:
[00:01:19.911,682] location: location_core_config_log: Methods count: 1
[00:01:19.919,342] location: location_core_config_log: Interval: 0
[00:01:19.926,605] location: location_core_config_log: Timeout: 300000ms
[00:01:19.934,356] location: location_core_config_log: Mode: 0
[00:01:19.941,223] location: location_core_config_log: List of methods:
[00:01:19.948,913] location: location_core_config_log: Method #0
[00:01:19.956,146] location: location_core_config_log: Method type: Wi-Fi (3)
[00:01:19.964,660] location: location_core_config_log: Timeout: 30000ms
[00:01:19.972,686] location: location_core_config_log: Service: Any (0)
[00:01:20.044,921] location: location_request_info_create: Wi-Fi and cellular methods are not one after the other in method list so they are not combined
[00:01:20.059,448] location: location_core_location_get_pos: Requesting location with 'Wi-Fi' method
[00:01:20.069,458] location: location_core_location_get_pos: Starting request timer with timeout=300000
[00:01:20.079,711] location: location_core_timer_start: Starting timer with timeout=30000
[00:01:20.088,745] location: scan_wifi_start: Triggering start of Wi-Fi scanning
[00:01:24.832,458] location: scan_wifi_result_handle: scan result #1 stored: ssid XXXX, channel 108, mac 24:36:da:16:xx:xx
[00:01:24.845,184] location: scan_wifi_result_handle: scan result #2 stored: ssid XXXX, channel 108, mac 24:36:da:16:xx:xx
[00:01:24.858,093] location: scan_wifi_result_handle: scan result #3 stored: ssid XXXX, channel 11, mac 24:36:da:16:xx:xx
[00:01:24.870,910] location: scan_wifi_result_handle: scan result #4 stored: ssid XXXX, channel 11, mac 24:36:da:16:xx:xx
[00:01:24.883,483] location: scan_wifi_result_handle: scan result #5 stored: ssid XXXX, channel 1, mac 24:36:da:11:xx:xx
[00:01:24.896,209] location: scan_wifi_result_handle: scan result #6 stored: ssid XXXX, channel 1, mac 24:36:da:17:xx:xx
[00:01:24.908,935] location: scan_wifi_result_handle: scan result #7 stored: ssid XXXX, channel 1, mac 24:36:da:17:xx:xx
[00:01:24.921,417] location: scan_wifi_result_handle: scan result #8 stored: ssid XXXX, channel 64, mac 24:36:da:11:xx:xx
[00:01:24.933,990] location: scan_wifi_result_handle: scan result #9 stored: ssid XXXX, channel 64, mac 24:36:da:11:xx:xx
[00:01:24.946,807] location: scan_wifi_result_handle: scan result #10 stored: ssid XXXX, channel 6, mac 8a:5a:85:af:xx:xx
[00:01:24.959,442] location: scan_wifi_result_handle: scan result #11 stored: ssid XXXX, channel 100, mac 24:36:da:17:xx:xx
[00:01:24.972,167] location: scan_wifi_result_handle: scan result #12 stored: ssid XXXX, channel 6, mac 34:21:09:48:xx:xx
[00:01:24.984,649] location: scan_wifi_result_handle: scan result #13 stored: ssid XXXX, channel 132, mac 24:36:da:11:xx:xx
[00:01:24.997,619] location: scan_wifi_result_handle: scan result #14 stored: ssid XXXX, channel 132, mac 24:36:da:11:xx:xx
[00:01:25.010,284] location: scan_wifi_done_handle: Scan request done with 14 Wi-Fi Aps
步骤 2:请求 nRF Cloud Wi-Fi 定位
nRF9160 会将 Wi-Fi 接入点信息(包括 MAC 地址、SSID、信号强度和信道)编码为 JSON 文件。然后,JSON 文件将被发送到 nRF Cloud,nRF Cloud 将使用这些数据检查 Wi-Fi 定位数据库,并使用特定算法计算设备位置。结果包含纬度、经度、精确度等信息。有关 API 请求和响应格式,请参阅 nRF Cloud REST API 文档。
[00:01:25.028,015] location: cloud_service_location_get: Cloud service location parameters:
[00:01:25.037,170] location: cloud_service_location_get: Service: 0
[00:01:25.044,494] location: cloud_service_location_get: Timeout: 25060ms
[00:01:25.052,337] location: cloud_service_nrf_pos_get: Sending positioning request (REST)
[00:01:25.160,125] nrf_cloud_codec_internal: nrf_cloud_location_req_json_encode: JSON: {"wifi":{"accessPoints":[{"macAddress":"24:36:da:16:xx:xx","ssid":"XXXX","signalStrength":-41,"channel":108},{"macAddress":"24:36:da:16:xx:xx","ssid":"XXXX","signalStrength":-41,"channel":108},{"macAddress":"24:36:da:16:xx:xx","ssid":"XXXX","signalStrength":-48,"channel":11},{"macAddress":"24:36:da:16:xx:xx","ssid":"XXXX","signalStrength":-49,"channel":11},{"macAddress":"24:36:da:11:xx:xx","ssid":"XXXX","signalStrength":-63,"channel":1},{"macAddress":"24:36:da:17:xx:xx","ssid":"XXXX","signalStrength":-70,"channel":1},{"macAddress":"24:36:da:17:xx:xx","ssid":"XXXX","signalStrength":-70,"channel":1},{"macAddress":"24:36:da:11:xx:xx","ssid":"XXXX","signalStrength":-72,"channel":64},{"macAddress":"24:36:da:11:xx:xx","ssid":"XXXX","signalStrength":-72,"channel":64},{"macAddress":"8a:5a:85:af:xx:xx","ssid":"XXXX","signalStrength":-73,"channel":6},{"macAddress":"24:36:da:17:xx:xx","ssid":"XXXX","signalStrength":-75,"channel":100},{"macAddress":"34:21:09:48:xx:xx","ssid":"XXXX","signalStrength":-76,"channel":6},{"macAddress":"24:36:da:11:xx:xx","ssid":"XXXX","signalStrength":-85,"channel":132},{"macAddress":"24:36:da:11:xx:xx","ssid":"XXXX","signalStrength":-85,"channel":132}]}}
[00:01:25.283,142] rest_client: rest_client_request: Requesting destination HOST: api.nrfcloud.com at port 443, URL: /v1/location/ground-fix
[00:01:25.296,966] rest_client: rest_client_request: Payload: {"wifi":{"accessPoints":[{"macAddress":"24:36:da:16:xx:xx","ssid":"XXXX","signalStrength":-41,"channel":108},{"macAddress":"24:36:da:16:xx:xx","ssid":"XXXX","signalStrength":-41,"channel":108},{"macAddress":"24:36:da:16:xx:xx","ssid":"XXXX","signalStrength":-48,"channel":11},{"macAddress":"24:36:da:16:xx:xx","ssid":"XXXX","signalStrength":-49,"channel":11},{"macAddress":"24:36:da:11:xx:xx","ssid":"XXXX","signalStrength":-63,"channel":1},{"macAddress":"24:36:da:17:xx:xx","ssid":"XXXX","signalStrength":-70,"channel":1},{"macAddress":"24:36:da:17:xx:xx","ssid":"XXXX","signalStrength":-70,"channel":1},{"macAddress":"24:36:da:11:xx:xx","ssid":"XXXX","signalStrength":-72,"channel":64},{"macAddress":"24:36:da:11:xx:xx","ssid":"XXXX","signalStrength":-72,"channel":64},{"macAddress":"8a:5a:85:af:xx:xx","ssid":"XXXX","signalStrength":-73,"channel":6},{"macAddress":"24:36:da:17:xx:xx","ssid":"XXXX","signalStrength":-75,"channel":100},{"macAddress":"34:21:09:48:xx:xx","ssid":"XXXX","signalStrength":-76,"channel":6},{"macAddress":"24:36:da:11:xx:xx","ssid":"XXXX","signalStrength":-85,"channel":132},{"macAddress":"24:36:da:11:xx:xx","ssid":"XXXX","signalStrength":-85,"channel":132}]}}[00:01:25.417,358] rest_client: rest_client_sckt_connect: Doing getaddrinfo() with connect addr api.nrfcloud.com port 443
[00:01:25.435,699] rest_client: rest_client_sckt_connect: getaddrinfo() 52.70.xxx.xxx
[00:01:25.445,892] rest_client: rest_client_sckt_connect: Connecting to api.nrfcloud.com port 443
+CSCON: 1
[00:01:26.902,130] rest_client: rest_client_http_response_cb: HTTP: All data received (content/total: 79/328), status: 200 OK
[00:01:26.914,245] rest_client: rest_client_request: API call response len: http status: 200, 79 bytes
[00:01:26.926,025] location: location_core_event_cb_fn: Location acquired successfully:
[00:01:26.934,844] location: location_core_event_cb_fn: method: Wi-Fi (3)
[00:01:26.942,901] location: location_core_event_cb_fn: latitude: 59.920980
[00:01:26.950,988] location: location_core_event_cb_fn: longitude: 10.689317
[00:01:26.959,136] location: location_core_event_cb_fn: accuracy: 15.0 m
[00:01:26.966,857] location: location_core_event_cb_fn: date: 2023-08-31
[00:01:26.974,609] location: location_core_event_cb_fn: time: 13:49:40.035 UTC
[00:01:26.982,879] location: location_core_event_cb_fn: Google maps URL: https://maps.google.com/?q=59.920980,10.689317
Got location:
method: Wi-Fi
latitude: 59.920980
longitude: 10.689317
accuracy: 15.0 m
date: 2023-08-31
time: 13:49:40.035 UTC
Google maps URL: https://maps.google.com/?q=59.920980,10.689317
步骤 3:设备进入休眠状态
当 RRC 非活动定时器到期时,nRF9160 将进入 RRC 空闲状态。我将活动定时器 (T3324) 设置为 0 (CONFIG_LTE_PSM_REQ_RAT=“00000000”) 以帮助设备进入 RRC 空闲状态后立即进入 PSM 以节省功耗。RRC 闲置定时器由移动网络运营商配置(此处挪威电信为 5 秒)。如果 MNO 支持快速释放功能 (AS-RAI),设备可通过发送 RAI 通知基站释放它们之间的无线连接,从而立即进入 RRC 空闲状态。这将进一步降低总功耗。您可以从这篇博客中了解有关 RAI 的更多信息:最大限度延长蜂窝物联网的电池寿命: 对 eDRX、PSM 和 AS-RAI 的分析。