[メモ] Raspberry PiをAWS IoTに接続する (とりあえずGUIで)

# Raspberry Pi基本セットアップ
- Model : Raspberry Pi 3 Model B+
- OS : RASPBIAN Buster LITE (CUIのみ版)

[以下を参考にセットアップ]
https://hirazakura.hatenablog.com/entry/raspberrypi/setup/first
https://hirazakura.hatenablog.com/entry/raspberrypi/setup/last
https://docs.aws.amazon.com/greengrass/latest/developerguide/setup-filter.rpi.html

# デバイスの登録
デバイスをAWS IoT Coreに登録する。 (デバイスの登録はIoTへの接続に必須ではないが、管理や検索が便利になる。)

IoT Coreのコンソールから「開始方法」をクリックする。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/345960/6e459c07-acc9-6974-0af2-f6f837978e65.png)

左ペインの「管理」 > 「モノ」 に移動し、「モノの登録」をクリックする。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/345960/c8f79be1-2bf7-807d-6457-b1affedce5fb.png)

「単一のモノを作成する」をクリックする。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/345960/c6348a2a-0465-0783-c68f-7805f8913fc3.png)

デバイス追加画面で以下を設定する。

- 名前 ： 今回は"raspi-test01" -> デバイスを識別する名前となる。
- タイプ : 新規に"test-type"タイプを作成 -> オプションだが、属性による検索ができるようになる
    - 属性として試しに"Model"を追加
- グループ : 新規に"test-group"を作成
- 検索可能なモノの属性の設定
    - Model: raspberry-pi-3-model-b-plus
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/345960/181971f7-7847-46f9-9df0-0d671fb92e17.png)

証明書の追加画面で「1-Click 証明書作成」を選択する。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/345960/bc400a8d-8448-e27e-f9ca-1c83734dd416.png)

証明書とキーをダウンロードし、ルートCA証明書もリンク先からダウンロードしておく。またルートCAの「有効化」をクリック。全て完了したら「完了」をクリックする。。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/345960/48ab6fa8-317e-a203-f3d6-841dde1e9258.png)

Thingが作成されている。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/345960/64fbbcba-0c30-b455-71ed-aa9ce9f9de96.png)

# ポリシーの作成
デバイスに対して付与する権限をポリシーとして作成する。

左ペインの「安全性」 > 「ポリシー」 に移動し、「ポリシーの作成」をクリックする。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/345960/817aaa34-bab7-b94c-3537-8739d12093f3.png)

ポリシーを設定し、「作成」をクリックする。

- 名前 ： 今回は"test-policy"
- ステートメント
    - テスト用のためアクション:iot*, リソースARN:*
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/345960/7125f340-125b-927d-08f5-b1ab3bfc611c.png)

ポリシーが作成される。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/345960/0c70f888-9058-318c-c000-728bb96c1ad8.png)

ポリシーを証明書にアタッチする。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/345960/8f1b2254-8427-0827-2b64-ff7f2578d35f.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/345960/f9d89718-28c8-d2f0-63ae-e50f1e4a2d7c.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/345960/ea48ed2f-ddb3-6f3c-8b79-4a31a527340b.png)

# raspberrypiの接続

## ツールのインストール
###gitのインストール
```bash
$ sudo apt-get install git
```

### python環境管理用にpyenv/pyenv-viurtualenvのインストール (必須ではないが今後Python環境管理するときのために)
$ pyenv/pyenv-viurtualenvのインストール

```bash
$ curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
```

PATHを通す

```
$ vi .bashrc
以下を追加
-----
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
-----
```

必要なパッケージをインストール

```
$ sudo apt install libffi-dev libbz2-dev libssl-dev libreadline-dev libsqlite3-dev
```

### python 3.7インストール

```bash
$ pyenv install --list
~snip~
 3.7.2
 3.7.3
 3.7.4
 3.8-dev
 3.9-dev
~snip~
$ pyenv install 3.7.4
$ pyenv global 3.7.4
$ pyenv version
3.7.4 (set by /home/pi/.pyenv/version)
```

### AWS IoT Python SDKインストール

```
$ pip install --upgrade pip
$ pip install AWSIoTPythonSDK
$ git clone https://github.com/aws/aws-iot-device-sdk-python.git
$ cd aws-iot-device-sdk-python
$ ls -l
total 84
drwxr-xr-x 4 pi pi  4096 Aug 25 17:30 AWSIoTPythonSDK
-rw-r--r-- 1 pi pi  4681 Aug 25 17:30 CHANGELOG.rst
-rwxr-xr-x 1 pi pi  9245 Aug 25 17:30 LICENSE.txt
-rw-r--r-- 1 pi pi   149 Aug 25 17:30 MANIFEST.in
-rwxr-xr-x 1 pi pi   208 Aug 25 17:30 NOTICE.txt
-rwxr-xr-x 1 pi pi 38410 Aug 25 17:30 README.rst
drwxr-xr-x 7 pi pi  4096 Aug 25 17:30 samples
-rw-r--r-- 1 pi pi    41 Aug 25 17:30 setup.cfg
-rw-r--r-- 1 pi pi  1550 Aug 25 17:30 setup.py
```

## 証明書の配置

```
mkdir cert
SCP等で証明書をコピーしておく
$ ls　cert
xxxxxxxxxx-certificate.pem.crt  xxxxxxxx-private.pem.key  AmazonRootCA1.pem
```

# サンプルプログラムで接続

## Pub/Subテスト
サンプルプログラムのディレクトリに移動

```
cd aws-iot-device-sdk-python/samples/
$ ls -l
total 20
drwxr-xr-x 2 pi pi 4096 Aug 25 17:30 basicPubSub
drwxr-xr-x 2 pi pi 4096 Aug 25 17:30 basicShadow
drwxr-xr-x 2 pi pi 4096 Aug 25 17:30 greengrass
drwxr-xr-x 2 pi pi 4096 Aug 25 17:30 jobs
drwxr-xr-x 2 pi pi 4096 Aug 25 17:30 ThingShadowEcho
``` 

basicPubSubでPub/Subテスト

```
$ cd basicPubsub
$ python basicPubSub.py \
  --endpoint xxxxxxxxxxxxxx-ats.iot.ap-northeast-1.amazonaws.com \
  --rootCA ~/cert/AmazonRootCA1.pem \
  --cert ~/cert/18734fd2c1-certificate.pem.crt  \
  --key ~/cert/18734fd2c1-private.pem.key 
```

```
2019-08-25 17:49:43,941 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Initializing MQTT layer...
2019-08-25 17:49:43,942 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Registering internal event callbacks to MQTT layer...
2019-08-25 17:49:43,943 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - MqttCore initialized
2019-08-25 17:49:43,943 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Client id: basicPubSub
2019-08-25 17:49:43,943 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Protocol version: MQTTv3.1.1
2019-08-25 17:49:43,944 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Authentication type: TLSv1.2 certificate based Mutual Auth.
2019-08-25 17:49:43,944 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Configuring endpoint...
2019-08-25 17:49:43,945 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Configuring certificates...
2019-08-25 17:49:43,945 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Configuring reconnect back off timing...
2019-08-25 17:49:43,946 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Base quiet time: 1.000000 sec
2019-08-25 17:49:43,946 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Max quiet time: 32.000000 sec
2019-08-25 17:49:43,946 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Stable connection time: 20.000000 sec
2019-08-25 17:49:43,947 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Configuring offline requests queueing: max queue size: -1
2019-08-25 17:49:43,947 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Configuring offline requests queue draining interval: 0.500000 sec
2019-08-25 17:49:43,948 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Configuring connect/disconnect time out: 10.000000 sec
2019-08-25 17:49:43,948 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Configuring MQTT operation time out: 5.000000 sec
2019-08-25 17:49:43,948 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Performing sync connect...
2019-08-25 17:49:43,948 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Performing async connect...
2019-08-25 17:49:43,949 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Keep-alive: 600.000000 sec
2019-08-25 17:49:43,949 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Event consuming thread started
2019-08-25 17:49:43,950 - AWSIoTPythonSDK.core.protocol.mqtt_core - DEBUG - Passing in general notification callbacks to internal client...
2019-08-25 17:49:43,950 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Filling in fixed event callbacks: CONNACK, DISCONNECT, MESSAGE
2019-08-25 17:49:44,145 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Starting network I/O thread...
2019-08-25 17:49:44,230 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Produced [connack] event
2019-08-25 17:49:44,231 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Dispatching [connack] event
2019-08-25 17:49:44,232 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - No need for recovery
2019-08-25 17:49:44,232 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Invoking custom event callback...
2019-08-25 17:49:44,233 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Performing sync subscribe...
2019-08-25 17:49:44,234 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Adding a new subscription record: sdk/test/Python qos: 1
2019-08-25 17:49:44,234 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Filling in custom suback event callback...
2019-08-25 17:49:44,268 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Produced [suback] event
2019-08-25 17:49:44,269 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Dispatching [suback] event
2019-08-25 17:49:44,269 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Invoking custom event callback...
2019-08-25 17:49:44,270 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - This custom event callback is for pub/sub/unsub, removing it after invocation...
2019-08-25 17:49:46,273 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Performing sync publish...
2019-08-25 17:49:46,274 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Filling in custom puback (QoS>0) event callback...
2019-08-25 17:49:46,336 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Produced [puback] event
2019-08-25 17:49:46,337 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Dispatching [puback] event
2019-08-25 17:49:46,338 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Invoking custom event callback...
2019-08-25 17:49:46,339 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - This custom event callback is for pub/sub/unsub, removing it after invocation...
2019-08-25 17:49:46,363 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Produced [message] event
2019-08-25 17:49:46,364 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Dispatching [message] event
Received a new message: 
b'{"message": "Hello World!", "sequence": 0}'
from topic: 
sdk/test/Python
--------------


2019-08-25 17:49:46,366 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Invoking custom event callback...
2019-08-25 17:49:47,341 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Performing sync publish...
2019-08-25 17:49:47,342 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Filling in custom puback (QoS>0) event callback...
2019-08-25 17:49:47,382 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Produced [puback] event
2019-08-25 17:49:47,383 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Dispatching [puback] event
2019-08-25 17:49:47,384 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Invoking custom event callback...
2019-08-25 17:49:47,385 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - This custom event callback is for pub/sub/unsub, removing it after invocation...
2019-08-25 17:49:47,410 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Produced [message] event
2019-08-25 17:49:47,411 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Dispatching [message] event
Received a new message: 
b'{"message": "Hello World!", "sequence": 1}'
from topic: 
sdk/test/Python
--------------


2019-08-25 17:49:47,413 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Invoking custom event callback...
2019-08-25 17:49:48,387 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Performing sync publish...
2019-08-25 17:49:48,389 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Filling in custom puback (QoS>0) event callback...
2019-08-25 17:49:48,416 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Produced [puback] event
2019-08-25 17:49:48,417 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Dispatching [puback] event
2019-08-25 17:49:48,418 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Invoking custom event callback...
2019-08-25 17:49:48,418 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - This custom event callback is for pub/sub/unsub, removing it after invocation...
2019-08-25 17:49:48,442 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Produced [message] event
2019-08-25 17:49:48,443 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Dispatching [message] event
Received a new message: 
b'{"message": "Hello World!", "sequence": 2}'
from topic: 
sdk/test/Python
--------------

~snip~

```

- MQTT 3.1.1, TLS1.2で通信している
- sdk/test/PythonというTopicをSubscribeし、またメッセージを繰り返しPublishしている


AWS IoTダッシュボードからも確認
<img width="1396" alt="image.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/345960/9c808c1c-34e3-df89-eb84-1057df021264.png">


## デバイスシャドウのdesired更新テスト
basicShadowUpdaterでバイスシャドウのdesiredを更新するテスト

```
$ cd cd ../basicShadow
$ python basicShadowUpdater.py \
  --endpoint xxxxxxxxxxxxxx-ats.iot.ap-northeast-1.amazonaws.com \
  --rootCA ~/cert/AmazonRootCA1.pem \
  --cert ~/cert/18734fd2c1-certificate.pem.crt  \
  --key ~/cert/18734fd2c1-private.pem.key \
  --thingName raspi-test01
```

```
2019-08-25 19:18:16,658 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Initializing MQTT layer...
2019-08-25 19:18:16,659 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Registering internal event callbacks to MQTT layer...
2019-08-25 19:18:16,660 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - MqttCore initialized
2019-08-25 19:18:16,660 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Client id: basicShadowUpdater
2019-08-25 19:18:16,660 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Protocol version: MQTTv3.1.1
2019-08-25 19:18:16,661 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Authentication type: TLSv1.2 certificate based Mutual Auth.
2019-08-25 19:18:16,661 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Configuring offline requests queueing: max queue size: 0
2019-08-25 19:18:16,662 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Configuring offline requests queue draining interval: 0.100000 sec
2019-08-25 19:18:16,663 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Configuring endpoint...
2019-08-25 19:18:16,663 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Configuring certificates...
2019-08-25 19:18:16,664 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Configuring reconnect back off timing...
2019-08-25 19:18:16,664 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Base quiet time: 1.000000 sec
2019-08-25 19:18:16,664 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Max quiet time: 32.000000 sec
2019-08-25 19:18:16,664 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Stable connection time: 20.000000 sec
2019-08-25 19:18:16,665 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Configuring connect/disconnect time out: 10.000000 sec
2019-08-25 19:18:16,665 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Configuring MQTT operation time out: 5.000000 sec
2019-08-25 19:18:16,665 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Performing sync connect...
2019-08-25 19:18:16,666 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Performing async connect...
2019-08-25 19:18:16,666 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Keep-alive: 600.000000 sec
2019-08-25 19:18:16,667 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Event consuming thread started
2019-08-25 19:18:16,667 - AWSIoTPythonSDK.core.protocol.mqtt_core - DEBUG - Passing in general notification callbacks to internal client...
2019-08-25 19:18:16,668 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Filling in fixed event callbacks: CONNACK, DISCONNECT, MESSAGE
2019-08-25 19:18:16,776 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Starting network I/O thread...
2019-08-25 19:18:16,857 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Produced [connack] event
2019-08-25 19:18:16,857 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Dispatching [connack] event
2019-08-25 19:18:16,858 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - No need for recovery
2019-08-25 19:18:16,859 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Invoking custom event callback...
2019-08-25 19:18:16,860 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Performing sync subscribe...
2019-08-25 19:18:16,861 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Adding a new subscription record: $aws/things/raspi-test01/shadow/delete/accepted qos: 0
2019-08-25 19:18:16,861 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Filling in custom suback event callback...
2019-08-25 19:18:16,894 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Produced [suback] event
2019-08-25 19:18:16,894 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Dispatching [suback] event
2019-08-25 19:18:16,895 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Invoking custom event callback...
2019-08-25 19:18:16,895 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - This custom event callback is for pub/sub/unsub, removing it after invocation...
2019-08-25 19:18:16,895 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Performing sync subscribe...
2019-08-25 19:18:16,896 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Adding a new subscription record: $aws/things/raspi-test01/shadow/delete/rejected qos: 0
2019-08-25 19:18:16,896 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Filling in custom suback event callback...
2019-08-25 19:18:16,926 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Produced [suback] event
2019-08-25 19:18:16,927 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Dispatching [suback] event
2019-08-25 19:18:16,927 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Invoking custom event callback...
2019-08-25 19:18:16,927 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - This custom event callback is for pub/sub/unsub, removing it after invocation...
2019-08-25 19:18:18,930 - AWSIoTPythonSDK.core.shadow.deviceShadow - INFO - Subscribed to delete accepted/rejected topics for deviceShadow: raspi-test01
2019-08-25 19:18:18,931 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Performing sync publish...
2019-08-25 19:18:18,933 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Produced [puback] event
2019-08-25 19:18:18,934 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Dispatching [puback] event
2019-08-25 19:18:18,936 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Performing sync subscribe...
2019-08-25 19:18:18,937 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Adding a new subscription record: $aws/things/raspi-test01/shadow/update/accepted qos: 0
2019-08-25 19:18:18,938 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Filling in custom suback event callback...
2019-08-25 19:18:18,959 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Produced [message] event
2019-08-25 19:18:18,960 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Dispatching [message] event
2019-08-25 19:18:18,962 - AWSIoTPythonSDK.core.shadow.deviceShadow - DEBUG - shadow message clientToken: f265b705-09f8-4d8c-af39-db10af025fd3
2019-08-25 19:18:18,962 - AWSIoTPythonSDK.core.shadow.deviceShadow - DEBUG - Token is in the pool. Type: accepted
-----------------------
2019-08-25 19:18:18,964 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Invoking custom event callback...
Delete request with token: f265b705-09f8-4d8c-af39-db10af025fd3 accepted!
-----------------------


2019-08-25 19:18:18,992 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Produced [suback] event
2019-08-25 19:18:18,992 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Dispatching [suback] event
2019-08-25 19:18:18,993 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Invoking custom event callback...
2019-08-25 19:18:18,994 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - This custom event callback is for pub/sub/unsub, removing it after invocation...
2019-08-25 19:18:18,994 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Performing sync subscribe...
2019-08-25 19:18:18,995 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Adding a new subscription record: $aws/things/raspi-test01/shadow/update/rejected qos: 0
2019-08-25 19:18:18,996 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Filling in custom suback event callback...
2019-08-25 19:18:19,041 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Produced [suback] event
2019-08-25 19:18:19,042 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Dispatching [suback] event
2019-08-25 19:18:19,043 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Invoking custom event callback...
2019-08-25 19:18:19,044 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - This custom event callback is for pub/sub/unsub, removing it after invocation...
2019-08-25 19:18:21,046 - AWSIoTPythonSDK.core.shadow.deviceShadow - INFO - Subscribed to update accepted/rejected topics for deviceShadow: raspi-test01
2019-08-25 19:18:21,047 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Performing sync publish...
2019-08-25 19:18:21,049 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Produced [puback] event
2019-08-25 19:18:21,051 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Dispatching [puback] event
2019-08-25 19:18:21,083 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Produced [message] event
2019-08-25 19:18:21,085 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Dispatching [message] event
2019-08-25 19:18:21,087 - AWSIoTPythonSDK.core.shadow.deviceShadow - DEBUG - shadow message clientToken: f2880500-93f6-423c-afdd-f85625daed35
2019-08-25 19:18:21,088 - AWSIoTPythonSDK.core.shadow.deviceShadow - DEBUG - Token is in the pool. Type: accepted
-----------------------
2019-08-25 19:18:21,091 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Invoking custom event callback...
Update request with token: f2880500-93f6-423c-afdd-f85625daed35 accepted!
property: 0
-----------------------


2019-08-25 19:18:22,054 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Performing sync publish...
2019-08-25 19:18:22,056 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Produced [puback] event
2019-08-25 19:18:22,057 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Dispatching [puback] event
2019-08-25 19:18:22,084 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Produced [message] event
2019-08-25 19:18:22,085 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Dispatching [message] event
2019-08-25 19:18:22,087 - AWSIoTPythonSDK.core.shadow.deviceShadow - DEBUG - shadow message clientToken: 7dafa694-051b-4c84-b60f-c1d83db416dc
2019-08-25 19:18:22,088 - AWSIoTPythonSDK.core.shadow.deviceShadow - DEBUG - Token is in the pool. Type: accepted
-----------------------
2019-08-25 19:18:22,091 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Invoking custom event callback...
Update request with token: 7dafa694-051b-4c84-b60f-c1d83db416dc accepted!
property: 1
-----------------------


2019-08-25 19:18:23,060 - AWSIoTPythonSDK.core.protocol.mqtt_core - INFO - Performing sync publish...
2019-08-25 19:18:23,062 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Produced [puback] event
2019-08-25 19:18:23,064 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Dispatching [puback] event
2019-08-25 19:18:23,096 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Produced [message] event
2019-08-25 19:18:23,098 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Dispatching [message] event
2019-08-25 19:18:23,100 - AWSIoTPythonSDK.core.shadow.deviceShadow - DEBUG - shadow message clientToken: 7619fe09-a6dd-4de5-ac3b-2485f55c49ef
2019-08-25 19:18:23,101 - AWSIoTPythonSDK.core.shadow.deviceShadow - DEBUG - Token is in the pool. Type: accepted
-----------------------
2019-08-25 19:18:23,104 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Invoking custom event callback...
Update request with token: 7619fe09-a6dd-4de5-ac3b-2485f55c49ef accepted!
property: 2
-----------------------

~snip~

```

シャドウステータスのdesiredが更新されている。
<img width="1491" alt="image.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/345960/b5fce170-4b35-c7db-9fed-2241cb75b724.png">



## デバイスシャドウのdelta検知テスト

basicShadowDeltaListener.pyでdelta(desiredとの差)を検知するテスト。
basicShadowUpdater.pyでdesiredが更新されると、delta messagesが受信される。

```
$ python basicShadowDeltaListener.py \
  --endpoint xxxxxxxxxxxxxx-ats.iot.ap-northeast-1.amazonaws.com \
  --rootCA ~/cert/AmazonRootCA1.pem \
  --cert ~/cert/18734fd2c1-certificate.pem.crt  \
  --key ~/cert/18734fd2c1-private.pem.key \
  --thingName raspi-test01

起動した状態で別のターミナルで先ほどのbasicShadowUpdater.pyを実行しdesiredを更新する。

$ python basicShadowUpdater.py \
  --endpoint xxxxxxxxxxxxxx-ats.iot.ap-northeast-1.amazonaws.com \
  --rootCA ~/cert/AmazonRootCA1.pem \
  --cert ~/cert/18734fd2c1-certificate.pem.crt  \
  --key ~/cert/18734fd2c1-private.pem.key \
  --thingName raspi-test01
```

```:basicShadowDeltaListenerの出力
++++++++DELTA++++++++++
property: 0
2019-08-25 19:29:54,846 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Invoking custom event callback...
version: 142
+++++++++++++++++++++++


2019-08-25 19:29:55,815 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Produced [message] event
2019-08-25 19:29:55,816 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Dispatching [message] event
2019-08-25 19:29:55,818 - AWSIoTPythonSDK.core.protocol.internal.clients - DEBUG - Invoking custom event callback...
delta/raspi-test01
++++++++DELTA++++++++++
property: 1
version: 143
+++++++++++++++++++++++


2019-08-25 19:29:56,818 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Produced [message] event
2019-08-25 19:29:56,818 - AWSIoTPythonSDK.core.protocol.internal.workers - DEBUG - Dispatching [message] event
delta/raspi-test0
++++++++DELTA++++++++++
property: 2
version: 144
+++++++++++++++++++++++

```
deltaが検知されている。

# 参考
https://dev.classmethod.jp/hardware/raspberrypi/aws-iot-device-sdk-for-python-raspberrypi-aws-iot/