0
0

More than 1 year has passed since last update.

IOT: MQTT クライアントのサンプル

Last updated at Posted at 2021-10-05

IOTのサンプルの仕様を作ってみました。
実装は、別ページで紹介します。

ブローカーを

example.com

トピックを

example/test01

とします。
認証は使いませんから、パブリック MQTT ブローカーでテストができます。

クライアントは、ブローカーに subcribe して MQTT の内容により、次の動作をします。

1) led を ON にする。
 
次の payload を受け取ると、

{"state": {"desired" : {"led" : "on"}}}

LED を ON にし、

次の payload を publish します。

{"state": {"reported" : {"led" : "on"}}}

2) led を OFF にする。
 
次の payload を受け取ると、

{"state": {"desired" : {"led" : "off"}}}

LED を ON にし、

次の payload を publish します。

{"state": {"reported" : {"led" : "off"}}}

検証スクリプト

publish_on.sh
#
#   publish_on.sh
#
BROKER="example.com"
TOPIC="example/test01"
message='{"state": {"desired" : {"led" : "on"}}}'
#
mosquitto_pub -d -t orz -m "${message}" -h ${BROKER} --topic ${TOPIC}
#
publish_off.sh
#
#   publish_off.sh
#
BROKER="example.com"
TOPIC="example/test01"
message='{"state": {"desired" : {"led" : "off"}}}'
#
mosquitto_pub -d -t orz -m "${message}" -h ${BROKER} --topic ${TOPIC}
#

MQTT のモニター

subscribe.sh
#
#   subscribe.sh
#
BROKER="example.com"
TOPIC="example/test01"
#
mosquitto_sub -d -t orz -h ${BROKER} --topic ${TOPIC}
#

実装例

Python3: MQTT クライアントのサンプル
Node.js: MQTT クライアントのサンプル

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