LoginSignup
0
0

More than 1 year has passed since last update.

Python3: AWS IOT の Device Shadow

Last updated at Posted at 2021-09-28

こちらのスクリプトのいくつかを、Boto3 で書いてみました。
AWS IOT の Device Shadow

エンドポイントは次のコマンドで調べます。

aws iot describe-endpoint --endpoint-type iot:Data-ATS

エンドポイント が次の場合のサンプルです。

https://abcd6goq68zt4o-ats.iot.ap-northeast-1.amazonaws.com

もの

sample_sep26

デバイスの状態が OFF であることを通知

report_off_thing.py
#! /usr/bin/python
# coding: utf-8
#
#   report_off_thing.py
#
#                   Sep/28/2021
# ------------------------------------------------------------------
import json
import boto3

# ------------------------------------------------------------------
url='https://abcd6goq68zt4o-ats.iot.ap-northeast-1.amazonaws.com'
thing='sample_sep26'
#
iot_data = boto3.client('iot-data', endpoint_url=url)

shadowDoc = {'state':{'reported':{'led':'off'}}}
payload = json.dumps(shadowDoc)

res = iot_data.update_thing_shadow(thingName=thing, payload=payload)
#
# ------------------------------------------------------------------

デバイスの状態が ON であることを通知

report_on_thing.py
#! /usr/bin/python
# coding: utf-8
#
#   report_on_thing.py
#
#                   Sep/28/2021
# ------------------------------------------------------------------
import json
import boto3

# ------------------------------------------------------------------
url='https://abcd6goq68zt4o-ats.iot.ap-northeast-1.amazonaws.com'
thing='sample_sep26'
#
iot_data = boto3.client('iot-data', endpoint_url=url)

shadowDoc = {'state':{'reported':{'led':'on'}}}
payload = json.dumps(shadowDoc)

res = iot_data.update_thing_shadow(thingName=thing, payload=payload)
#
# ------------------------------------------------------------------

デバイスを OFF にするように指令

command_off_thing.py
#! /usr/bin/python
# coding: utf-8
#
#   command_off_thing.py
#
#                   Sep/28/2021
# ------------------------------------------------------------------
import json
import boto3

# ------------------------------------------------------------------
url='https://abcd6goq68zt4o-ats.iot.ap-northeast-1.amazonaws.com'
thing='sample_sep26'
#
iot_data = boto3.client('iot-data', endpoint_url=url)

shadowDoc = {'state':{'desired':{'led':'off'}}}
payload = json.dumps(shadowDoc)

res = iot_data.update_thing_shadow(thingName=thing, payload=payload)
#
# ------------------------------------------------------------------

デバイスを ON にするように指令

command_on_thing.py
#! /usr/bin/python
# coding: utf-8
#
#   command_on_thing.py
#
#                   Sep/28/2021
# ------------------------------------------------------------------
import json
import boto3

# ------------------------------------------------------------------
url='https://abcd6goq68zt4o-ats.iot.ap-northeast-1.amazonaws.com'
thing='sample_sep26'
#
iot_data = boto3.client('iot-data', endpoint_url=url)

shadowDoc = {'state':{'desired':{'led':'on'}}}
payload = json.dumps(shadowDoc)

res = iot_data.update_thing_shadow(thingName=thing, payload=payload)
#
# ------------------------------------------------------------------

Shadow の状態を取得

get_thing_shadow.py
#! /usr/bin/python
# coding: utf-8
#
#   get_thing_shadow.py
#
#                   Sep/28/2021
# ------------------------------------------------------------------
import json
import boto3

# ------------------------------------------------------------------
url='https://abcd6goq68zt4o-ats.iot.ap-northeast-1.amazonaws.com'
thing='sample_sep26'
#
iot_data = boto3.client('iot-data', endpoint_url=url)

res = iot_data.get_thing_shadow(thingName=thing)

bb=res['payload'].read()
print(bb.decode())
# ------------------------------------------------------------------

次のバージョンで確認しました。

$ python
Python 3.9.7 (default, Aug 31 2021, 13:28:12) 
[GCC 11.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import boto3
>>> boto3.__version__
'1.18.34'
>>> 
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0