Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'mqtt'
- require 'json'
- # 環境変数 mqtt_host
- mqtt_host = 'org-id.messaging.internetofthings.ibmcloud.com'
- # 環境変数 mqtt_u|s_port
- mqtt_port = 1883
- # IoT Platform 組織ID
- org_id = 'org-id'
- # IoT Platform デバイス・タイプ
- device_type = 'rpi'
- # IoT Platfrom デバイスID
- device_id = 'HeyaNoSumi'
- # IoT Platform 認証トークン
- device_token = 'your-device-token'
- # IoT Platform へ接続
- client = MQTT::Client.connect(
- :host => mqtt_host,
- :port => mqtt_port,
- :username => 'use-token-auth', # auth type
- :password => device_token,
- :client_id = 'd:' + org_id + ':' + device_type + ':' + device_id) # d:org-id:device-type:device-id
- # センサデータの取得等の処理を行う…今回は割愛。
- # テストデータとして t = 10 のデータを送信する。
- # パブリッシュするテストJSON の構成
- msg = JSON.generate({
- "d" => {
- "t" => 10
- }
- })
- # パブリッシュするトピック
- # イベントID (ユーザが定義)
- event_id = 'status'
- # データフォーマット (パブリッシュ内容により定義)
- format = 'json'
- # パブリッシュ
- client.publish(
- 'iot-2/evt/' + event_id + '/fmt/' + format, # topic
- msg)
- # 接続終了
- client.disconnect
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement