Hi Marc,
I'm not a Python expert by any means but I will have a go. There's a good tutorial on KeyError here:
https://airbrake.io/blog/python-excepti ... n-keyerrorthe KeyError is raised when accessing an invalid key within a dict.
The dict setup for us is the code below, so we can be fairly sure the problem is in that area.
Code: Select all
commands = {
'info' : '{"system":{"get_sysinfo":{}}}',
'on' : '{"system":{"set_relay_state":{"state":1}}}',
'off' : '{"system":{"set_relay_state":{"state":0}}}',
'led_on' : '{"system":{"set_led_off":{"off":0}}}',
'led_off' : '{"system":{"set_led_off":{"off":1}}}',
'state' : '{"system":{"get_sysinfo":{}}}',
'cloudinfo': '{"cnCloud":{"get_info":{}}}',
'wlanscan' : '{"netif":{"get_scaninfo":{"refresh":0}}}',
'time' : '{"time":{"get_time":{}}}',
'schedule' : '{"schedule":{"get_rules":{}}}',
'countdown': '{"count_down":{"get_rules":{}}}',
'antitheft': '{"anti_theft":{"get_rules":{}}}',
'reboot' : '{"system":{"reboot":{"delay":1}}}',
'reset' : '{"system":{"reset":{"delay":1}}}',
'energy' : '{"emeter":{"get_realtime":{}}}'
}
TABS show as (->) and spaces as dots (.) when Show all Characters is turned on in Notepad++. I also have the Tab Settings set as 'Replace by space' in Notepad++ Preferences, so that TABs are not automatically added. If you checked and there are no TABS in this section instead of spaces, we need to debug a bit further.
Firstly I would try pasting the above dict code block directly over yours, just in case you are missing a minor formatting issue.
If that doesn't help, we can add an extra debugging line after json_data = (my line 146) to see what is coming through:
Code: Select all
json_data = json.loads(received_data)
logger.debug("json_data: {}".format(json_data))
I see you have debug_level="DEBUG", which is the best option for catching problems. In addition to any error that may appear in the console, we can also look at the log file, as you have done. It is saved in the same folder as the program. (in my case HS110-1.log). For one power logging circuit, mine shows the following:
2018-09-15 11:53:23,780:__main__:DEBUG:Command: {"emeter":{"get_realtime":{}}}
2018-09-15 11:53:23,885:__main__:DEBUG:Sent: {"emeter":{"get_realtime":{}}}
2018-09-15 11:53:23,886:__main__:DEBUG:Received: {"emeter":{"get_realtime":{"voltage_mv":242153,"current_ma":14,"power_mw":0,"total_wh":2,"err_code":0}}}
2018-09-15 11:53:23,887:__main__:DEBUG:json_data: {u'emeter': {u'get_realtime': {u'total_wh': 2, u'current_ma': 14, u'power_mw': 0, u'voltage_mv': 242153, u'err_code': 0}}}
2018-09-15 11:53:23,887:__main__:DEBUG:IDX: 90, Item: voltage, Value: 242.15
2018-09-15 11:53:23,888:__main__:DEBUG:URL:
http://rpi3:8080/json.htm?type=command& ... lue=242.15
2018-09-15 11:53:23,895:__main__:DEBUG:IDX: 91, Item: current, Value: 0.01
2018-09-15 11:53:23,896:__main__:DEBUG:URL:
http://rpi3:8080/json.htm?type=command& ... value=0.01
2018-09-15 11:53:23,903:__main__:DEBUG:IDX: 108, Item: power, Value: 0.0
2018-09-15 11:53:23,904:__main__:DEBUG:URL:
http://rpi3:8080/json.htm?type=command& ... svalue=0.0
2018-09-15 11:53:23,910:__main__:DEBUG:IDX: 93, Items: Power (W), Usage (kWhr), Values: 0.0;0.002
2018-09-15 11:53:23,911:__main__:DEBUG:URL:
http://rpi3:8080/json.htm?type=command& ... ue=0.0;2.0
There's nothing plugged in, so the current and power readings are very low.
Also note my software and hardware versions are different from yours, which could suggest different firmware, although I'm a bit unsure about the dates (mine looks earlier):
2018-09-15 11:53:08,757:__main__:DEBUG:Received: {"system":{"get_sysinfo":{"sw_ver":"1.2.10 Build 170828 Rel.104625","hw_ver":"2.0","type":"IOT.SMARTPLUGSWITCH","model":"HS110(AU)","mac":"xx:84:C6:3D:C0:xx","dev_name":"Smart Wi-Fi Plug With Energy Monitoring","alias":"TP-Link HS110 #1","relay_state":1,"on_time":5894,"active_mode":"none","feature":"TIM:ENE","updating":0,"icon_hash":"","rssi":-70,"led_off":0,"longitude_i":xx,"latitude_i":xx,"hwId":"A28C8BB92AFCB6CAFB83A8C00145F7E2","fwId":"00000000000000000000000000000000","deviceId":"8006B1553E3288EB95B589531A1EF88519FD5012","oemId":"6480C2101948463DC65D7009CAECDECC","err_code":0}}}
Good luck, I'm sure you're almost there! Please let us know either way.
Cheers - Andrew