TP-Link smart plug HS100/HS110

Others (MiLight, Hue, Toon etc...)

Moderator: leecollings

Post Reply
reddo
Posts: 30
Joined: Thursday 01 September 2016 12:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

TP-Link smart plug HS100/HS110

Post by reddo »

Evening,

I've got domoticz running a week now, controlling my sunscreen based on the power delivery of my solar panels and the windspeed read from Weather Underground, some lights and 4 smoke detectors. Heating pump control is coming with an ESP8266 to measure the water temp with a DS18B20.

Now I've got 3 TP-Link smart plugs that I used before I got Domoticz and found a script but.. no joy when I place it in the script folder and call the on and off commands to the correct IP adresses and port 9999 (checked that, is indeed 9999) from a virtual switch :( (script:///home/pi/domoticz/scripts/hs100.sh 192.168.1.218 9999 on)

result from the LOG:
Error executing script command (/home/pi/domoticz/scripts/hs100.sh). returned: 32256

Anyone any suggestions ??

Script found at :

https://github.com/ggeorgovassilis/linu ... g/hs100.sh
-Domoticz-RFLink-PVoutput-Solarmeter-SBFSpot-ESPEasy-Volumio with spotify connect-
sebastiano
Posts: 1
Joined: Tuesday 20 September 2016 10:05
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Netherlands
Contact:

Re: TP-Link smart plug HS100/HS110

Post by sebastiano »

Hi i think the script is not executable by domoticz.

Execute following in the console, and try it again

Code: Select all

chmod +x /home/pi/domoticz/scripts/hs100.sh
Let me know how it went, i'm going to buy the same switches if it works.
Raspberry PI 3 B - Aeotec Z-Stick Gen5 - Aeotec MultiSensor 6 -
Aeotec Smart Switch 6 - FIBARO Wall Plug - Neo CoolCam Power plug -
RFXCOM - RFXtrx433 USB 433.92MHz
reddo
Posts: 30
Joined: Thursday 01 September 2016 12:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

Re: TP-Link smart plug HS100/HS110

Post by reddo »

Already did... nothing...

Should be possible to get them to work though but sadly, I'm no programmer....
-Domoticz-RFLink-PVoutput-Solarmeter-SBFSpot-ESPEasy-Volumio with spotify connect-
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: TP-Link smart plug HS100/HS110

Post by MikeF »

Have a look at this website:
https://www.softscheck.com/en/reverse-e ... ink-hs110/
- they've done a thorough job of reverse engineering these plugs, and they've produced a python script to send commands to the plug.

For instance, with the HS110, if you send the following from the command line:

Code: Select all

./tplink-smartplug.py -t <IP of HS110> -j '{"emeter":{"get_realtime":{}}}'
you'll get JSON output like this (OK, I've prettied it up):

Code: Select all

{
    "emeter": {
        "get_realtime": {
            "current": 0.848669, 
            "err_code": 0, 
            "power": 114.479615, 
            "total": 0.022, 
            "voltage": 240.036493
        }
    }
}
(I've actually modified the script, to include 'energy' as a recognised command, so that

Code: Select all

./tplink-smartplug.py -t <IP of HS110> -c energy 
produces the same output.)

As this is JSON, you should be able to send data to a virtual (text) switch in Domoticz.
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: TP-Link smart plug HS100/HS110

Post by MikeF »

...and send 'on' and 'off' commands:

Code: Select all

script:///home/pi/domoticz/scripts/tplink-smartplug.py -t <IP of HS110> -c on
and

Code: Select all

script:///home/pi/domoticz/scripts/tplink-smartplug.py -t <IP of HS110> -c off
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: TP-Link smart plug HS110 - send energy data to Domoticz

Post by MikeF »

I have now created a python script to send energy data from the TP-Link HS110 Smart Plug to Domoticz:
Spoiler: show

Code: Select all

#!/usr/bin/env python
# 
# TP-Link Wi-Fi Smart Plug Protocol Client
# For use with TP-Link HS110: energy monitor
# 
# Gets current power (W) and cumulative energy (kWh)
# and sends to Domoticz

import socket
import argparse
import json
import urllib
import urllib2

# ip, port, and command for HS110
ip = '<IP of HS110>'
port = 9999
cmd = '{"emeter":{"get_realtime":{}}}'

# Domoticz command stub and IDx of HS110
baseURL = 'http://<Domoticz IP:port>/json.htm?type=command&param=udevice&nvalue=0'
HSIdx = <Domoticz IDx of HS110>

# Encryption and Decryption of TP-Link Smart Home Protocol
# XOR Autokey Cipher with starting key = 171
def encrypt(string):
	key = 171
	result = "\0\0\0\0"
	for i in string: 
		a = key ^ ord(i)
		key = a
		result += chr(a)
	return result

def decrypt(string):
	key = 171 
	result = ""
	for i in string: 
		a = key ^ ord(i)
		key = ord(i) 
		result += chr(a)
	return result

def domoticzrequest (url):
   request = urllib2.Request(url)
#   request.add_header("Authorization", "Basic %s" % base64string)
   response = urllib2.urlopen(request)
   return None;

# Send command and receive reply 
try:
	sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	sock_tcp.connect((ip, port))
	sock_tcp.send(encrypt(cmd))
	data = sock_tcp.recv(2048)
	sock_tcp.close()
	
#	print "Sent:     ", cmd
	result = decrypt(data[4:])
	jsonData = json.loads(result)
#	print "Received: "
#	print json.dumps(jsonData, indent=4, sort_keys=True)
	power = jsonData['emeter']['get_realtime']['power']
	total = jsonData['emeter']['get_realtime']['total'] * 1000
#	print power, total
except socket.error:
	quit("Cound not connect to host " + ip + ":" + str(port))

# Send data to Domoticz
try:
    url = baseURL + "&idx=%s&svalue=%s;%s" % (HSIdx, power, total)
    domoticzrequest(url)
except urllib2.URLError, e:
	print e.code
Create a virtual sensor with sensor type 'Electric (Instant+Counter)', and insert its IDx and the IP addresses of your HS110 and Domoticz server in the script. Then set up a crontab entry to run the script every 5 or 10 minutes, as you prefer.

It will appear in the Utilities page - here's an example:
Image
uronito
Posts: 10
Joined: Tuesday 04 October 2016 20:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: TP-Link smart plug HS100/HS110

Post by uronito »

Please, can you modify te script for send if hs110i is on /off state to another switch (type button) in domoticz?

Thanks!!!!!
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: TP-Link smart plug HS100/HS110

Post by MikeF »

@uronito,

I'm not going to do it for you, but I'll tell you how to do it.

There is an HS110 command which returns system info: {"system":{"get_sysinfo":{}}}
- so you need to use this instead (line 18).
One of the values this returns is relay_state, which can be 1 or 0.
Replace lines 63 and 64 with a single line:

Code: Select all

   relay_state = jsonData['system']['get_sysinfo']['relay_state']
You'll need to convert this to 'On' or 'Off' for the Domoticz command - see below.

There is a different format of Domoticz command for updating a switch (see the Domoticz wiki: API / JSON URLs)
- so you'll need to change lines 21 & 71 accordingly.
reddo
Posts: 30
Joined: Thursday 01 September 2016 12:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

Re: TP-Link smart plug HS100/HS110

Post by reddo »

MikeF wrote:...and send 'on' and 'off' commands:

Code: Select all

script:///home/pi/domoticz/scripts/tplink-smartplug.py -t <IP of HS110> -c on
and

Code: Select all

script:///home/pi/domoticz/scripts/tplink-smartplug.py -t <IP of HS110> -c off
Now i've placed the pythion script in the correct dir, added these to lines to the virtual switch i've made with the right IP of the TPlink but.. no joy....

2016-10-06 00:24:38.325 Executing script: /home/pi/domoticz/scripts/python/tplink_smartplug.py
2016-10-06 00:24:38.348 Error: Error executing script command (/home/pi/domoticz/scripts/python/tplink_smartplug.py). returned: 32256
-Domoticz-RFLink-PVoutput-Solarmeter-SBFSpot-ESPEasy-Volumio with spotify connect-
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: TP-Link smart plug HS100/HS110

Post by MikeF »

@reddo,

I haven't actually tried this from a Domoticz switch, I've only run it from the command line to check it works.
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: TP-Link smart plug HS100/HS110

Post by MikeF »

OK, I've tried this, and it works for me.

I've set up the following two commands:

Code: Select all

script:///home/pi/devices/tplink-smartplug.py -t 192.168.0.84 -c on
script:///home/pi/devices/tplink-smartplug.py -t 192.168.0.84 -c ff
(my path is different)
and I get:

Code: Select all

2016-10-06 10:21:31.740 User: Admin initiated a switch command
2016-10-06 10:21:31.741 (HS110) Light/Switch (HS110 dummy)
2016-10-06 10:21:32.455 Executing script: /home/pi/devices/tplink-smartplug.py 
Have you checked permissions of the script? Try

Code: Select all

chmod a+x tplink-smartplug.py
in the directory where the script is.
reddo
Posts: 30
Joined: Thursday 01 September 2016 12:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

Re: TP-Link smart plug HS100/HS110

Post by reddo »

hm... still same answer in the domoticz log. Tried it from the command line in the PI, script seems to be working but somehow I get an error:

pi@raspberrypi:~/domoticz/scripts/python$ python tplink_smartplug.py -t 192.168.1.218 -c on
Sent: {"system":{"set_relay_state":{"state":1}}}
Received: {"system":{"set_relay_state":{"err_code":0}}}
pi@raspberrypi:~/domoticz/scripts/python$



now the plugs are all configured with the KASA app and can be switched from there.... is that the problem ??
-Domoticz-RFLink-PVoutput-Solarmeter-SBFSpot-ESPEasy-Volumio with spotify connect-
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: TP-Link smart plug HS100/HS110

Post by MikeF »

That's not an error - that's what I get.

I'm monitoring the results of these commands through Kasa, so it's not due to that.
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: TP-Link smart plug HS100/HS110

Post by MikeF »

Run this from the command line:

Code: Select all

./tplink-smartplug.py -t 192.168.1.218 -c info
and post the output here.
reddo
Posts: 30
Joined: Thursday 01 September 2016 12:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

Re: TP-Link smart plug HS100/HS110

Post by reddo »

pi@raspberrypi:~/domoticz/scripts/python$ python tplink_smartplug.py -t 192.168.1.218 -c info
Sent: {"system":{"get_sysinfo":{}}}
Received: {"system":{"get_sysinfo":{"err_code":0,"sw_ver":"1.0.8 Build 151101 Rel.24452","hw_ver":"1.0","type":"smartplug","model":"HS100(EU)","mac":"50:C7:BF:00:CC:4D","deviceId":"8006AC090ECD80A3E34D602A934F239C170E8223","hwId":"22603EA5E716DEAEA6642A30BE87AFCA","fwId":"BFF24826FBC561803E49379DBE74FD71","oemId":"812A90EB2FCF306A993FAD8748024B07","alias":"Computer Kamer","dev_name":"Wi-Fi Smart Plug","icon_hash":"","relay_state":1,"on_time":5999,"active_mode":"schedule","feature":"TIM","updating":0,"rssi":-71,"led_off":0,"latitude":53.046426,"longitude":5.669153}}}
-Domoticz-RFLink-PVoutput-Solarmeter-SBFSpot-ESPEasy-Volumio with spotify connect-
reddo
Posts: 30
Joined: Thursday 01 September 2016 12:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

Re: TP-Link smart plug HS100/HS110

Post by reddo »

re-done the hs switches and now can switch from command line... not from the virtual switch yet....
-Domoticz-RFLink-PVoutput-Solarmeter-SBFSpot-ESPEasy-Volumio with spotify connect-
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: TP-Link smart plug HS100/HS110

Post by MikeF »

This all looks OK - comparing it to the example on the website https://www.softscheck.com/en/reverse-e ... ink-hs110/
... and presumably

Code: Select all

./tplink-smartplug.py -t 192.168.0.1.218 -c off
turns the relay off?
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: TP-Link smart plug HS100/HS110

Post by MikeF »

OK, just saw your last post.
reddo
Posts: 30
Joined: Thursday 01 September 2016 12:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

Re: TP-Link smart plug HS100/HS110

Post by reddo »

from within the correct dir (scripts) this command from the command line in putty

python tplink_smartplug.py -t 192.168.1.218 -c on

works, off function too.

when from the virtual switch, no joy....

script://home/pi/domoticz/scripts/python/tplink_smartplug.py -t 192.168.1.218 -c on

gives the following in the log

2016-10-06 17:07:06.576 User: redmer initiated a switch command
2016-10-06 17:07:06.576 (hs100_switch) Light/Switch (3D printer)

same command with but with 3 forward slashes (as it should be I think)

script:///home/pi/domoticz/scripts/python/tplink_smartplug.py -t 192.168.1.218 -c on

gives

2016-10-06 17:09:29.083 User: redmer initiated a switch command
2016-10-06 17:09:29.084 (hs100_switch) Light/Switch (3D printer)
2016-10-06 17:09:29.746 Executing script: /home/pi/domoticz/scripts/python/tplink_smartplug.py
2016-10-06 17:09:29.810 Error: Error executing script command (/home/pi/domoticz/scripts/python/tplink_smartplug.py). returned: 32512

i've chmodded the script but I can't get it to work from domoticz. Works 100% from the putty command line...
-Domoticz-RFLink-PVoutput-Solarmeter-SBFSpot-ESPEasy-Volumio with spotify connect-
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: TP-Link smart plug HS100/HS110

Post by MikeF »

I'm really at a loss to explain this. I just downloaded the python script again (on my Apple Mac), saved it with a different filename (HS110.py), and copied it across to my Raspberry Pi that runs Domoticz. I changed the 'on' and 'off' commands to pick up the new filename, clicked on the dummy switch icon in Domoticz, and turned it off and back on again (monitoring it via the Kasa app).

The permissions I have on the Pi are:

Code: Select all

-rwxr-xr-x 1 pi pi 3062 Oct  6 22:21 HS110.py
The Domoticz command I have is:

Code: Select all

script:///home/pi/devices/HS110.py -t 192.168.0.84 -c on
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest