

# 設定 LoRaWAN 裝置的位置
<a name="lorawan-location-devices"></a>

您將裝置新增至 AWS IoT Core for LoRaWAN 時，可以指定靜態位置資訊、選擇性啟用定位，以及指定目的地。目的地描述了處理裝置位置資訊並將更新後的位置路由至 Amazon Location Service 的 IoT 規則。設定裝置位置後，位置資料會顯示在 Amazon Location 地圖上，其中包含準確度資訊和您指定的目的地。

您可以使用 AWS 管理主控台、AWS IoT Wireless API 或 AWS CLI 來設定裝置的位置。

## 訊框連接埠和上行訊息格式
<a name="lorawan-location-devices-fportuplink"></a>

如果啟用定位，您必須指定地理位置訊框連接埠，以便將 Wi-Fi 和 GNSS 掃描資料從裝置傳送到 AWS IoT Core for LoRaWAN。位置資訊會使用此訊框連接埠傳送給 AWS IoT Core for LoRaWAN。

LoRaWAN 規格提供資料交付欄位 (FRMPayload) 和連接埠欄位 (FPort)，以區分不同類型的訊息。若要傳送位置資訊，您可以為訊框連接埠指定 1 到 223 之間的任意值。FPort 0 會保留給 MAC 訊息使用，FPort 224 會保留給 MAC 相容性測試使用，連接埠 225-255 則保留給未來的標準化應用程式延伸模組。

### AWS IoT Core for LoRaWAN 傳送至規則引擎的上行訊息
<a name="lorawan-location-devices-uplink"></a>

新增目的地時，則會建立 AWS IoT 規則，以使用規則引擎將資料路由到 Amazon Location Service。更新後的位置資訊隨即會顯示在 Amazon Location 地圖上。如果尚未啟用定位，目的地會在您更新裝置的靜態位置座標時路由位置資料。

下列程式碼顯示從 AWS IoT Core for LoRaWAN 傳送的上行訊息格式，並提供位置資訊、準確度、求解器組態和無線中繼資料。下面反白顯示的欄位為選用項目。如果沒有垂直準確度資訊，則值為 `null`。

```
{
    // Position configuration parameters for given wireless device
    "WirelessDeviceId": {{"5b58245e-146c-4c30-9703-0ca942e3ff35"}},     

    // Position information for a device in GeoJSON format. Altitude
    // is optional. If no vertical accuracy information is available
    // or positioning isn't activated, the value is set to null.
    // The position information coordinates are listed in the order 
    // [longitude, latitude, altitude].
    "coordinates": [{{33.33000183105469, -22.219999313354492}}, 99.0],
    "type": "Point",
    "properties": { 
         "horizontalAccuracy": {{number}},
         "verticalAccuracy": number",
         "timestamp": "2022-08-19T03:08:35.061Z"
    },

    //Parameters controlled by AWS IoT Core for LoRaWAN
    "WirelessMetadata": 
    {
        "LoRaWAN":
        {
            "ADR": false,
            "Bandwidth": {{125}},
            "ClassB": false,
            "CodeRate": "{{4/5}}",
            "DataRate": "0",
            "DevAddr": "{{00b96cd4}}",
            "DevEui": "{{58a0cb000202c99}}",            
            "FOptLen": 2,
            "FCnt": 1,
            "Fport": {{136}},   
            "Frequency": "{{868100000}}",     
            "Gateways": [
             {
                    "GatewayEui": "{{80029cfffe5cf1cc}}",      
                    "Snr": {{-29}},
                    "Rssi": {{9.75}}
             }
             ],  
            "MIC": "{{7255cb07}}",  
            "MType": "UnconfirmedDataUp",
            "Major": "LoRaWANR1",
            "Modulation": "LORA", 
            "PolarizationInversion": false,    
            "SpreadingFactor": {{12}},                         
            "Timestamp": "{{2021-05-03T03:24:29Z}}"
            
        }
    }
}
```

## 使用主控台設定裝置的位置
<a name="lorawan-location-devices-console"></a>

若要使用 AWS 管理主控台 設定並管理裝置的位置，請先登入主控台，然後前往 AWS IoT 主控台的 [https://console.aws.amazon.com/iot/home#/wireless/devices](https://console.aws.amazon.com/iot/home#/wireless/devices) (裝置) 中樞頁面。

**新增位置資訊**  
若要為裝置新增位置的位置資訊：

1. 在 **Device** (裝置) 中樞頁面，選擇 **Add wireless device** (新增無線裝置)。

1. 輸入無線裝置規格、裝置和服務設定檔，以及定義將資料路由到其他 AWS 服務 的 IoT 規則的目的地。如需詳細資訊，請參閱[將裝置加入 AWS IoT Core for LoRaWAN](lorawan-onboard-end-devices.md)。

1. 輸入位置資訊、選擇性啟用地理位置，並指定要用來路由訊息的位置資料目的地。
   + 

**位置資訊**  
使用經緯度座標以及高度座標 (選用) 來指定裝置的位置資料。位置資訊以 WGS84 座標系統為基礎。
   + 

**GeoLocation**  
如果您希望 AWS IoT Core for LoRaWAN 使用地理位置來計算裝置，請啟用定位功能。其使用第三方 GNSS 和 Wi-Fi 求解器即時識別您裝置的位置。

     若要輸入地理位置資訊，請選擇**啟用定位**，然後輸入地理位置訊框連接埠，以便將 GNSS 和 Wi-Fi 掃描資料傳送至 AWS IoT Core for LoRaWAN。您會看到預設填入的 FPort 供您參考。不過，您可以選擇 1 和 223 之間的任意值。
   + 

**位置資料目的地**  
選擇一個目的地來描述處理裝置位置資料並將資料轉送至 AWS IoT 的 AWS IoT Core for LoRaWAN 規則。僅使用此目的地來路由位置資料。該目的地必須與用於將裝置資料路由到其他 AWS 服務 的目的地不同。

**檢視裝置的位置組態**  
設定裝置的位置後，AWS IoT Core for LoRaWAN 會建立名為 `iotwireless.map` 的 Amazon Location 地圖。您可以在 **Position** (位置) 索引標籤中的裝置詳細資訊頁面上查看此地圖。根據您指定的位置座標或第三方求解器計算的位置，裝置位置會在地圖上顯示為標記。您可以放大或縮小標記，清楚檢視裝置在地圖上的位置。在 **Position** (位置) 索引標籤中的裝置詳細資訊頁面上，您也可看到準確度資訊、決定裝置位置的時間戳記，以及您指定的位置資料目的地。

**注意**  
如果尚未啟用 Amazon Location Service 地圖，您會看到一則訊息，指明您必須使用 Amazon Location Service 來存取地圖和檢視位置。使用 Amazon Location Service 地圖可能會向您的 AWS 帳戶 收取額外費用。如需詳細資訊，請參閱 [AWS IoT Core 定價](https://aws.amazon.com/iot-core/pricing/)。

地圖 `iotwireless.map` 作為地圖資料的來源，透過 `Get` API 操作存取，例如 [https://docs.aws.amazon.com/location-maps/latest/APIReference/API_GetMapTile.html](https://docs.aws.amazon.com/location-maps/latest/APIReference/API_GetMapTile.html)。如需 `Get` API 與地圖搭配使用的相關資訊，請參閱 [Amazon Location Service API reference](https://docs.aws.amazon.com/location-maps/latest/APIReference/Welcome.html) (《Amazon Location Service API 參考》)。

如需有關此地圖的其他詳細資訊，請前往 Amazon Location Service 主控台，選擇 **maps** (地圖)，然後選擇 [iotwireless.map](https://console.aws.amazon.com/location/maps/home#/describe/iotwireless.map)。如需詳細資訊，請參閱《Amazon Location Service 開發人員指南》中的[地圖](https://docs.aws.amazon.com/location/latest/developerguide/map-concepts.html)。

**更新裝置的位置組態**  
若要變更裝置的位置組態，請在裝置詳細資訊頁面中選擇 **Edit** (編輯)，然後更新位置資訊、任何地理位置設定以及目的地。

**注意**  
無法取得有關歷史位置資料的資訊。如果更新裝置的位置座標，即會覆寫先前報告的位置資料。更新位置之後，您會在 **Position** (位置) 索引標籤中的裝置詳細資訊頁面上看到新的位置資訊。時間戳記的變更表示它對應到裝置的最後已知位置。

## 使用 API 設定裝置位置
<a name="lorawan-location-devices-api"></a>

您可以使用 AWS IoT Wireless API 或 AWS CLI 來指定位置資訊、設定裝置位置，以及啟用選用的地理位置。

**重要**  
不再支援下列 API 動作：[ UpdatePosition](https://docs.aws.amazon.com/iot-wireless/2020-11-22/apireference/API_UpdatePosition.html)、[GetPosition](https://docs.aws.amazon.com/iot-wireless/2020-11-22/apireference/API_GetPosition.html)、[PutPositionConfiguration](https://docs.aws.amazon.com/iot-wireless/2020-11-22/apireference/API_PutPositionConfiguration.html)、[GetPositionConfiguration](https://docs.aws.amazon.com/iot-wireless/2020-11-22/apireference/API_GetPositionConfiguration.html) 以及 [ListPositionConfigurations](https://docs.aws.amazon.com/iot-wireless/2020-11-22/apireference/API_ListPositionConfigurations.html)。用於更新和擷取位置資訊的呼叫應改為使用 [GetResourcePosition](https://docs.aws.amazon.com/iot-wireless/2020-11-22/apireference/API_GetResourcePosition.html) 和 [UpdateResourcePosition](https://docs.aws.amazon.com/iot-wireless/2020-11-22/apireference/API_UpdateResourcePosition.html) API 操作。

### 新增位置資訊和組態
<a name="lorawan-location-devices-api-add"></a>

若要為指定的無線裝置新增位置資訊，請使用 [ UpdateResourcePosition](https://docs.aws.amazon.com/iot-wireless/2020-11-22/apireference/API_UpdateResourcePosition.html) API 操作或 [update-resource-position](https://docs.aws.amazon.com/cli/latest/reference/iotwireless/update-resource-position.html) CLI 命令來指定座標。指定 `WirelessDevice` 作為 `ResourceType`、要更新之無線閘道的 ID 作為 `ResourceIdentifier`，以及定位資訊作為 GeoJSON 承載。

```
aws iotwireless update-resource-position \ 
    --resource-type WirelessDevice \ 
    --resource-id {{"1ffd32c8-8130-4194-96df-622f072a315f"}} \ 
    --position [33.33, -33.33, 10.0]
```

下列顯示 `{{deviceposition.json}}` 檔案的內容。若要指定用於傳送地理位置資料的 FPort 值，請搭配 [CreateWirelessDevice](https://docs.aws.amazon.com/iot-wireless/2020-11-22/apireference/API_CreateWirelessDevice.html) 和 [UpdateWirelessDevice](https://docs.aws.amazon.com/iot-wireless/2020-11-22/apireference/API_UpdateWirelessDevice.html) API 操作來使用[定位](https://docs.aws.amazon.com/iot-wireless/2020-11-22/apireference/API_Positioning.html)物件。

**deviceposition.json 的內容**

```
{ 
     "type": "Point",
     "coordinates": {{[33.3318, -22.2155, 13.123]}},
     "properties": {
          "verticalAccuracy": 707,
          "horizontalAccuracy": 
          "timestamp": {{"2018-11-30T18:35:24Z"}}
      }
}
```

執行這個命令不會產生任何輸出。若要查看您指定的位置資訊，請使用 `GetResourcePosition` API 操作。

### 取得位置資訊和組態
<a name="lorawan-location-devices-api-get"></a>

若要取得指定無線裝置的位置資訊，請使用[ GetResourcePosition](https://docs.aws.amazon.com/iot-wireless/2020-11-22/apireference/API_GetResourcePosition.html) API 或 [get-resource-position](https://docs.aws.amazon.com/cli/latest/reference/iotwireless/get-resource-position.html) CLI 命令。指定 `WirelessDevice` 作為 `resourceType`，並提供作為 `resourceIdentifier` 的無線裝置 ID。

```
aws iotwireless get-resource-position \ 
    --resource-type WirelessDevice \ 
    --resource-id "{{1ffd32c8-8130-4194-96df-622f072a315f}}"
```

執行這個命令會將無線裝置位置資訊顯示為 GeoJSON 承載。您將看到的資訊包含位置坐標、位置類型以及若干屬性，其可能包括對應於裝置最後已知位置的準確度及時間戳記資訊。

```
{ 
     "type": "Point",
     "coordinates": {{[33.3318, -22.2155, 13.123]}},
     "properties": {
          "verticalAccuracy": 707,
          "horizontalAccuracy": 389,
          "horizontalConfidenceLevel": 0.68,
          "verticalConfidenceLevel": 0.68,
          "timestamp": {{"2018-11-30T18:35:24Z"}}
      }
}
```