Watson IoT Platform - MQTT - Topicの確認


初めてWatson IoT Platformを使ってみよう、というかたのためのガイドです。
[Watson IoT Platformを使ってみる](http://qiita.com/egplnt/items/af867711a7191923b2ff)でIoTアプリを準備しました。
[MessageSightを動かす](http://qiita.com/egplnt/items/246205f6574cde268712)でMQTTブローカーとしてMessageSightを準備しました。
[Node-REDをローカル環境で稼働させる](http://qiita.com/egplnt/items/39f4794504fc27823bb4)
と[Watson IoT Platform - MQTT - Node-REDからpub/subする](http://qiita.com/egplnt/items/2105158291f4857aa7b0)でNode-REDからpub/subできるようにしました。

#トピックの確認
- [Node-REDのフローをコピーする方法](http://qiita.com/egplnt/items/84b244a50d8bdaced324)を参照して以下のフローをコピペし、MQTTノードのアドレスを自環境に合わせます。
 
```lang:ワイルドカードの使い方
[{"id":"42b340e4.dc4bd","type":"mqtt-broker","z":"375326d0.fda38a","broker":"192.168.100.100","port":"1883","clientid":"","usetls":false,"verifyservercert":true,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willRetain":null,"willPayload":"","birthTopic":"","birthQos":"0","birthRetain":null,"birthPayload":""},{"id":"3c212a03.3f13fe","type":"debug","z":"375326d0.fda38a","name":"","active":true,"console":"false","complete":"false","x":830,"y":280,"wires":[]},{"id":"629eab7e.16c20c","type":"template","z":"375326d0.fda38a","name":"\"/abc\"で受信","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"\"/abc\"で受信できた。payload: {{payload}}","x":610,"y":280,"wires":[["3c212a03.3f13fe"]]},{"id":"26e1bf98.4a4e78","type":"mqtt in","z":"375326d0.fda38a","name":"","topic":"/abc","broker":"42b340e4.dc4bd","x":430,"y":280,"wires":[["629eab7e.16c20c"]]},{"id":"76758baf.adfb34","type":"mqtt out","z":"375326d0.fda38a","name":"","topic":"/abc/def/ghi","qos":"","retain":"","broker":"42b340e4.dc4bd","x":250,"y":280,"wires":[]},{"id":"7828661d.3fbe58","type":"inject","z":"375326d0.fda38a","name":"","topic":"","payload":"おはよう","payloadType":"str","repeat":"","crontab":"","once":false,"x":100,"y":280,"wires":[["76758baf.adfb34"]]},{"id":"1abe147.55d79ec","type":"debug","z":"375326d0.fda38a","name":"","active":true,"console":"false","complete":"false","x":830,"y":400,"wires":[]},{"id":"4d62d2ba.e1f8ec","type":"template","z":"375326d0.fda38a","name":"\"/abc/def/ghi\"で受信","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"\"/abc/def/ghi\"で受信できた。payload: {{payload}}","x":640,"y":400,"wires":[["1abe147.55d79ec"]]},{"id":"9051b2f5.8e7be8","type":"mqtt in","z":"375326d0.fda38a","name":"","topic":"/abc/def/ghi","broker":"42b340e4.dc4bd","x":450,"y":400,"wires":[["4d62d2ba.e1f8ec"]]},{"id":"a9f1554c.d61418","type":"debug","z":"375326d0.fda38a","name":"","active":true,"console":"false","complete":"false","x":830,"y":340,"wires":[]},{"id":"9e27703c.ae39f","type":"template","z":"375326d0.fda38a","name":"\"/abc/def\"で受信","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"\"/abc/def\"で受信できた。payload: {{payload}}","x":630,"y":340,"wires":[["a9f1554c.d61418"]]},{"id":"fc1d278d.37e0c8","type":"mqtt in","z":"375326d0.fda38a","name":"","topic":"/abc/def","broker":"42b340e4.dc4bd","x":440,"y":340,"wires":[["9e27703c.ae39f"]]}]

```
![スクリーンショット 2016-05-31 12.32.25.png](https://qiita-image-store.s3.amazonaws.com/0/125842/6b82e02f-2563-2119-62f5-89ae95e079d5.png)

- トピックは階層構造にして指定します。
- ここでは`/abc/def/ghi`というトピックでパブリッシュしたので、同じ`/abc/def/ghi`でサブスクライブしたノードのみメッセージを受信することができました。
- `/abc`もしくは`/abc/def`のノードへはメッセージが伝達されていないことを確認します。

#ワイルドカードの使い方
- サブスクリプションではワイルドカードを使用することができます。
- `+`記号は一階層を、`#`はそこから下の全サブツリーを代替します。
- [Node-REDのフローをコピーする方法](http://qiita.com/egplnt/items/84b244a50d8bdaced324)を参照して以下のフローをコピペし、MQTTノードのアドレスを自環境に合わせます。

```lang:ワイルドカードの使い方
[{"id":"42b340e4.dc4bd","type":"mqtt-broker","z":"375326d0.fda38a","broker":"192.168.100.100","port":"1883","clientid":"","usetls":false,"verifyservercert":true,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willRetain":null,"willPayload":"","birthTopic":"","birthQos":"0","birthRetain":null,"birthPayload":""},{"id":"262d8048.85c4c","type":"inject","z":"375326d0.fda38a","name":"","topic":"","payload":"こんにちは","payloadType":"str","repeat":"","crontab":"","once":false,"x":100,"y":80,"wires":[["1f19df03.430769"]]},{"id":"1f19df03.430769","type":"mqtt out","z":"375326d0.fda38a","name":"","topic":"/a/b/c","qos":"","retain":"","broker":"42b340e4.dc4bd","x":230,"y":80,"wires":[]},{"id":"b09b9ba6.e701b8","type":"debug","z":"375326d0.fda38a","name":"","active":true,"console":"false","complete":"false","x":730,"y":80,"wires":[]},{"id":"8050145a.148e18","type":"template","z":"375326d0.fda38a","name":"\"/a/#\"で受信","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"\"/a/#\"で受信できた。payload: {{payload}}","x":570,"y":80,"wires":[["b09b9ba6.e701b8"]]},{"id":"dbb2bf72.f71868","type":"mqtt in","z":"375326d0.fda38a","name":"","topic":"/a/#","broker":"42b340e4.dc4bd","x":430,"y":80,"wires":[["8050145a.148e18"]]},{"id":"a9262b80.dff88","type":"mqtt out","z":"375326d0.fda38a","name":"","topic":"/g/h/i","qos":"","retain":"","broker":"42b340e4.dc4bd","x":230,"y":180,"wires":[]},{"id":"4c8301a5.1f203","type":"inject","z":"375326d0.fda38a","name":"","topic":"","payload":"こんばんは","payloadType":"str","repeat":"","crontab":"","once":false,"x":100,"y":180,"wires":[["a9262b80.dff88"]]},{"id":"e68bafc3.be2c38","type":"debug","z":"375326d0.fda38a","name":"","active":true,"console":"false","complete":"false","x":750,"y":180,"wires":[]},{"id":"ca9712b4.aceaa","type":"template","z":"375326d0.fda38a","name":"\"/g/+/i\"で受信","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"\"/g/+/i\"で受信できた。payload: {{payload}}","x":580,"y":180,"wires":[["e68bafc3.be2c38"]]},{"id":"84d0438f.6a17a","type":"mqtt in","z":"375326d0.fda38a","name":"","topic":"/g/+/i","broker":"42b340e4.dc4bd","x":430,"y":180,"wires":[["ca9712b4.aceaa"]]}]

```
![スクリーンショット 2016-05-31 12.38.25.png](https://qiita-image-store.s3.amazonaws.com/0/125842/01e81aee-5428-e6e8-807a-1b77e478eade.png)

- いろいろ変化させて試してみてください。
- Node-REDのDebug出力の仕様で同じフロー内の別のノードでMQTTメッセージが受信されるとDebug出力へ表示されてしまうので、トピックを区別して練習したほうが良さそうです。