Reading out TP-Link HS110 on Linux/Raspberry Pi

By Atomstar on Sunday 25 November 2018 13:47 - Comments (2)
Categories: Linux, Smarthome, Views: 788

I bought a TP-Link HS110 version 3.0 smart wi-fi plug for 30 EUR to read out power usage of some appliances. I have a Raspberry Pi 3B+ to connect this to running domoticz and influxdb/grafana to visualise the data.

The device identifies itself as:
{
  "system": {
    "get_sysinfo": {
      "sw_ver": "1.4.3 Build 171009 Rel.104144",
      "hw_ver": "2.0",
      "type": "IOT.SMARTPLUGSWITCH",
      "model": "HS110(EU)",
      "mac": "ab:cd:ef:gh:ij:kl",
      "dev_name": "Smart Wi-Fi Plug With Energy Monitoring",
      "alias": "TP-LINK_Smart Plug_IJKL",
      "relay_state": 1,
      "on_time": 336,
      "active_mode": "none",
      "feature": "TIM:ENE",
      "updating": 0,
      "icon_hash": "",
      "rssi": 33,
      "led_off": 0,
      "longitude_i": 0,
      "latitude_i": 0,
      "hwId": "044A516EE63C875F9458DA25C2CCC5A0",
      "fwId": "00000000000000000000000000000000",
      "deviceId": "8006FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
      "oemId": "1998A14DAA86E4E001FD7CAF42868B5E",
      "err_code": 0
    }
  }
}
where mac, the last 4 digits of alias, and the last 36 digits of deviceId have been masked.

Device setup

Here we connect the HS110 to our existing WiFi network without using the TP-link Kasa app and without it ever connecting to the TP-link cloud server.
  1. Plug into wall socket, the wifi symbol will blink green-amber.
  2. Get python clients https://github.com/softScheck/tplink-smartplug
  3. Connect to HS110 access point TP-LINK_Smart Plug_XXXX.
    1. Get initial information:
      ./tplink_smartplug.py -t 192.168.0.1 -c info
  4. Disable cloud access:
    1. Set devs.tplinkcloud.com to resolve to 127.0.0.1 (or similar)
    2. Set cloud server to new dummy value of trusted party: using
      ./tplink_smartplug.py -t 192.168.0.1 -j '{"cnCloud":{"set_server_url":{"server":"xs4all.nl"}}}'
      N.B. err_code:0 means no error
  5. Bind to existing WiFi network:
    ./tplink_smartplug.py -t 192.168.0.1 -j '{"netif":{"set_stainfo":{"ssid":"WiFi","password":"123","key_type":3}}}'
    N.B. This command is not encrypted so it will leak your SSID and network. There is no way around it as the official Kasa app uses the same protocol.
  6. Recommend to give it a fixed IP

Device read out

Now the device is connected to our WiFi network and we can use it as a measurement device
  1. Find out new IP
    1. On your router, look at connected devices, write down IP
    2. Ping broadcast address to find IP of all connected devices responding to ping: e.g. ping 192.168.0.255, the TP-link HS110 will respond
  2. Read out device:
    ./tplink_smartplug.py -t 192.168.0.134 -c energy

Implementing in Domoticz/Influxdb

Here I automate measurement data collection on my Rpi:
Influxdb
  1. Get updated script from https://github.com/tvwerkhoven/tplink-smartplug, store somewhere
    git clone https://github.com/tvwerkhoven/tplink-smartplug.git
  2. Install as crontab, e.g.
    */1 * * * * /home/pi/workers/hs110_reader/tplink_smartplug.py -t 172.16.0.204 -c energy --influxdb http://172.16.0.133:8086 smarthome --influxdb_energy "energy,type=elec,device=hs110" --influxdb_power "power,type=elec,device=hs110"
Domoticz
Todo, probably: create virtual sensor/meter, update script to push to domoticz, adapt cron, done.

Accuracy

Accuracy is comparable to Brennenstuhl PM 231E offline power meter, which has a reported accuracy of 1% or 0.2W.
15W incandescent bulb
  • Reference measurement: 15.00.2W (average-by-eye)
  • TP-Link measurement: 15.030.09W (average of 10 measurements)
1300W heating iron
  • Reference measurement: 13405W (average-by-eye)
  • TP-link measurement: 1342W5W (average of 14 measurements)
N.B. the heating iron's load drops 20W over the timeframe of a minute, such that these uncertainties are upper limits.

Power usage

The TP-link HS110 itself uses 4.30.2 Watt (as measured by Brennenstuhl PM 231E)

Speed

The plug starts up in 8 seconds, and then takes 3 seconds to join the WiFi network.

Alternatives

  1. Elgato Eve Energy works with Bluetooth (vs WiFi), compatible with Homekit. Could work on Linux using Homebridge, but this sounded like more trouble than reading out over Wi-Fi/HTTP
  2. Fibaro Wall Plug works with Z-wave (vs WiFi), requires additional Z-wave dongle, which I don't have. It's also more expensive than the TP-link. After that it could work on Linux, e.g. via this OpenHab post or this Youtube Tutorial

Sources