forked from Nickduino/Pi-Somfy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
somfy.py
229 lines (184 loc) · 6.94 KB
/
somfy.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import locale
import time
import datetime as dt
import ephem
import pigpio
import traceback
TXGPIO=4 # 433.42 MHz emitter on GPIO 4
#Button values
boutonHaut = 0x2
boutonStop = 0x1
boutonBas = 0x4
frame = bytearray(7)
homeLocation = ephem.Observer()
homeLocation.lat, homeLocation.lon = '42.764457', '5.066435' #PUT YOUR OWN COORDINATES HERE (check on Google Maps for instance)
locale.setlocale(locale.LC_TIME,'')
def envoi_commande(telco, bouton): #Sending a frame
checksum = 0
with open("somfy/" + telco + ".txt", 'r') as file:# the files are un a subfolder "somfy"
data = file.readlines()
teleco = int(data[0], 16)
code = int(data[1])
data[1] = str(code + 1)
print hex(teleco)
print code
with open("somfy/" + telco + ".txt", 'w') as file:
file.writelines(data)
pi = pigpio.pi() # connect to Pi
if not pi.connected:
exit()
pi.wave_add_new()
pi.set_mode(TXGPIO, pigpio.OUTPUT)
print "Remote : " + "0x%0.2X" % teleco
print "Button : " + "0x%0.2X" % bouton
print "Rolling code : " + str(code)
print ""
frame[0] = 0xA7; # Encryption key. Doesn't matter much
frame[1] = bouton << 4 # Which button did you press? The 4 LSB will be the checksum
frame[2] = code >> 8 # Rolling code (big endian)
frame[3] = (code & 0xFF) # Rolling code
frame[4] = teleco >> 16 # Remote address
frame[5] = ((teleco >> 8) & 0xFF) # Remote address
frame[6] = (teleco & 0xFF) # Remote address
print "Frame : ",
for octet in frame:
print "0x%0.2X" % octet,
print ""
for i in range(0, 7):
checksum = checksum ^ frame[i] ^ (frame[i] >> 4)
checksum &= 0b1111; # We keep the last 4 bits only
frame[1] |= checksum;
print "With cks : ",
for octet in frame:
print "0x%0.2X" % octet,
print ""
for i in range(1, 7):
frame[i] ^= frame[i-1];
print "Obfuscated :",
for octet in frame:
print "0x%0.2X" % octet,
print ""
#This is where all the awesomeness is happening. You're telling the daemon what you wana send
wf=[]
wf.append(pigpio.pulse(1<<TXGPIO, 0, 9415))
wf.append(pigpio.pulse(0, 1<<TXGPIO, 89565))
for i in range(2):
wf.append(pigpio.pulse(1<<TXGPIO, 0, 2560))
wf.append(pigpio.pulse(0, 1<<TXGPIO, 2560))
wf.append(pigpio.pulse(1<<TXGPIO, 0, 4550))
wf.append(pigpio.pulse(0, 1<<TXGPIO, 640))
for i in range (0, 56):
if ((frame[i/8] >> (7 - (i%8))) & 1):
wf.append(pigpio.pulse(0, 1<<TXGPIO, 640))
wf.append(pigpio.pulse(1<<TXGPIO, 0, 640))
else:
wf.append(pigpio.pulse(1<<TXGPIO, 0, 640))
wf.append(pigpio.pulse(0, 1<<TXGPIO, 640))
wf.append(pigpio.pulse(0, 1<<TXGPIO, 30415))
#2
for i in range(7):
wf.append(pigpio.pulse(1<<TXGPIO, 0, 2560))
wf.append(pigpio.pulse(0, 1<<TXGPIO, 2560))
wf.append(pigpio.pulse(1<<TXGPIO, 0, 4550))
wf.append(pigpio.pulse(0, 1<<TXGPIO, 640))
for i in range (0, 56):
if ((frame[i/8] >> (7 - (i%8))) & 1):
wf.append(pigpio.pulse(0, 1<<TXGPIO, 640))
wf.append(pigpio.pulse(1<<TXGPIO, 0, 640))
else:
wf.append(pigpio.pulse(1<<TXGPIO, 0, 640))
wf.append(pigpio.pulse(0, 1<<TXGPIO, 640))
wf.append(pigpio.pulse(0, 1<<TXGPIO, 30415))
#2
for i in range(7):
wf.append(pigpio.pulse(1<<TXGPIO, 0, 2560))
wf.append(pigpio.pulse(0, 1<<TXGPIO, 2560))
wf.append(pigpio.pulse(1<<TXGPIO, 0, 4550))
wf.append(pigpio.pulse(0, 1<<TXGPIO, 640))
for i in range (0, 56):
if ((frame[i/8] >> (7 - (i%8))) & 1):
wf.append(pigpio.pulse(0, 1<<TXGPIO, 640))
wf.append(pigpio.pulse(1<<TXGPIO, 0, 640))
else:
wf.append(pigpio.pulse(1<<TXGPIO, 0, 640))
wf.append(pigpio.pulse(0, 1<<TXGPIO, 640))
wf.append(pigpio.pulse(0, 1<<TXGPIO, 30415))
pi.wave_add_generic(wf)
wid = pi.wave_create()
pi.wave_send_once(wid)
while pi.wave_tx_busy():
pass
pi.wave_delete(wid)
pi.stop()
ancienJour = dt.datetime.now().day
varChambreUP, varChambreDOWN, varCuiSdbUP, varScheyeDOWN, varSoirDOWN, varGeneralDOWN, varSS, varMatinUp = (0,)*8
while 1:
if(ancienJour != dt.datetime.now().day):
varChambreUP, varChambreDOWN, varCuiSdbUP, varScheyeDOWN, varSoirDOWN, varGeneralDOWN, varSS, varMatinUp = (0,)*8
ancienJour = dt.datetime.now().day
homeLocation.date = dt.datetime.now().strftime("%Y/%m/%d 00:00:00")
sunrise = ephem.localtime(homeLocation.next_rising(ephem.Sun()))
sunset = ephem.localtime(homeLocation.next_setting(ephem.Sun()))
print "Sunset : " + str(sunset)
for i in range(1, 120):
if(dt.time(8, 00) < dt.datetime.now().time() < dt.time(8, 02)) and varChambreUP == 0:
print "Test Chambre"
try:
envoi_commande("chambre", boutonHaut)
time.sleep(4)
envoi_commande("chambre", boutonStop)
time.sleep(1)
varChambreUP = 1
except:
print "Impossible d'ouvrir chambre"
print traceback.format_exc()
time.sleep(15)
if(dt.time(8, 05) < dt.datetime.now().time() < dt.time(8, 07)) and varMatinUp == 0:
print "Cuisine UP"
try:
envoi_commande("cuisine", boutonHaut)
time.sleep(1)
envoi_commande("sdb", boutonHaut)
time.sleep(1)
envoi_commande("salon", boutonHaut)
time.sleep(1)
varMatinUp = 1
except:
print "Impossible d'ouvrir cuisine, salon & sdb"
print traceback.format_exc()
time.sleep(15)
if((sunset + dt.timedelta(minutes=45)) < (dt.datetime.now()) < (sunset + dt.timedelta(minutes=48))) and varSoirDOWN == 0:
print "Salon à " + str(dt.datetime.now().time())
try:
envoi_commande("salon", boutonBas)
time.sleep(1)
envoi_commande("ss", boutonBas)
time.sleep(1)
envoi_commande("chambre", boutonBas)
time.sleep(1)
varSoirDOWN = 1
except:
print "Impossible de fermer salon++"
print traceback.format_exc()
time.sleep(15)
if(dt.time(21, 00) < dt.datetime.now().time()) and (sunset + dt.timedelta(minutes=60)) < (dt.datetime.now()) and varGeneralDOWN == 0:
print "Fermeture générale à " + str(dt.datetime.now().time())
try:
envoi_commande("salon", boutonBas)
time.sleep(1)
envoi_commande("cuisine", boutonBas)
time.sleep(1)
envoi_commande("sdb", boutonBas)
time.sleep(1)
envoi_commande("chambre", boutonBas)
time.sleep(1)
envoi_commande("ss", boutonBas)
time.sleep(1)
varGeneralDOWN = 1
except:
print "Impossible de fermer le général"
print traceback.format_exc()
time.sleep(15)
time.sleep(30)