TP-Link smart plug HS100/HS110

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

Moderator: leecollings

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 »

-rwxrwxrwx 1 pi pi 3159 Oct 6 17:33 tplink_smartplug.py

but with filezilla I can change it anyway I want but it does not help a bit.

Also made an Sh script on and off that calls the python script on but also, no joy. :evil:
-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 »

Clutching at straws here: have you successfully run other Domoticz commands from switches? have you got security enabled?
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 »

Dunno, will try another scriot but then still.... what is caausing the 'not running' ?

Switch is not secured.

Got Domoticz running on a PI2 now instead of a 3 but no difference.

So, maybe, a little setup a dummy switch for dummies anyone ? Might be something wrong there......
-Domoticz-RFLink-PVoutput-Solarmeter-SBFSpot-ESPEasy-Volumio with spotify connect-
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 »

Thanks @MikeF!!!!!

:D
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 »

Turned out... I had an error in the python script I think. Copied all the lines in to a file and saved that with notepad++ to the python file, which apparently is no good. Now, downloaded the python file, wacked it on the pi and .... working !!!

Thanks for the patience MikeF !!
-Domoticz-RFLink-PVoutput-Solarmeter-SBFSpot-ESPEasy-Volumio with spotify connect-
doaerts
Posts: 1
Joined: Friday 18 November 2016 16:18
Target OS: -
Domoticz version:
Contact:

Re: TP-Link smart plug HS100/HS110

Post by doaerts »

Hi guys

I'm trying to get the python script running (on Windows), but no luck so far...

When using python 3, I get the error message:
"could not connect to host <ip> 9999 "

I also tried using python 2.7, which gives me the error message:
"AttributeError: 'module' objest has no attribute 'inet_pton'"

I'm not a Python expert, could anyone point me in the right direction?
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 »

doaerts wrote:Hi guys

I'm trying to get the python script running (on Windows), but no luck so far...

When using python 3, I get the error message:
"could not connect to host <ip> 9999 "

I also tried using python 2.7, which gives me the error message:
"AttributeError: 'module' objest has no attribute 'inet_pton'"

I'm not a Python expert, could anyone point me in the right direction?
Can't help you with scripting but.. the HS100's just stopped working through domoticz, get an error each time I want to switch them. Strange thing is, I can still switch from the command line... :evil:

Changed nothing, updated nothing, just happened..
-Domoticz-RFLink-PVoutput-Solarmeter-SBFSpot-ESPEasy-Volumio with spotify connect-
jonohunt
Posts: 7
Joined: Wednesday 21 December 2016 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: TP-Link smart plug HS100/HS110

Post by jonohunt »

I've followed the instructions here and am having trouble getting it working with my HS100 plugs.

If I run this from the command line I get:

File "tplink-smartplug.py", line 22
import socket
^
IndentationError: unexpected indent


(I just coped the code from here and pasted it into a file called 'tplink-smartplug.py'.


If I press the switches in Domoticz's web page it shows this n the logs:

2016-12-22 14:04:31.566 Executing script: /home/pi/domoticz/scripts/tplink-smartplug.py
2016-12-22 14:04:31.658 Error: Error executing script command (/home/pi/domoticz/scripts/tplink-smartplug.py). returned: 256



Is this a problem with the script, or could I be doing something wrong?

Maybe one of you who have it working could add a link to download your version of the script and I could try that?

Edit:
I've gone through the steps again from the start and managed to get this working OK :)
rebelo
Posts: 13
Joined: Tuesday 17 January 2017 13:04
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5877
Location: Portugal
Contact:

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

Post by rebelo »

MikeF wrote: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
@MikeF
Tks a lot for this.
Getting though as 401 error even when run from the command line.
Script copied/ edited via nano and chmoded a+x.
Believe it is something regarding authorization but can not find solution.
Any hint from You?
rebelo
Posts: 13
Joined: Tuesday 17 January 2017 13:04
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5877
Location: Portugal
Contact:

Re: TP-Link smart plug HS100/HS110

Post by rebelo »

reddo wrote:
doaerts wrote:Hi guys

I'm trying to get the python script running (on Windows), but no luck so far...

When using python 3, I get the error message:
"could not connect to host <ip> 9999 "

I also tried using python 2.7, which gives me the error message:
"AttributeError: 'module' objest has no attribute 'inet_pton'"

I'm not a Python expert, could anyone point me in the right direction?
Can't help you with scripting but.. the HS100's just stopped working through domoticz, get an error each time I want to switch them. Strange thing is, I can still switch from the command line... :evil:

Changed nothing, updated nothing, just happened..
@reddo

See if this can help You.
Have lost a few hours trying to make this work.
Like You command line was ok but nothing else.
And finaly have found(I think) that editing the scripts in windows even with notepad+ and uploading them via FileZilla did not work.
Using the pi inbuilt nano editor and chomd via commmand line did the trick.

Enjoy :-)
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 »

rebelo wrote: Getting though as 401 error even when run from the command line.
Script copied/ edited via nano and chmoded a+x.
Believe it is something regarding authorization but can not find solution.
Any hint from You?
I've never used authorisation on Domoticz, so don't know, I'm afraid - try without?
rebelo
Posts: 13
Joined: Tuesday 17 January 2017 13:04
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5877
Location: Portugal
Contact:

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

Post by rebelo »

MikeF wrote:
rebelo wrote: Getting though as 401 error even when run from the command line.
Script copied/ edited via nano and chmoded a+x.
Believe it is something regarding authorization but can not find solution.
Any hint from You?
I've never used authorisation on Domoticz, so don't know, I'm afraid - try without?
Tks but what i ment by authorization was abt the error, 401, i get in the command line.
Never mind will try again.
hitech2207
Posts: 3
Joined: Wednesday 15 February 2017 9:19
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: TP-Link smart plug HS100/HS110

Post by hitech2207 »

Hello,

First time reply on this forum but follower since months. :)

I have setup a new Domoticz server on a raspberry pi and it works like i charm.

I still need to read all tutorials and manuals to get it under control.

I have a smartplug HS100 and not HS110.

Meaning that i don't have the energy meter in it.

I am getting these errors when i launch the script:

~/Desktop $ python tplink-smartplug.py -c on
Traceback (most recent call last):
File "tplink-smartplug.py", line 63, in <module>
power = jsonData['emeter']['get_realtime']['power']
KeyError: 'get_realtime'

I tried to remove the energy meter script lines from the given python script without success.

Can someone help me out and provide me a script that just turns on and off the HS100 without any error?

Thank you :)
rebelo
Posts: 13
Joined: Tuesday 17 January 2017 13:04
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5877
Location: Portugal
Contact:

Re: TP-Link smart plug HS100/HS110

Post by rebelo »

@ hitech2207

You have the links in this post
https://raw.githubusercontent.com/softS ... artplug.py
and
https://github.com/softScheck/tplink-smartplug

Check them out.

Rebelo
hitech2207
Posts: 3
Joined: Wednesday 15 February 2017 9:19
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: TP-Link smart plug HS100/HS110

Post by hitech2207 »

Hi Rebelo,

I was able to successfully launch this script and my light on from my raspberry terminal. :D

Now i need to find out how i can implement this in domoticz correctly.

I used this on cmd but nothing happens..

for ON:

script:///home/pi/Desktop/tplink-smartplug.py -t 192.168.1.22 -c on

for OFF:

script:///home/pi/Desktop/tplink-smartplug.py -t 192.168.1.22 -c off

This doesn't work.

When i use in terminal:

python /home/pi/Desktop/tplink-smartplug.py -t 192.168.1.22 -c on

it works..

What is wrong?

Thank you
rebelo
Posts: 13
Joined: Tuesday 17 January 2017 13:04
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5877
Location: Portugal
Contact:

Re: TP-Link smart plug HS100/HS110

Post by rebelo »

Well, path looks ok.
And assuming that You are putting the cmd lines on the virtual switch that You have created.

If so check the script permissions and try to chmod it a+x.

Are you editing the scripts in windows even with notepad+ and uploading/ changing permissions via FileZilla?
I believe that was my mistake at that time.
Using the pi inbuilt nano editor and chomd via commmand line did the trick.

Let me know.
hitech2207
Posts: 3
Joined: Wednesday 15 February 2017 9:19
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: TP-Link smart plug HS100/HS110

Post by hitech2207 »

Hi Rebelo,

sudo chmod a+x did the trick :)

Concerning the scripts i always use the terminal on my mac to ssh into the raspberry pi and sudo nano the scripts.

Thanks again :)
rebelo
Posts: 13
Joined: Tuesday 17 January 2017 13:04
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5877
Location: Portugal
Contact:

Re: TP-Link smart plug HS100/HS110

Post by rebelo »

Good job :D , enjoy it.
User avatar
marcotini
Posts: 28
Joined: Thursday 16 February 2017 17:51
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

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

Post by marcotini »

MikeF wrote: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
Can you explain how to do that? Because when I create the virtual sensor and put the script in /script/ I don't know what else to do.
Screen Shot 2017-02-22 at 22.22.42.png
Screen Shot 2017-02-22 at 22.22.42.png (216.54 KiB) Viewed 6534 times
ikek
Posts: 2
Joined: Sunday 05 February 2017 17:15
Target OS: Windows
Domoticz version:
Contact:

Re: TP-Link smart plug HS100/HS110

Post by ikek »

Did you ever found the solution?
I have exactly the same problem. Strange thing is: I created a Domoticz environment on a Windows computer. The script did id not work with python 2.7.4 but works fine with python 3.4 (I have modified the script for python3). However, if I run python3 on the raspberry with the same script it is still not working...
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest