forked from Nickduino/Somfy_Remote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Somfy_Remote.ino
190 lines (161 loc) · 5.53 KB
/
Somfy_Remote.ino
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
/* This sketch allows you to emulate a Somfy RTS or Simu HZ remote.
If you want to learn more about the Somfy RTS protocol, check out https://pushstack.wordpress.com/somfy-rts-protocol/
The rolling code will be stored in EEPROM, so that you can power the Arduino off.
Easiest way to make it work for you:
- Choose a remote number
- Choose a starting point for the rolling code. Any unsigned int works, 1 is a good start
- Upload the sketch
- Long-press the program button of YOUR ACTUAL REMOTE until your blind goes up and down slightly
- send 'p' to the serial terminal
To make a group command, just repeat the last two steps with another blind (one by one)
Then:
- m, u or h will make it to go up
- s make it stop
- b, or d will make it to go down
- you can also send a HEX number directly for any weird command you (0x9 for the sun and wind detector for instance)
*/
#include <EEPROM.h>
#define PORT_TX 5 //5 of PORTD = DigitalPin 5
#define SYMBOL 640
#define HAUT 0x2
#define STOP 0x1
#define BAS 0x4
#define PROG 0x8
#define EEPROM_ADDRESS 0
#define REMOTE 0x121300 //<-- Change it!
unsigned int newRollingCode = 101; //<-- Change it!
unsigned int rollingCode = 0;
byte frame[7];
byte checksum;
void BuildFrame(byte *frame, byte button);
void SendCommand(byte *frame, byte sync);
void setup() {
Serial.begin(115200);
DDRD |= 1<<PORT_TX; // Pin 5 an output
PORTD &= !(1<<PORT_TX); // Pin 5 LOW
if (EEPROM.get(EEPROM_ADDRESS, rollingCode) < newRollingCode) {
EEPROM.put(EEPROM_ADDRESS, newRollingCode);
}
Serial.print("Simulated remote number : "); Serial.println(REMOTE, HEX);
Serial.print("Current rolling code : "); Serial.println(rollingCode);
}
void loop() {
if (Serial.available() > 0) {
char serie = (char)Serial.read();
Serial.println("");
// Serial.print("Remote : "); Serial.println(REMOTE, HEX);
if(serie == 'm'||serie == 'u'||serie == 'h') {
Serial.println("Monte"); // Somfy is a French company, after all.
BuildFrame(frame, HAUT);
}
else if(serie == 's') {
Serial.println("Stop");
BuildFrame(frame, STOP);
}
else if(serie == 'b'||serie == 'd') {
Serial.println("Descend");
BuildFrame(frame, BAS);
}
else if(serie == 'p') {
Serial.println("Prog");
BuildFrame(frame, PROG);
}
else {
Serial.println("Custom code");
BuildFrame(frame, serie);
}
Serial.println("");
SendCommand(frame, 2);
for(int i = 0; i<2; i++) {
SendCommand(frame, 7);
}
}
}
void BuildFrame(byte *frame, byte button) {
unsigned int code;
EEPROM.get(EEPROM_ADDRESS, code);
frame[0] = 0xA7; // Encryption key. Doesn't matter much
frame[1] = button << 4; // Which button did you press? The 4 LSB will be the checksum
frame[2] = code >> 8; // Rolling code (big endian)
frame[3] = code; // Rolling code
frame[4] = REMOTE >> 16; // Remote address
frame[5] = REMOTE >> 8; // Remote address
frame[6] = REMOTE; // Remote address
Serial.print("Frame : ");
for(byte i = 0; i < 7; i++) {
if(frame[i] >> 4 == 0) { // Displays leading zero in case the most significant
Serial.print("0"); // nibble is a 0.
}
Serial.print(frame[i],HEX); Serial.print(" ");
}
// Checksum calculation: a XOR of all the nibbles
checksum = 0;
for(byte i = 0; i < 7; i++) {
checksum = checksum ^ frame[i] ^ (frame[i] >> 4);
}
checksum &= 0b1111; // We keep the last 4 bits only
//Checksum integration
frame[1] |= checksum; // If a XOR of all the nibbles is equal to 0, the blinds will
// consider the checksum ok.
Serial.println(""); Serial.print("With checksum : ");
for(byte i = 0; i < 7; i++) {
if(frame[i] >> 4 == 0) {
Serial.print("0");
}
Serial.print(frame[i],HEX); Serial.print(" ");
}
// Obfuscation: a XOR of all the bytes
for(byte i = 1; i < 7; i++) {
frame[i] ^= frame[i-1];
}
Serial.println(""); Serial.print("Obfuscated : ");
for(byte i = 0; i < 7; i++) {
if(frame[i] >> 4 == 0) {
Serial.print("0");
}
Serial.print(frame[i],HEX); Serial.print(" ");
}
Serial.println("");
Serial.print("Rolling Code : "); Serial.println(code);
EEPROM.put(EEPROM_ADDRESS, code + 1); // We store the value of the rolling code in the
// EEPROM. It should take up to 2 adresses but the
// Arduino function takes care of it.
}
void SendCommand(byte *frame, byte sync) {
if(sync == 2) { // Only with the first frame.
//Wake-up pulse & Silence
PORTD |= 1<<PORT_TX;
delayMicroseconds(9415);
PORTD &= !(1<<PORT_TX);
delayMicroseconds(89565);
}
// Hardware sync: two sync for the first frame, seven for the following ones.
for (int i = 0; i < sync; i++) {
PORTD |= 1<<PORT_TX;
delayMicroseconds(4*SYMBOL);
PORTD &= !(1<<PORT_TX);
delayMicroseconds(4*SYMBOL);
}
// Software sync
PORTD |= 1<<PORT_TX;
delayMicroseconds(4550);
PORTD &= !(1<<PORT_TX);
delayMicroseconds(SYMBOL);
//Data: bits are sent one by one, starting with the MSB.
for(byte i = 0; i < 56; i++) {
if(((frame[i/8] >> (7 - (i%8))) & 1) == 1) {
PORTD &= !(1<<PORT_TX);
delayMicroseconds(SYMBOL);
PORTD ^= 1<<5;
delayMicroseconds(SYMBOL);
}
else {
PORTD |= (1<<PORT_TX);
delayMicroseconds(SYMBOL);
PORTD ^= 1<<5;
delayMicroseconds(SYMBOL);
}
}
PORTD &= !(1<<PORT_TX);
delayMicroseconds(30415); // Inter-frame silence
}