Page 2 of 13
Re: TP-Link smart plug HS100/HS110
Posted: Friday 07 October 2016 17:56
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.
Re: TP-Link smart plug HS100/HS110
Posted: Saturday 08 October 2016 11:28
by MikeF
Clutching at straws here: have you successfully run other Domoticz commands from switches? have you got security enabled?
Re: TP-Link smart plug HS100/HS110
Posted: Saturday 08 October 2016 17:08
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......
Re: TP-Link smart plug HS100/HS110
Posted: Monday 10 October 2016 9:40
by uronito
Thanks @MikeF!!!!!
Re: TP-Link smart plug HS100/HS110
Posted: Monday 10 October 2016 18:05
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 !!
Re: TP-Link smart plug HS100/HS110
Posted: Friday 18 November 2016 16:34
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?
Re: TP-Link smart plug HS100/HS110
Posted: Monday 12 December 2016 18:07
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...
Changed nothing, updated nothing, just happened..
Re: TP-Link smart plug HS100/HS110
Posted: Thursday 22 December 2016 15:10
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
Re: TP-Link smart plug HS110 - send energy data to Domoticz
Posted: Tuesday 17 January 2017 13:12
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¶m=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:
@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?
Re: TP-Link smart plug HS100/HS110
Posted: Tuesday 17 January 2017 13:20
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...
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
Re: TP-Link smart plug HS110 - send energy data to Domoticz
Posted: Monday 23 January 2017 12:07
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?
Re: TP-Link smart plug HS110 - send energy data to Domoticz
Posted: Tuesday 24 January 2017 19:30
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.
Re: TP-Link smart plug HS100/HS110
Posted: Wednesday 15 February 2017 9:27
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
Re: TP-Link smart plug HS100/HS110
Posted: Wednesday 15 February 2017 9:54
by rebelo
Re: TP-Link smart plug HS100/HS110
Posted: Wednesday 15 February 2017 11:08
by hitech2207
Hi Rebelo,
I was able to successfully launch this script and my light on from my raspberry terminal.
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
Re: TP-Link smart plug HS100/HS110
Posted: Wednesday 15 February 2017 19:39
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.
Re: TP-Link smart plug HS100/HS110
Posted: Wednesday 15 February 2017 20:06
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
Re: TP-Link smart plug HS100/HS110
Posted: Wednesday 15 February 2017 20:10
by rebelo
Good job
, enjoy it.
Re: TP-Link smart plug HS110 - send energy data to Domoticz
Posted: Wednesday 22 February 2017 22:23
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¶m=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:
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 (216.54 KiB) Viewed 6538 times
Re: TP-Link smart plug HS100/HS110
Posted: Wednesday 08 March 2017 15:24
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...