Hallo liebe Forenmitglieder,
habe bei mir mehrere Steckdosen KP105 mit den Raspberry am laufen. Das funktioniert auch soweit ganz gut.
Die Statusabfrage wird über 2 Python Scripte realisiert.
tplink_smartplug.py
import sys
import socket
import json
from struct import pack
version = 0.3
# Predefined Smart Plug Commands
# For a full list of commands, consult tplink_commands.txt
commands = {'info': '{"system":{"get_sysinfo":{}}}',
'on': '{"system":{"set_relay_state":{"state":1}}}',
'off': '{"system":{"set_relay_state":{"state":0}}}',
'ledoff': '{"system":{"set_led_off":{"off":1}}}',
'ledon': '{"system":{"set_led_off":{"off":0}}}',
'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":{}}}'
}
# Encryption and Decryption of TP-Link Smart Home Protocol
# XOR Autokey Cipher with starting key = 171
# Python 3.x Version
if sys.version_info[0] > 2:
def encrypt(string):
key = 171
result = pack('>I', len(string))
for i in string:
a = key ^ ord(i)
key = a
result += bytes([a])
return result
def decrypt(string):
key = 171
result = ""
for i in string:
a = key ^ i
key = i
result += chr(a)
return result
# Python 2.x Version
else:
def encrypt(string):
key = 171
result = pack('>I', len(string))
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 send(ip, port, command):
cmd = commands[command]
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()
decrypted = decrypt(data[4:])
return json.loads(decrypted)
except:
print ("Cound not connect to host " + ip + ":" + str(port))
Display More
Auslesen.py
#!/usr/bin/env python3
import tplink_smartplug
# Auslesen der einzelnen HS100 Steckdosen
# Kachelofen
result51 = tplink_smartplug.send("192.168.10.51", 9999, "info")
relay_state51 = result51['system']['get_sysinfo']['relay_state']
if (relay_state51):
output51 = '1'
else:
output51 = '0'
print (output51)
# Küche
result50 = tplink_smartplug.send("192.168.10.50", 9999, "info") # Steckdose links
result52 = tplink_smartplug.send("192.168.10.52", 9999, "info") # Steckdose rechts
relay_state50 = result50['system']['get_sysinfo']['relay_state']
relay_state52 = result52['system']['get_sysinfo']['relay_state']
if relay_state50 == 1 and relay_state52 == 1:
output00 = 1
elif relay_state50 == 0 and relay_state52 == 0:
output00 = 0
elif relay_state50 == 1 and relay_state52 == 0 or relay_state50 == 0 and relay_state52 == 1:
output00 = 9
print (output00)
# Wohnzimmer Lampe
result56 = tplink_smartplug.send("192.168.10.56", 9999, "info")
relay_state56 = result56['system']['get_sysinfo']['relay_state']
if (relay_state56):
output56 = '1'
else:
output56 = '0'
print (output56)
# Raspberry Monitor ( Smart Home )
result54 = tplink_smartplug.send("192.168.10.54", 9999, "info")
relay_state54 = result54['system']['get_sysinfo']['relay_state']
if (relay_state54):
output54 = '1'
else:
output54 = '0'
print (output54)
# Raspberry Aquarium
result57 = tplink_smartplug.send("192.168.10.57", 9999, "info")
relay_state57 = result57['system']['get_sysinfo']['relay_state']
if (relay_state57):
output57 = '1'
else:
output57 = '0'
print (output57)
Display More
Das Problem was ich habe, ist wenn eine Steckdose nicht erreichbar ist, das die Abfrage unterbrochen wird. Das Script tplink_smartplug.py gibt zwar wenn ich die Abfrage über der Console mache die Fehlermeldung print ("Cound not connect to host " + ip + ":" + str(port)) aus.
Um auf meiner Webseite zu erkennen das eine Steckdose ausgefallen ist, benötige ich im Script Auslesen.py den Wert "99" in der Variable outputxx.
Das ist das Problem was ich im Moment habe.
Was muss ich ändern, habe schon versucht das zu realisieren. Aber leider habe ich noch zu wenig mit Python bis jetzt zu tun gehabt.