LoginSignup
4
1

More than 3 years have passed since last update.

コマンドラインからスマートLEDランプ(TP-Link KL110/KL130)をつける

Posted at

TP-Link社のWifiスマートLEDランプ KL110/KL130 を使って、同一ネットワーク内にあるmacbookからランプをコマンドライン制御します。

color-temp

このランプの良いところは、電灯のON/OFFだけでなく色温度や輝度を調節できる点です。
例えば

  • 暖色系の色で輝度を落として落ち着いた雰囲気で集中作業する
  • 手元でモノを探すときに輝度を上げる
  • 物理本を読むときに色温度を変える(Kindleの背景の白とセピアくらい紙の色味が変わる)

といったことが手元でできるのが良いです。

特に色温度を変えるとだいぶ色味が変わります。
↓実際にランプの色温度最低値と最高値で見比べた写真:2500K<--->9000K
color-temp

こういった色味、明るさ、薄暗さをそのときの気分でコマンドラインで調整できるようにします。

試してみた環境

macbook : macOS Mojave
Node.js : v12.12.0

準備として、最初にスマホアプリでKL110/KL130のWifi設定を済ませておきます。
そして、同一ネットワーク内にmacbookを用意して
macbook ---- Wifiルータ ---- KL110/KL130
という構成にしておきます。
ちなみにKL110/KL130は2.4GHz Wifiにしか対応していないようです。(5GHz未対応)

tplink-lightbulb をインストール

tplink-lightbulbをnpmでグローバルインストールしておきます。
すると tplight コマンドが使えるようになります。

$ npm i -g tplink-lightbulb
$ tplight
Usage: tplight <COMMAND>

コマンド:
  tplight scan                              Scan for lightbulbs
  tplight on <ip>                           Turn on lightbulb
  tplight off <ip>                          Turn off lightbulb
  tplight temp <ip> <color>                 Set the color-temperature of the
                                            lightbulb (for those that support
                                            it)
  tplight hex <ip> <color>                  Set color of lightbulb using hex
                                            color (for those that support it)
  tplight hsb <ip> <hue> <saturation>       Set color of lightbulb using HSB
  <brightness>                              color (for those that support it)
  tplight cloud <ip>                        Get cloud info
  tplight raw <ip> <json>                   Send a raw JSON command
  tplight details <ip>                      Get details about the device

オプション:
  -h, --help  ヘルプを表示                                                [真偽]
  --version   バージョンを表示                                            [真偽]

例:
  tplight scan -h     Get more detailed help with `scan` command
  tplight on -h       Get more detailed help with `on` command
  tplight off -h      Get more detailed help with `off` command
  tplight temp -h     Get more detailed help with `temp` command
  tplight hex -h      Get more detailed help with `hex` command
  tplight hsb -h      Get more detailed help with `hsb` command
  tplight cloud -h    Get more detailed help with `cloud` command
  tplight raw -h      Get more detailed help with `raw` command
  tplight details -h  Get more detailed help with `details` command

You need a command.

シェルスクリプトを書く

tplightコマンドを使ったシェルスクリプトを用意します。
スクリプト内のライトのIPアドレスは環境に合わせて書き換えてください。
~/bin/light などパスを通しているディレクトリにスクリプトを置きます。

#!/bin/bash
LIGHT_IP=192.168.1.14

ON_OFF=1
COLOR_TEMP=2500
BRIGHTNESS=100

if [ "${1}" = "on" ]; then
  ON_OFF=1
elif [ "${1}" = "off" ]; then
  ON_OFF=0
elif [ "${1}" = "hot" ]; then
  ON_OFF=1
  COLOR_TEMP=2500
elif [ "${1}" = "cold" ]; then
  ON_OFF=1
  COLOR_TEMP=9000
fi

if [ -n "${2}" ]; then
  BRIGHTNESS=${2}
fi

tplight raw ${LIGHT_IP} \
"{
  \"smartlife.iot.smartbulb.lightingservice\": {
    \"transition_light_state\": {
      \"on_off\": ${ON_OFF},
      \"color_temp\": ${COLOR_TEMP},
      \"brightness\": ${BRIGHTNESS},
      \"hue\": 0,
      \"saturation\": 0
    }
  }
}" > /dev/null

実行する

ライトを点ける

$ light on

ライトを消す

$ light off

暖色系のライトを点ける

$ light hot

寒色系のライトを点ける

$ light cold

暖色系のライトで輝度を20にする(max 100)

$ light hot 20

カスタマイズする

$ tplight details [IP address] コマンドで現在のライトの状態・設定値をみることができます。
設定値のlight_state あたりを眺めて $ tplight raw [json] でjsonを投げてやれば輝度や色温度を変えることができます。
on_offは1でon、0でoff、brightnessは輝度、color_tempは色温度、hue, saturationはカラー対応(KL130)の場合に色味の設定ができます。

$ tplight details 192.168.1.14
{
  "sw_ver": "1.8.11 Build 191113 Rel.105336",
  "hw_ver": "1.0",
  "model": "KL130(JP)",
  "description": "Smart Wi-Fi LED Bulb with Color Changing",
  "alias": "アプリで設定した名前がここに設定される",
  "mic_type": "IOT.SMARTBULB",
  "dev_state": "normal",
  "mic_mac": "xxxxxxxxxxxx",
  "deviceId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "oemId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "hwId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "is_factory": false,
  "disco_ver": "1.0",
  "ctrl_protocols": {
    "name": "Linkie",
    "version": "1.0"
  },
  "light_state": {
    "on_off": 1,
    "mode": "normal",
    "hue": 0,
    "saturation": 0,
    "color_temp": 2500,
    "brightness": 100
  },
  "is_dimmable": 1,
  "is_color": 1,
  "is_variable_color_temp": 1,
  "preferred_state": [
    {
      "index": 0,
      "hue": 0,
      "saturation": 0,
      "color_temp": 2700,
      "brightness": 50
    },
    {
      "index": 1,
      "hue": 0,
      "saturation": 75,
      "color_temp": 0,
      "brightness": 100
    },
    {
      "index": 2,
      "hue": 120,
      "saturation": 75,
      "color_temp": 0,
      "brightness": 100
    },
    {
      "index": 3,
      "hue": 240,
      "saturation": 75,
      "color_temp": 0,
      "brightness": 100
    }
  ],
  "rssi": -69,
  "active_mode": "none",
  "heapsize": 284368,
  "tid": "tty.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "err_code": 0,
  "lamp_beam_angle": 150,
  "min_voltage": 110,
  "max_voltage": 120,
  "wattage": 10,
  "incandescent_equivalent": 60,
  "max_lumens": 800,
  "color_rendering_index": 80
}

リンク

KL110 : https://www.tp-link.com/jp/home-networking/smart-bulb/kl110/
KL130(マルチカラー対応) : https://www.tp-link.com/jp/home-networking/smart-bulb/kl130/

tplink-lightbulb : https://www.npmjs.com/package/tplink-lightbulb

4
1
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
4
1