LoginSignup
0
0

More than 5 years have passed since last update.

共用サブスクリプションを試してみる - Watson IoT Platform

Last updated at Posted at 2017-04-27

初めてWatson IoT Platformを使ってみよう、というかたのためのガイドです。
Watson IoT Platformを使ってみるでIoTアプリを準備しました。
Watson IoT Platformで共用サブスクリプションを試してみます。

1. Watson IoT Platformサービスを用意する

2. pythonクライアントでpub/subしてみる

  • ローカル端末のpython環境から`python 01下記フローをコピペします

  • パブリッシュするクライアント(デバイス側を想定)

  • 共用サブスクリプションを使用する際に、publisher側の変更は不要です。

01_dev_pub.py
import paho.mqtt.client as mqtt
import random
import time

org = "6桁の組織ID"
device_type = "デバイス・タイプ" #device type
device_id = "デバイスID" #Defined as "Device ID" in Watson IoT Platform
auth_token = "18桁のauth-token" #auth token

username = "use-token-auth"
broker = org + ".messaging.internetofthings.ibmcloud.com"
client = "d:" + org + ":" + device_type + ":" + device_id
topic = "iot-2/evt/status/fmt/json"

def on_connect(client, userdata, rc):
    print("Connected with result code "+str(rc))

def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))

def publish():
    cnt = 1
    while 1:
            temp = random.randrange(250,450)/10.0
            humid = random.randrange(500,1000)/10.0
            print "counter = " + str(cnt) + ", temp = " + str(temp) + ", humid = " + str(humid)
            msg = " {\"d\": {\"Counter\": " + str(cnt) + ",\"Temperature\": " + str(temp) +",\"Humidity\": " + str(humid) + "} }"
            client.publish(topic, msg, 0, True)
            cnt = cnt + 1
            time.sleep(2)

if __name__ == '__main__':

    client = mqtt.Client(client_id=client, clean_session=True, protocol=mqtt.MQTTv311)
    client.username_pw_set(username, password=auth_token)
    client.on_connect = on_connect
    client.on_message = on_message

    client.connect(broker, 1883, 60)
    publish()

  • 共用サブスクライブするアプリケーションクライアント
  • Watson IoT Platformで共用サブスクリプションを使用するには、下記のようにクライアント名を"A"で始めます(他の設定は不要です)
02_appl_shared_sub.py
import paho.mqtt.client as mqtt

org = "6桁の組織ID"
applId = "アプリ名(「shared_sub_appl」など)"
api_key = "「a-組織ID-10桁」形式のAPIキー" #API key
auth_token = "18桁のauth-token値" #auth-token

broker = org + ".messaging.internetofthings.ibmcloud.com"
# Only "A" was the key. No other change was needed. 17/04/24
client = "A:" + org + ":" + applId
topic = "iot-2/type/デバイス・タイプ/id/+/evt/status/fmt/json"


def on_connect(client, userdata, rc):
    print("Connected with result code "+str(rc))

    client.subscribe(topic)

def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))

if __name__ == '__main__':

    client = mqtt.Client(client_id=client, clean_session=True, protocol=mqtt.MQTTv311)
    client.username_pw_set(api_key, password=auth_token)
    client.on_connect = on_connect
    client.on_message = on_message

    client.connect(broker, 1883, 60)
    client.loop_forever()

結果を確認します

  • 01_dev_pub.pyを1インスタンス稼働させ、それに対して複数の02_appl_shared_sub.pyを稼働させ、共用サブスクリプションが機能していることを確認します

  • パブリッシュしていた01_dev_pub.pyからの出力
    スクリーンショット 2017-04-27 12.43.15.png

-共用サブスクライブしていた02_appl_shared_sub.py 一つ目からの出力
スクリーンショット 2017-04-27 12.43.37.png

-共用サブスクライブしていた02_appl_shared_sub.py 二つ目からの出力
スクリーンショット 2017-04-27 12.43.52.png

-共用サブスクライブしていた02_appl_shared_sub.py 三つ目からの出力
スクリーンショット 2017-04-27 12.44.05.png

  • それぞれのメッセージが特定のサブスクライバーのみに伝搬されている様子が確認できます。
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