LoginSignup
0
0

More than 5 years have passed since last update.

Watson IoT Platform - アプリを登録してセンサーデータを受信する

Last updated at Posted at 2016-05-22

初めてWatson IoT Platformを使ってみよう、というかたのためのガイドです。
Watson IoT Platformを使ってみるでIoTアプリを準備しました。
Internet of Things Platformにアプリを登録してデバイスからのデータを送信します。

アプリで使用するAPIキーを取得します

  1. Internet of Things Platformサービスの「ダッシュボード」を起動します
  2. 画面左のメニューから「アクセス」を選択します。
    スクリーンショット 2016-05-22 22.10.04.png

  3. 「アクセス」の画面で「APIキー」のタブを選択し、画面右側の「+APIキーの生成」ボタンを押します。
    スクリーンショット 2016-05-22 22.10.47.png

  4. 「APIキーの生成」画面に表示される「APIキー」と18桁の英数字の「認証トークン」をコピペして保存します。この「認証トークン」はこの画面でしか見られず、後から再度表示したり確認したりすることはできないので注意します。万が一この「認証トークン」を無くしてしまった場合、「APIキー」の再生成からやり直す事になります。(「APIキーの生成」の画面は省略)

  5. 「×」印を押して「APIキーの生成」画面を閉じます。IoT Platformの「アクセス」のページに生成されたAPIキー値(認証トークンはのぞく)が表示されます。
    スクリーンショット 2016-05-22 22.19.34.png

  6. 下記のようなプログラムを用意し、デバイスからInternet of Things Platformへアクセスできる事を確認します。

applSub.py
#!/usr/bin/python
# -*- coding: utf-8 -*-

import logging
import os
import os.path
import sys
import codecs
import time
import paho.mqtt.client as mqtt
import json
import threading
import random

broker = ""
organization = "xxxxxx"     # 6桁の「組織ID」を指定します
applId = "appSubSample"
username = "a-xxxxxx-xxxxxxxxxx" #APIキーを指定します。
password = "xxxxxxxxxxxxxxxxxx"  # 英数字18桁の「認証トークン」を指定します

topic = "iot-2/type/device_type_001/id/+/evt/status/fmt/json"   # /type/の次にデバイスタイプを指定します。

def on_connect(client, userdata, flags, rc):
    print("connected")

    client.subscribe(topic)

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

clientID = "a:" + organization + ":" + applId
broker = organization + ".messaging.internetofthings.ibmcloud.com"
mqttc = mqtt.Client(clientID)

if username is not "":
    mqttc.username_pw_set(username, password=password)

mqttc.on_connect = on_connect
mqttc.on_message = on_message

mqttc.connect(host=broker, port=1883, keepalive=60)

mqttc.loop_forever()
  • 上記のpythonでは、organizationに6桁の「組織ID」を、usernameに「a-組織ID-10桁のきー」の「APIキー」を、passwordに英数字18桁の「認証トークン」を指定します。
  • applIdは適当に指定します。
  • Subscribeするトピック名として、iot-2/type/デバイス・タイプ/id/+/evt/status/fmt/jsonを指定することで、デバイスを登録してデータ送信するで作成したデバイスアプリからpublishされたセンサーデータを受信することができます。/id/+/の指定はデバイスIDが何でも構わない(ワイルド・カード)ことを表します。
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