TP-Link スマートコンセントをAPIから操作(2)

前回はnode.jsベースのtplink-smarthome-apiというAPIを試したが、まだまだ理解できていないし、テレビの見過ぎを防止するために時間をカウントして消すという方法ができそうにない。他にPythonベースのAPIを見つけた。

Reverse Engineering the TP-Link HS110

書いている内容が理解できないのは自分の英語のレベルが低いせいだけではない。タイトルにある通り、リバースエンジニアリングで解析しているとのことだ。やっていることはとても真似できそうにもないが、pythonで書かれているスクリプトは非常にわかりやすかった。

プラグへ通信データを送受信するときは0x85 (171)を先頭キーとして、暗号化しているとのことだ。また、スクリプトはPython2ベースとのこと。

コマンド名を入れるやり方とJSONで書くやり方2種類用意されている。
コマンド名指定で電源ON

$ python2.7 tplink_smartplug.py -t 192.168.0.2 -c on
Sent: {"system":{"set_relay_state":{"state":1}}}
Received: {"system":{"set_relay_state":{"err_code":0}}}
JSON指定で電源ON
$ python2.7 tplink_smartplug.py -t 192.168.0.2 -j '{"system":{"set_relay_state":{"state":1}}}'
Sent: {"system":{"set_relay_state":{"state":1}}}
Received: {"system":{"set_relay_state":{"err_code":0}}}

コマンドは以下のものがある。

CommandDescription
onTurns on the plug
offTurns off the plug
infoReturns device info
cloudinfoReturns cloud connectivity info
wlanscanScan for nearby access points
timeReturns the system time
scheduleLists configured schedule rules
countdownLists configured countdown rules
antitheftLists configured antitheft rules
rebootReboot the device
resetReset the device to factory settings
energyReturn realtime voltage/current/power

最後の項目にあるenargyなら、電流値を見てテレビがついているかわかるかと思ったが、HS105は対応していないらしい。

$ python2.7 tplink_smartplug.py -t 192.168.0.2 -c energy
Sent: {“emeter”:{“get_realtime”:{}}}
Received: {“emeter”:{“get_realtime”:{“err_code”:-1,”err_msg”:”module not support”}}}

ちなみにpyton3で試したら2と3との互換の関係でできなかった。原因がわかったらまた報告します。

コメントする

メールアドレスが公開されることはありません。 が付いている欄は必須項目です