2
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.

pahoのsandboxとNode-REDでMQTTの練習

Last updated at Posted at 2016-11-24

初めてWatson IoT Platformを使ってみよう、というかたのためのガイドです。
Watson IoT Platformを使ってみるでIoTアプリを準備しました。
pahoのsandboxとNode-REDを使ってMQTTの基本機能を確認してみます。

0. Node-REDアプリを用意する

  • (Watson IoT Platformを使ってみる)[http://qiita.com/egplnt/items/af867711a7191923b2ff]を参照してNode-REDのアプリを2つ(以上)用意します。
  • ここではtest01とtest02という2つのNode-REDアプリを用意、とします

1. Node-REDでpublish/subscribeしてみる

Node-REDからpub/sub
[{"id":"11549cdc.190a5b","type":"inject","z":"cd034fba.7dbc28","name":"","topic":"","payload":"こんにちは","payloadType":"str","repeat":"","crontab":"","once":false,"x":160,"y":120,"wires":[["b3080a28.87cde8"]]},{"id":"b3080a28.87cde8","type":"mqtt out","z":"cd034fba.7dbc28","name":"pub01","topic":"/topic1","qos":"","retain":"","broker":"3f1776ff.27a58a","x":310,"y":120,"wires":[]},{"id":"d844b6a.429b2c8","type":"mqtt in","z":"cd034fba.7dbc28","name":"sub01","topic":"/topic1","qos":"0","broker":"5c5b48aa.adc918","x":490,"y":120,"wires":[["75f006d0.d31318"]]},{"id":"75f006d0.d31318","type":"debug","z":"cd034fba.7dbc28","name":"","active":true,"console":"false","complete":"false","x":660,"y":120,"wires":[]},{"id":"e1eed094.12741","type":"comment","z":"cd034fba.7dbc28","name":"とりあえずpub/subしてみる","info":"","x":180,"y":60,"wires":[]},{"id":"6521b93b.134dc","type":"mqtt in","z":"cd034fba.7dbc28","name":"sub02","topic":"/topic1","qos":"0","broker":"35d7209f.7fe68","x":490,"y":180,"wires":[["1bebb39b.6911b4"]]},{"id":"1bebb39b.6911b4","type":"debug","z":"cd034fba.7dbc28","name":"","active":true,"console":"false","complete":"false","x":660,"y":180,"wires":[]},{"id":"d53b122c.36ada8","type":"mqtt in","z":"cd034fba.7dbc28","name":"sub03","topic":"/topic1","qos":"0","broker":"8276a9b0.cc689","x":490,"y":240,"wires":[["6dbe1238.190004"]]},{"id":"6dbe1238.190004","type":"debug","z":"cd034fba.7dbc28","name":"","active":true,"console":"false","complete":"false","x":660,"y":240,"wires":[]},{"id":"3f1776ff.27a58a","type":"mqtt-broker","z":"cd034fba.7dbc28","broker":"iot.eclipse.org","port":"1883","clientid":"pub01","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"5c5b48aa.adc918","type":"mqtt-broker","z":"cd034fba.7dbc28","broker":"iot.eclipse.org","port":"1883","clientid":"sub01","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"35d7209f.7fe68","type":"mqtt-broker","z":"","broker":"iot.eclipse.org","port":"1883","clientid":"sub02","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"8276a9b0.cc689","type":"mqtt-broker","z":"","broker":"iot.eclipse.org","port":"1883","clientid":"sub03","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""}]


  • MQTTブローカーとしてpahoのiot.eclipse.orgを使用しています
  • フロー左端の灰色のInjectノードのボタンを押し、メッセージがpublishされ、右側のsubscribeしているフローで受信される事を確認します。一対多の通信が簡単に実現されています。
    スクリーンショット 2016-11-24 16.35.53.png

2. Topicの練習

  • 下記のフローをコピペします
Topicの練習
[{"id":"3b2e7d55.5a4342","type":"inject","z":"ae85d786.00355","name":"","topic":"","payload":"こんにちは","payloadType":"str","repeat":"","crontab":"","once":false,"x":140,"y":100,"wires":[["52831a8d.228cd4"]]},{"id":"52831a8d.228cd4","type":"mqtt out","z":"ae85d786.00355","name":"pub11","topic":"/abc/def","qos":"","retain":"","broker":"92f10c84.e84cd8","x":290,"y":100,"wires":[]},{"id":"ec45d90a.78c85","type":"mqtt in","z":"ae85d786.00355","name":"sub11","topic":"/abc","qos":"0","broker":"9e16de64.1b60b","x":460,"y":100,"wires":[["e00ba6d7.c7a2d8"]]},{"id":"e00ba6d7.c7a2d8","type":"debug","z":"ae85d786.00355","name":"/abc を指定","active":true,"console":"false","complete":"payload","x":630,"y":100,"wires":[]},{"id":"6f9f4f23.68ed1","type":"comment","z":"ae85d786.00355","name":"mqtt topicの確認","info":"","x":120,"y":40,"wires":[]},{"id":"43c4573c.2cd25","type":"debug","z":"ae85d786.00355","name":"/abc/def を指定","active":true,"console":"false","complete":"payload","x":640,"y":160,"wires":[]},{"id":"b7f9787f.fbc3b","type":"mqtt in","z":"ae85d786.00355","name":"sub12","topic":"/abc/def","qos":"0","broker":"f429ba39.439e88","x":460,"y":160,"wires":[["43c4573c.2cd25"]]},{"id":"cf23c82f.d4ceb","type":"debug","z":"ae85d786.00355","name":"/abc/# を指定","active":true,"console":"false","complete":"payload","x":640,"y":220,"wires":[]},{"id":"4f860634.ec90b","type":"mqtt in","z":"ae85d786.00355","name":"sub13","topic":"/abc/def","qos":"0","broker":"45f2884d.8ecb48","x":460,"y":220,"wires":[["cf23c82f.d4ceb"]]},{"id":"ad8c2721.3b6c7","type":"debug","z":"ae85d786.00355","name":"/abc/+ を指定","active":true,"console":"false","complete":"payload","x":640,"y":280,"wires":[]},{"id":"d88ed7b8.905e8","type":"mqtt in","z":"ae85d786.00355","name":"sub14","topic":"/abc/+","qos":"0","broker":"b80eef8f.99b28","x":460,"y":280,"wires":[["ad8c2721.3b6c7"]]},{"id":"92f10c84.e84cd8","type":"mqtt-broker","z":"ae85d786.00355","broker":"iot.eclipse.org","port":"1883","clientid":"pub11","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"9e16de64.1b60b","type":"mqtt-broker","z":"ae85d786.00355","broker":"iot.eclipse.org","port":"1883","clientid":"sub11","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"f429ba39.439e88","type":"mqtt-broker","z":"ae85d786.00355","broker":"iot.eclipse.org","port":"1883","clientid":"sub12","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"45f2884d.8ecb48","type":"mqtt-broker","z":"ae85d786.00355","broker":"iot.eclipse.org","port":"1883","clientid":"sub13","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"b80eef8f.99b28","type":"mqtt-broker","z":"ae85d786.00355","broker":"iot.eclipse.org","port":"1883","clientid":"sub14","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""}]

スクリーンショット 2016-11-24 14.28.48.png

3. トピックについてもう少し

  • 下記フローをコピペします
もう少しtopicの確認
[{"id":"e5bd7511.7236b8","type":"inject","z":"92d1a08c.d9a43","name":"","topic":"","payload":"こんにちは","payloadType":"str","repeat":"","crontab":"","once":false,"x":140,"y":100,"wires":[["99ed39bb.664c98"]]},{"id":"99ed39bb.664c98","type":"mqtt out","z":"92d1a08c.d9a43","name":"pub21","topic":"/abc/def/ghi","qos":"","retain":"","broker":"1a163edd.4aa579","x":290,"y":100,"wires":[]},{"id":"5ed5b930.0c24c8","type":"mqtt in","z":"92d1a08c.d9a43","name":"sub21","topic":"/abc/+/ghi","qos":"0","broker":"a9f9396e.d91a5","x":460,"y":100,"wires":[["a3ac1a3b.ca2a88"]]},{"id":"a3ac1a3b.ca2a88","type":"debug","z":"92d1a08c.d9a43","name":"/abc/+/ghi を指定","active":true,"console":"false","complete":"payload","x":650,"y":100,"wires":[]},{"id":"de3b795d.5e9c2","type":"comment","z":"92d1a08c.d9a43","name":"もう少しtopicの確認","info":"","x":130,"y":60,"wires":[]},{"id":"d4f876e4.db763","type":"debug","z":"92d1a08c.d9a43","name":"/abc/# を指定","active":true,"console":"false","complete":"payload","x":640,"y":160,"wires":[]},{"id":"4bbca37.283035c","type":"mqtt in","z":"92d1a08c.d9a43","name":"sub22","topic":"/abc/#","qos":"0","broker":"4313f190.64f7b","x":460,"y":160,"wires":[["d4f876e4.db763"]]},{"id":"a11e0a55.4a90e","type":"debug","z":"92d1a08c.d9a43","name":"/abc/def/+ を指定","active":true,"console":"false","complete":"payload","x":650,"y":220,"wires":[]},{"id":"7e968dec.d1b594","type":"mqtt in","z":"92d1a08c.d9a43","name":"sub23","topic":"/abc/def/+","qos":"0","broker":"d0e63210.9ef26","x":460,"y":220,"wires":[["a11e0a55.4a90e"]]},{"id":"b14b1ad8.a7d918","type":"debug","z":"92d1a08c.d9a43","name":"/abc/def/# を指定","active":true,"console":"false","complete":"payload","x":650,"y":280,"wires":[]},{"id":"4adfacc8.314e74","type":"mqtt in","z":"92d1a08c.d9a43","name":"sub24","topic":"/abc/def/#","qos":"0","broker":"4ccafdc1.d76f44","x":460,"y":280,"wires":[["b14b1ad8.a7d918"]]},{"id":"1a163edd.4aa579","type":"mqtt-broker","z":"92d1a08c.d9a43","broker":"iot.eclipse.org","port":"1883","clientid":"pub21","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"a9f9396e.d91a5","type":"mqtt-broker","z":"92d1a08c.d9a43","broker":"iot.eclipse.org","port":"1883","clientid":"sub21","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"4313f190.64f7b","type":"mqtt-broker","z":"92d1a08c.d9a43","broker":"iot.eclipse.org","port":"1883","clientid":"sub22","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"d0e63210.9ef26","type":"mqtt-broker","z":"92d1a08c.d9a43","broker":"iot.eclipse.org","port":"1883","clientid":"sub23","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"4ccafdc1.d76f44","type":"mqtt-broker","z":"92d1a08c.d9a43","broker":"iot.eclipse.org","port":"1883","clientid":"sub24","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""}]


スクリーンショット 2016-11-24 14.39.45.png

  • pub21から三層のtopic /abc/def/ghiでpublishしています
  • sub21は途中に単一レベルのワイルドカード+を使用し/abc/+/ghiでsubscribeしているので受信できています
  • sub22は最後にマルチレベルのワイルドカード#を使用し/abc/#でsubscribeしているので受信できています
  • sub23とsub24のワイルドカードの使い方でも受信可能です

4. Retainについて

-一つめのアプリ(test01)で下記フローをコピペします

retainの確認
[{"id":"2fe23a8e.3a2db6","type":"inject","z":"86ec7546.fb684","name":"","topic":"","payload":"こんにちは","payloadType":"str","repeat":"","crontab":"","once":false,"x":140,"y":100,"wires":[["c6d3b4c3.bfdf28"]]},{"id":"c6d3b4c3.bfdf28","type":"mqtt out","z":"86ec7546.fb684","name":"pub31","topic":"/abc","qos":"","retain":"true","broker":"e99c1d45.cc40b8","x":290,"y":100,"wires":[]},{"id":"debfd4bb.8b934","type":"mqtt in","z":"86ec7546.fb684","name":"sub31","topic":"/abc","qos":"0","broker":"ad094ad5.b47958","x":470,"y":100,"wires":[["6dcb18bd.662bb8"]]},{"id":"6dcb18bd.662bb8","type":"debug","z":"86ec7546.fb684","name":"/abc を指定","active":true,"console":"false","complete":"payload","x":630,"y":100,"wires":[]},{"id":"308dd84e.441448","type":"comment","z":"86ec7546.fb684","name":"retainの確認","info":"","x":110,"y":60,"wires":[]},{"id":"e99c1d45.cc40b8","type":"mqtt-broker","z":"86ec7546.fb684","broker":"iot.eclipse.org","port":"1883","clientid":"pub31","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"ad094ad5.b47958","type":"mqtt-broker","z":"86ec7546.fb684","broker":"iot.eclipse.org","port":"1883","clientid":"sub31","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""}]

スクリーンショット 2016-11-24 14.48.59.png

  • フロー左端のInjectノードを押し、メッセージが伝搬されるのを確認します。

  • 二つめのアプリ(test02)に下記のフローをコピペーします

retainの確認
[{"id":"a23134b8.bf251","type":"debug","z":"fad8a04a.17b828","name":"","active":true,"console":"false","complete":"payload","x":370,"y":100,"wires":[]},{"id":"9cb4cf80.1ff05","type":"mqtt in","z":"fad8a04a.17b828","name":"sub32","topic":"/abc","qos":"2","broker":"89b320f5.3dc71","x":200,"y":100,"wires":[["a23134b8.bf251"]]},{"id":"89b320f5.3dc71","type":"mqtt-broker","z":"fad8a04a.17b828","broker":"iot.eclipse.org","port":"1883","clientid":"sub32","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""}]


スクリーンショット 2016-11-24 14.50.52.png

  • デプロイしたタイミングで、retainされていたメッセージが伝搬されて受信される事を確認します

  • retainされたメッセージを削除するため、以下のフローをコピペします

retainされたメッセージを削除
[{"id":"95ac48b2.aab77","type":"inject","z":"86ec7546.fb684","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"x":150,"y":420,"wires":[["6e4dc9ce.8a4e7"]]},{"id":"6e4dc9ce.8a4e7","type":"mqtt out","z":"86ec7546.fb684","name":"pub32","topic":"/abc","qos":"","retain":"true","broker":"31ff41ec.c42406","x":320,"y":420,"wires":[]},{"id":"9c845414.379fb","type":"mqtt in","z":"86ec7546.fb684","name":"sub33","topic":"/abc","qos":"0","broker":"b9a16b2a.8dfb78","x":470,"y":420,"wires":[["2352b62d.19ba9a"]]},{"id":"2352b62d.19ba9a","type":"debug","z":"86ec7546.fb684","name":"","active":false,"console":"false","complete":"false","x":630,"y":420,"wires":[]},{"id":"31ff41ec.c42406","type":"mqtt-broker","z":"86ec7546.fb684","broker":"iot.eclipse.org","port":"1883","clientid":"pub32","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"b9a16b2a.8dfb78","type":"mqtt-broker","z":"86ec7546.fb684","broker":"iot.eclipse.org","port":"1883","clientid":"sub33","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""}]

スクリーンショット 2016-11-24 14.53.56.png

  • Retainされたものと同じtopicへ長さ0のペイロードのメッセージを送付することでretainされていたメッセージが削除されたことを確認します

5. Birthについて

  • 二つめのアプリ(test02)へ以下のフローをコピペします
Birthメッセージを受信するフロー
[{"id":"b33ad244.b9801","type":"debug","z":"9d39d2de.0296c8","name":"/pub41wasborn へsubscribe","active":true,"console":"false","complete":"payload","x":650,"y":100,"wires":[]},{"id":"ac80cb5b.d9051","type":"mqtt in","z":"9d39d2de.0296c8","name":"sub42","topic":"/pub41wasborn","qos":"0","broker":"5e2d77f6.9a1c7","x":430,"y":100,"wires":[["b33ad244.b9801"]]},{"id":"5e2d77f6.9a1c7","type":"mqtt-broker","z":"9d39d2de.0296c8","broker":"iot.eclipse.org","port":"1883","clientid":"sub42","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""}]

  • 一つめのアプリ(test01)へ以下のフローをコピペします
Birthを設定する側のフロー
[{"id":"eae16f1e.161b98","type":"mqtt out","z":"dd86b0dd.f917c","name":"pub41","topic":"/abc","qos":"","retain":"","broker":"4954fbb4.1fde5c","x":790,"y":140,"wires":[]},{"id":"1e8159f0.37251e","type":"comment","z":"dd86b0dd.f917c","name":"birthの確認","info":"","x":680,"y":80,"wires":[]},{"id":"4954fbb4.1fde5c","type":"mqtt-broker","z":"dd86b0dd.f917c","broker":"iot.eclipse.org","port":"1883","clientid":"pub41","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"/pub41wasborn","birthQos":"0","birthPayload":"pub41 was born 2"}]

スクリーンショット 2016-11-24 14.58.10.png

  • このフローではBirthメッセージを設定しています
    スクリーンショット 2016-11-24 15.03.52.png

  • 上記のBirthを設定する側のフローをアプリ1にデプロイし接続(MQTT Connection)が確立した時点で、Birthメッセージを受信する側のフロー(test02)でBirthメッセージが受信される事を確認します
    スクリーンショット 2016-11-24 15.00.02.png

6. Willについて

  • 二つめのアプリ(test02)に以下のフローをコピペします
Willを受信するフロー
[{"id":"ddb577b3.bbb26","type":"debug","z":"4730cb90.7528fc","name":"/pub51wasdisconnected へsubscribe","active":true,"console":"false","complete":"payload","x":350,"y":120,"wires":[]},{"id":"eadcf57.bace508","type":"mqtt in","z":"4730cb90.7528fc","name":"sub51","topic":"/pub51wasdisconnected","qos":"0","broker":"b1af13e7.66cbf","x":100,"y":120,"wires":[["ddb577b3.bbb26"]]},{"id":"b1af13e7.66cbf","type":"mqtt-broker","z":"4730cb90.7528fc","broker":"iot.eclipse.org","port":"1883","clientid":"sub51","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""}]

  • 一つめのアプリ(test01)に以下のフローをコピペします
Willを伝達する側のフロー
[{"id":"d7d19c0f.1d8de","type":"mqtt out","z":"bd2e6dfa.f03d28","name":"pub51","topic":"/abc","qos":"","retain":"","broker":"ee84b39d.d1375","x":310,"y":140,"wires":[]},{"id":"da81ef5f.38fe5","type":"comment","z":"bd2e6dfa.f03d28","name":"Willの確認","info":"","x":240,"y":80,"wires":[]},{"id":"ee84b39d.d1375","type":"mqtt-broker","z":"bd2e6dfa.f03d28","broker":"iot.eclipse.org","port":"1883","clientid":"pub51","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"/pub51wasdisconnected","willQos":"0","willPayload":"pub51 was disconnected","birthTopic":"","birthQos":"0","birthPayload":""}]

スクリーンショット 2016-11-24 15.05.17.png

  • このフローではwillを設定しています。
    スクリーンショット 2016-11-24 15.02.36.png

  • Bluemixのダッシュボード画面で、一つ目のアプリ(test01)を含むIoTアプリを再起動します

  • 再起動したタイミングでMQTTブローカーとの接続が切断され、WILLメッセージが伝搬される事を確認します
    スクリーンショット 2016-11-24 15.07.09.png

2
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
2
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?