LoginSignup
0
0

More than 1 year has passed since last update.

Python3: AWS IOT の Device Shadow (botocore)

Posted at

こちらのプログラムを Botocore で書いてみました。
Python3: 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/python3
# coding: utf-8
#
#   report_off_thing.py
#
#                   Sep/29/2021
# ------------------------------------------------------------------
import json
import botocore.session

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

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

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

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

report_on_thing.py
#! /usr/bin/python3
# coding: utf-8
#
#   report_off_thing.py
#
#                   Sep/29/2021
# ------------------------------------------------------------------
import sys
import json
import botocore.session
# ------------------------------------------------------------------
url='https://abcd6goq68zt4o-ats.iot.ap-northeast-1.amazonaws.com'
thing='sample_sep26'
#
session = botocore.session.get_session()
client = session.create_client('iot-data', endpoint_url=url)

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

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

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

command_off_thing.py
#! /usr/bin/python3
# coding: utf-8
#
#   command_off_thing.py
#
#                   Sep/29/2021
# ------------------------------------------------------------------
import sys
import json
import botocore.session

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

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

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

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

command_on_thing.py
#! /usr/bin/python3
# coding: utf-8
#
#   command_on_thing.py
#
#                   Sep/29/2021
# ------------------------------------------------------------------
import json
import botocore.session

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

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

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

Shadow の状態を取得

get_thing_shadow.py
#! /usr/bin/python3
# coding: utf-8
#
#   get_thing_shadow.py
#
#                   Sep/28/2021
# ------------------------------------------------------------------
import json
import botocore.session

# ------------------------------------------------------------------
url='https://abcd6goq68zt4o-ats.iot.ap-northeast-1.amazonaws.com'
thing='sample_sep26'
#
session = botocore.session.get_session()
client = session.create_client('iot-data', endpoint_url=url)
res = client.get_thing_shadow(thingName=thing)

bb=res['payload'].read()
print(bb.decode())
# ------------------------------------------------------------------
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