LoginSignup
0
0

More than 5 years have passed since last update.

mosquitto-client + TLS でニフティクラウド IoT デバイスハブの API にアクセスする

Last updated at Posted at 2017-02-28

タイトルの通り、mosquitto-client + TLS で、ニフティクラウド IoT デバイスハブの API にアクセスしてみました。

環境

  • ニフティクラウド Computing + Ubuntu 14.04 サーバー (標準イメージから作成)

準備

まず、mosquitto-client をインストールします。ドキュメント に書かれてあるコマンドを試すためには、ppa を追加して新しめのバージョンをインストールする必要がありました。
(試行錯誤したせいで install/update/dist-upgrade の順番が変ですが、ご容赦ください。)

# apt-get install -y mosquitto-client
# apt-get install -y software-properties-common
# apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
# apt-get update
# apt-get dist-upgrade -y

mosquitto_pub/mosquitto_sub というコマンドが使えるようになります。

# mosquitto_pub --help | head -2
mosquitto_pub is a simple mqtt client that will publish a message on a single topic and exit.
mosquitto_pub version 1.4.10 running on libmosquitto 1.4.10.
# mosquitto_sub --help | head -2
mosquitto_sub is a simple mqtt client that will subscribe to a single topic and print all messages it receives.
mosquitto_sub version 1.4.10 running on libmosquitto 1.4.10.

次に、IoT デバイスハブは DigiCert の SSL 証明書を利用しているため、DigiCert のルート CA 証明書をダウンロードし、der 形式から mosquitto-client が対応している pem 形式への変換を行います。

# wget https://www.digicert.com/CACerts/DigiCertGlobalRootCA.crt
# openssl x509 -in DigiCertGlobalRootCA.crt -inform der -out DigiCertGlobalRootCA.crt.pem

ここまでできたら準備完了です。

イベント送信

まず、イベント送信をやってみます。
コマンドラインから下記のコマンドを実行します。
(試す際には ${DEVICE_ID}${DEVICE_API_KEY} を各自のものに置き換えて実行してください。)

# mosquitto_pub \
  -d \
  -V mqttv311 \
  -h iot-device.jp-east-1.mqtt.cloud.nifty.com \
  -p 8883 \
  --cafile DigiCertGlobalRootCA.crt.pem \
  -t /devices/${DEVICE_ID}/event \
  -m '{ "eventType": "test", "eventData": { "x": 123 } }' \
  -u ${DEVICE_ID} \
  -P ${DEVICE_API_KEY} \
  -q 1
Client mosqpub/27898-e1iotstgc sending CONNECT
Client mosqpub/27898-e1iotstgc received CONNACK
Client mosqpub/27898-e1iotstgc sending PUBLISH (d0, q1, r0, m1, '/devices/${DEVICE_ID}/event', ... (50 bytes))
Client mosqpub/27898-e1iotstgc received PUBACK (Mid: 1)
Client mosqpub/27898-e1iotstgc sending DISCONNECT

アプリシミュレーターから、イベントが送信されたことが確認できました。

コマンド受信

次にコマンド受信を試してみます。
下記のようなコマンドを実行すると、Subscribed と表示され、待ち状態になります。

# mosquitto_sub \
  -d \
  -V mqttv311 \
  -h iot-device.jp-east-1.mqtt.cloud.nifty.com \
  -p 8883 \
  --cafile DigiCertGlobalRootCA.crt.pem \
  -t /devices/${DEVICE_ID}/command \
  -u ${DEVICE_ID} \
  -P ${DEVICE_API_KEY}
Client mosqsub/27811-e1iotstgc sending CONNECT
Client mosqsub/27811-e1iotstgc received CONNACK
Client mosqsub/27811-e1iotstgc sending SUBSCRIBE (Mid: 1, Topic: /devices/${DEVICE_ID}/command, QoS: 0)
Client mosqsub/27811-e1iotstgc received SUBACK
Subscribed (mid: 1): 0

そのままにしておき、アプリシミュレーターからデバイスへコマンドを送信してみます。

待ち状態となっていた mosquitto_sub コマンドに戻ると、下記のようにコマンド受信できていることが確認できました。

Client mosqsub/27811-e1iotstgc received PUBLISH (d0, q0, r0, m0, '/devices/${DEVICE_ID}/command', ... (48 bytes))
{"action":"test","parameters":{"hello":"world"}}

まとめ

mosquitto-client + TLS でニフティクラウド IoT デバイスハブの API にアクセスしてみることができました。
ちょっとした検証等に便利なので、よろしければお試しください。

参考資料

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