-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathswitch.py
80 lines (71 loc) · 2.23 KB
/
switch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/python
#
# Make the control a Belkin WeMo
#
# Poll the tactile switch, when pressed, turn the WeMo on.
# When pressed again, turn it off. Lather, rinse, repeat.
#
import subprocess, time, os, socket
switch_gpio = """/sys/class/gpio/gpio0/value"""
soapHeader = """POST /upnp/control/basicevent1 HTTP/1.1
SOAPAction: "urn:Belkin:service:basicevent:1#SetBinaryState"
Host: 192.168.0.105:49153
Content-Type: text/xml
Content-Length: """
soapRequestOn = """<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<m:SetBinaryState xmlns:m="urn:Belkin:service:basicevent:1">
<BinaryState>1</BinaryState>
</m:SetBinaryState>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>"""
soapRequestOff = """<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<m:SetBinaryState xmlns:m="urn:Belkin:service:basicevent:1">
<BinaryState>0</BinaryState>
</m:SetBinaryState>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>"""
def send_soap(host, port, soapRequest):
sh = soapHeader + "%s\r\n\r\n" % len(soapRequest)
fullRequest = sh + soapRequest
#print "send_soap %s" % fullRequest
#Send data and run away
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect((host,port))
sock.send(fullRequest)
data = sock.recv(8192) # Get the response
sock.close()
#print "Reply: %s" % data # And print it
def read_switch():
fd = open(switch_gpio, "r")
val = fd.read()
fd.close()
if val[:1] == "1":
return False
else:
return True
def change_state(new_state):
if (new_state):
print "On"
send_soap("192.168.0.105", 49153, soapRequestOn)
else:
print "Off"
send_soap("192.168.0.105", 49153, soapRequestOff)
if __name__ == '__main__':
curr_state = False
flip_flop = False
while 1:
switch_state = read_switch()
#print switch_state
if (curr_state == False) and (switch_state == True):
if (flip_flop):
flip_flop = False
else:
flip_flop = True
change_state(flip_flop)
curr_state = switch_state
time.sleep(.1)
#host send X controllee basicevent SetBinaryState