0
2

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 5 years have passed since last update.

EC2(ubuntu)のmosquitto clientを使ってAWS IoTにPub/Subする

Last updated at Posted at 2017-11-25

EC2インスタンス(ubuntu16.04)にmosquitto-clientsをインストールし、AWS IoTに向かってPublish/Subscribeをしました。

前提条件は、
・AWS IoTのconnect_device_package(デバイス証明書や秘密鍵を含んでいる)を取得していること。(AWS IoTコンソールの接続ウィザードからデバイスの設定をする)
・AWS IoT ルートCAを取得していること。(connect_device_packageのstart.sh内に記載されている)

$ curl https://www.symantec.com/content/en/us/enterprise/verisign/roots/VeriSign-Class%203-Public-Primary-Certification-Authority-G5.pem > root-CA.crt

です。

mosquitto-clientsのインストール

$ su
Password:
# apt-get install mosquitto-clients
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package mosquitto-clients
# apt-get update
(...log...)
# apt-get install mosquitto-clients
(...log...)

AWS IoTに向かってSubscribe

$ mosquitto_sub --cafile root-CA.crt --cert X.cert.pem --key X.private.key -h "${endpoint}" -p 8883 -q 1 -d -t mq/test
Client mosqsub/2897-ip-172-31- sending CONNECT
Client mosqsub/2897-ip-172-31- received CONNACK
Client mosqsub/2897-ip-172-31- sending SUBSCRIBE (Mid: 1, Topic: mq/test, QoS: 1)
Client mosqsub/2897-ip-172-31- received SUBACK
Subscribed (mid: 1): 1

※endpointは、aws cliが設定済であれば以下のコマンドで取得可能。(コンソールからも確認可能)

$ aws iot describe-endpoint

AWS IoTに向かってPublish

Subscribeのターミナルとは別のターミナルを立ち上げてpublishします。

$ mosquitto_pub --cafile root-CA.crt --cert X.cert.pem --key X.private.key -h "${endpoint}" -p 8883 -q 1 -d -t mq/test -m "XYZ"
Client mosquitto_pub sending CONNECT
Client mosquitto_pub received CONNACK
Client mosquitto_pub sending PUBLISH (d0, q1, r0, m1, 'mq/test', ... (3 bytes))
Client mosquitto_pub received PUBACK (Mid: 1)
Client mosquitto_pub sending DISCONNECT

Subscribeを実行したコンソールを確認

Client mosqsub/2897-ip-172-31- received PUBLISH (d0, q1, r0, m1, 'mq/test', ... (3 bytes))
Client mosqsub/2897-ip-172-31- sending PUBACK (Mid: 1)
XYZ
0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?