Skip to main content

Hue 電球 ホワイトグラデーション

Philips Hue スマート電球 ホワイトグラデーション を Thing として扱うための手順です。WoTifyにある Thing Description を元に、Node-RED と node-red-nodegen を使用します。

本サンプルでは、REST API が既に用意されており、エンドポイントを決め、API の URL を Thing Description、ノードモジュールへと対応づける例として、 Hue を取り上げ WoT 化を行います。

必要なもの#

Hueのセットアップ#

公式の開発者ページ Get Started に従い進めていきます。
ブリッジと同一のネットワークにてhttps://discovery.meethue.comにアクセスしIPアドレスを確認、
https://<bridge ip address>/debug/clip.htmlのインタフェースにアクセスします。

次に、表示されたインターフェースにて、ユーザ名を取得するために、

  • URL: /api
  • Body: {"devicetype":"my_hue_app#iphone peter"}
  • Method: POST

を行います。この際に、ブリッジのボタンを押す必要があるメッセージが返されます。ボタンを押し、再びPOSTを行うことで、username が得られます。

Thing Description#

今回は、 WoTify にある Philips HUE White Light の Thing Description を利用します。

WoTify より Philips HUE White Light の Thing Description をダウンロードの上、base URI の
"base": "http://192.168.0.111/api/R6D7CYQFzXckikMPLEL8WbSZWg9XKkEyx-NrgKws/lights/1/"
の箇所を先ほど確認した、IPアドレスとユーザ名にそれぞれ置き換えて使います。

使用例#

上記の Thing Description を元にして JSON-LD ファイルを作成し、 node-red-nodegen を使用してノードモジュールを作成します。

npx node-red-nodegen lightTD1.jsonldnpm --prefix ~/.node-red install ./node-red-contrib-wotlightTD1

電球の状態を表示する#

Thing Description のpropertiesの一部を引用し注目すると、titleから、電球の状態を持っており、formsから、それらの情報に読み書きアクセス可能であることがわかります。 また、type,propertiesから、それらの情報はJSONで表現されることがわかります。

...  "properties": {      "lightInformation": {          "title": "State and Attributes Of Light",          "description": "Gets the attributes and state of a given light",          "type": "object",          "properties": {              "state": {                  "description": "Details the state of the light, see the state table below for more details.",                  "type": "object",                  "properties": {                      "on": {                          "description": "On/Off state of the light. On=true, Off=false",                          "type": "boolean",                          "readOnly": true,                          "writeOnly": false                      },                      ......                      "bri": {                          "description": "brightness level",                          "type": "integer",                          "minimum": 0,                          "maximum": 254,                          "readOnly": true,                          "writeOnly": false                      },                      ......                  }              },              ......          },          "forms": [              {                  "href": "",                  "htv:methodName": "GET",                  "http:methodName": "GET",                  "contentType": "application/json",                  "op": [                      "readproperty"                  ]              },              {                  "href": "",                  "htv:methodName": "PUT",                  "contentType": "application/json",                  "op": [                      "writeproperty"                  ]              }          ]      }  },...

したがって、下記のようにHue White Lamp 1ノードを編集することで、電球の状態を表示することができます。

  • Interaction: Property
  • Name: State and Attributes Of Light
  • Access: Read
  • Form: nosec http://...

使用例

使用例

電球を操作する#

Thing Description のactionsの一部を引用し注目すると、電球のオンオフの操作は、booleanによって定義され、明るさはintegerによって定義されていることがわかります。また、formsよりJSONを受け取るように記述されています。

...  "actions": {    ......      "set_state": {          "title": "Set State",          "description": "Allows the user to turn the light on and off, modify the hue and effects",          "input": {              "type": "object",              "properties": {                  "on": {                      "description": "On/Off state of the light. On=true, Off=false",                      "type": "boolean"                  },                  "bri": {                      "description": "Brightness level. Accepts 255 as well",                      "type": "integer",                      "minimum": 0,                      "maximum": 254                  },                ......              }          },          ......          "forms": [              {                  "href": "state",                  "contentType": "application/json",                  "htv:methodName": "PUT",                  "op": [                      "invokeaction"                  ]              }          ],        ......      }  }...

したがって、Hue White Lamp 1ノードを下記のように設定の上、injectノードで電球の状態のJSONを設定することでHueを操作することが出来ます。

  • Interaction: Action
  • Name: Set State
  • Form: nosec http://...

使用例

応用例 (ディマースイッチ・ブリッジ)#

同様の手順で、 Hue ディマースイッチHue ブリッジ をThingとして扱うことができます。
下記のWoTifyにある Thing Description を元に、それぞれWoT化が可能です。