1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AWS IOT の shadow の値を読む方法

Last updated at Posted at 2021-09-11

AWS IOT の shadow の値を読む方法です。

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

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

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

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

もの

violet

##Curl##

curl_get.sh
#
#	curl_get.sh
#
#					Sep/11/2021
#
HOST="https://abcd6goq68zt4o-ats.iot.ap-northeast-1.amazonaws.com:8443"
THINGNAME="violet"
URL=$HOST"/things/"$THINGNAME"/shadow"
#
curl --tlsv1.2 \
	--cert ../certs/device.pem.crt \
	--key ../certs/private.pem.key \
	-X GET $URL
#

##HTTPie##

http_get.sh
#
#	http_get.sh
#
#					Sep/11/2021
#
HOST="https://abcd6goq68zt4o-ats.iot.ap-northeast-1.amazonaws.com:8443"
THINGNAME="violet"
URL=$HOST"/things/"$THINGNAME"/shadow"
#
http --cert ../certs/device.pem.crt \
	--cert-key ../certs/private.pem.key \
	$URL
#

##Python3##

get_value.py
#! /usr/bin/python
#
#	get_value.py
#
#					Sep/11/2021
# ------------------------------------------------------------------
import requests
import sys

host = "https://abcd6goq68zt4o-ats.iot.ap-northeast-1.amazonaws.com:8443"
thingname="violet"
url = host + "/things/" + thingname + "/shadow"
cert = "../certs/device.pem.crt"
key = "../certs/private.pem.key"
#
publish = requests.request('GET', url, cert=[cert, key])
#
sys.stderr.write("Response status: " + str(publish.status_code) + "\n")
if publish.status_code == 200:
	print(publish.text)
#
# ------------------------------------------------------------------

##mosquitto_sub##

get_mosquitto.sh
#! /bin/bash
#
#	get_mosquitto.sh
#
#						Sep/11/2021
cafile="../certs/Amazon-root-CA-1.pem"
certfile="../certs/device.pem.crt"
keyfile="../certs/private.pem.key"
#
host="abcd6goq68zt4o-ats.iot.ap-northeast-1.amazonaws.com"

port=8883
device="violet"
topic="\$aws/things/"$device"/shadow/update/documents"
qos=1
#
mosquitto_sub -d --cafile $cafile --cert $certfile \
	--key $keyfile -q $qos -h $host -p $port \
	-t $topic

##Paho##

get_paho.py
#!/usr/bin/python
#
#	get_paho.py
#
#					Sep/11/2021
# ------------------------------------------------------------------
import paho.mqtt.client as mqtt
import ssl

host = 'abcd6goq68zt4o-ats.iot.ap-northeast-1.amazonaws.com'
port = 8883
cacert = '../certs/Amazon-root-CA-1.pem'
clientCert = '../certs/device.pem.crt'
clientKey = '../certs/private.pem.key'

thing="violet"
topic = "$aws/things/" + thing + "/shadow/update/documents"

# ------------------------------------------------------------------
def on_connect(client, userdata, flags, respons_code):
	print('status {0}'.format(respons_code))
	client.subscribe(topic)

# ------------------------------------------------------------------
def on_message(client, userdata, msg):
	print(msg.topic + ' ' + str(msg.payload))

# ------------------------------------------------------------------
client = mqtt.Client(client_id= "python",protocol=mqtt.MQTTv311)
	### SSL
client.tls_set(cacert,
	certfile = clientCert,
	keyfile = clientKey,
	tls_version = ssl.PROTOCOL_TLSv1_2)
client.tls_insecure_set(True)

client.on_connect = on_connect
client.on_message = on_message

client.connect(host, port=port, keepalive=6)
client.loop_forever()
#
# ------------------------------------------------------------------

##Boto3##

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

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

res = iot_data.get_thing_shadow(thingName=thing)

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

実行結果

$ ./get_thing_shadow.py 
{"state":{"desired":{"power":"on"},"reported":{"power":"off"},"delta":{"power":"on"}},"metadata":{"desired":{"power":{"timestamp":1632782252}},"reported":{"power":{"timestamp":1632782577}}},"version":5,"timestamp":1632786613}
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?