I am receiving similar errors to MnM001 and have been spinning my wheels on it for a couple of days now:
Traceback (most recent call last):
File "./tplink-smartplug-HS110.py", line 60, in <module>
jsonData = json.loads(result)
File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
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 = '10.1.1.5'
port = 9999
cmd = '{"emeter":{"get_realtime":{}}}'
# Domoticz command stub and IDx of HS110
baseURL = 'http://<10.1.1.5:8080>/json.htm?type=command¶m=udevice&nvalue=0'
HSIdx = 2
# 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
Electricity (instant and counter)
/json.htm?type=command¶m=udevice&idx=IDX&nvalue=0&svalue=POWER;ENERGY
........however others including yourself have this working with your code so im not sure where I am going wrong. I have messed around with the IP & port but no luck. I can turn the plug on\off using the another HSxxx script (tplink-smartplug.py).
Please Help!!!