-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathControlShade.ino
314 lines (271 loc) · 7.16 KB
/
ControlShade.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include <WiFi.h>
// up, hold, down, led
byte output[4] = {16, 17, 18, 19};
// window shade 0, shade 1, shade 2, led
byte state[4] = {100, 100, 100, 0};
// time to move the shade from up to down
const float TIME = 20000.0;
const short LED_TIMEOUT = 4000;
const short HIGH_PHASE = 100;
byte action = 255;
byte a = 255;
byte b = 255;
byte c = 255;
// Wifi variables
const char* ssid = "";
const char* password = "";
byte wifi_retry = 0;
// variable for LDR
byte ldrPin = 34;
short val;
WiFiServer server(8448);
void setup() {
Serial.begin(115200);
// initialize output ports
for(byte i=0; i<4; i++) {
pinMode(output[i], OUTPUT);
digitalWrite(output[i], LOW);
}
// configure analog input
analogReadResolution(12);
// setup the TCP socket
delay(100);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
checkWifiConnect();
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop() {
// check if WIFI ist still connected
checkWifiConnect();
// control through server
WiFiClient client = server.available();
if (client) {
Serial.println("New Client.");
client.write("shade");
// check which action is requested
while(client.connected()) {
if (client.available() > 0) {
action = client.read();
break;
} else {
delay(100);
}
}
Serial.println(action);
if(action == 0) {
while (client.connected()) {
if (client.available() >= 3) {
a = client.read();
Serial.print("a: ");
Serial.println(a);
b = client.read();
Serial.print("b: ");
Serial.println(b);
c = client.read();
Serial.print("c: ");
Serial.println(c);
break;
}
}
client.write(byte(0));
Serial.println("Client Disconnected.");
if(a != 255 && b != 255 && c != 255) {
control(a, b, c);
Serial.println("Finished");
a = 255;
b = 255;
c = 255;
}
} else if (action == 1) {
client.write(state, 3);
}
client.stop();
action = 255;
Serial.println("Client disconnected");
}
// control through LDR
val = analogRead(ldrPin);
if(val < 800 && state[0] == 0 && state[1] == 20 && state[2] == 0) {
control(99, 100, 100);
Serial.println("Finished");
}
}
/**
* Checks if wifi is connected and connect if it's
* not connected.
*/
void checkWifiConnect() {
while(WiFi.status() != WL_CONNECTED) {
WiFi.disconnect();
Serial.println("Try connecting to wifi");
// connect with wifi
WiFi.begin(ssid, password);
delay(500);
wifi_retry = 0;
// waiting for reconnection
while(WiFi.status() != WL_CONNECTED) {
wifi_retry++;
if(wifi_retry == 10) {
break;
}
Serial.print(".");
delay(500);
}
Serial.println();
}
}
/*
* Move all window shades in the given position.
*/
void control(byte a, byte b, byte c) {
// calculate the distance in which the window shade has to be moved
short in[3] = {a - state[0], b - state[1], c - state[2]};
for(byte j=0; j<3; j++) {
Serial.print("Windows: ");
Serial.print(j);
Serial.print(" state: ");
Serial.print(state[j]);
Serial.print(" down: ");
Serial.println(in[j]);
}
// sort elements from highest to lowest
byte con[3];
if(((abs(in[0]) <= abs(in[1]) && abs(in[0]) <= abs(in[2])) || a == 100 || a == 0) && b != 100 && b != 0 && c != 100 && c != 0) {
con[0] = 0;
if(((abs(in[1]) <= abs(in[2])) || b == 100) && c != 100 && c != 0) {
con[1] = 1;
con[2] = 2;
} else {
con[1] = 2;
con[2] = 1;
}
}
else if(((abs(in[1]) <= abs(in[0]) && abs(in[1]) <= abs(in[2])) || b == 100 || b == 0) && c != 100 && c != 0) {
con[0] = 1;
if(((abs(in[0]) <= abs(in[2])) || a == 100) && c != 100 && c != 0) {
con[1] = 0;
con[2] = 2;
} else {
con[1] = 2;
con[2] = 0;
}
}
else if((abs(in[2]) <= abs(in[0]) && abs(in[2]) <= abs(in[1])) || c == 100 || c == 0) {
con[0] = 2;
if(((abs(in[1]) <= abs(in[0])) || b == 100) && a != 100 && a != 0) {
con[1] = 1;
con[2] = 0;
} else {
con[1] = 0;
con[2] = 1;
}
}
// move all window shades in the given position
for(byte i=0; i < 3; i++) {
controlOne(con[i], in[con[i]]);
}
}
/*
* Moves one window shade in the given position.
*/
void controlOne(byte shade, short down) {
if((state[shade] + down) == 100 && down != 0) {
// switch controlle mode to the current window
toLEDState(shade);
// move shade fully down
moveUpOrDown(2, down, shade);
} else if((state[shade] + down) == 0 && down != 0) {
// switch controlle mode to the current window
toLEDState(shade);
// move shade fully up
moveUpOrDown(0, down, shade);
} else if(down > 0 && abs(down) > 10) {
// switch controlle mode to the current window
toLEDState(shade);
// move shade in given position
moveShade(2, down, shade);
} else if (down < 0 && abs(down) > 10) {
// switch controlle mode to the current window
toLEDState(shade);
// move shade in given position
moveShade(0, down, shade);
}
}
/*
* Control the remote to move the window shade
* to a given position.
*/
void moveShade(byte d, short down, byte shade) {
int delay_time = delayTime(down, state[shade]);
Serial.print("Window: ");
Serial.print(shade);
Serial.print(" move. Time: ");
Serial.print(delay_time);
Serial.print(" Direction: ");
Serial.println(down);
digitalWrite(output[d], HIGH);
delay(HIGH_PHASE);
digitalWrite(output[d], LOW);
delay(delay_time);
digitalWrite(output[1], HIGH);
delay(HIGH_PHASE);
digitalWrite(output[1], LOW);
state[shade] += down;
delay(LED_TIMEOUT);
}
/*
* Move a window shade fully up or down.
*/
void moveUpOrDown(byte d, short down, byte shade) {
Serial.print("Window: ");
Serial.print(shade);
Serial.println(" fully");
digitalWrite(output[d], HIGH);
delay(HIGH_PHASE);
digitalWrite(output[d], LOW);
state[shade] += down;
delay(LED_TIMEOUT);
}
/*
* Calculates the time the window shade has to move.
* Its not a linear process.
*/
int delayTime(short down, byte s) {
int t = (abs(down)/100.0) * TIME;
if(s == 100) {
t += 10000;
}
return t;
}
/*
* Switches the remote state to the state given
* by s.
*/
void toLEDState(byte s) {
Serial.print("\tLED state: ");
Serial.println(state[3]);
Serial.print("\tShade: ");
Serial.println(s);
if(state[3] != s) {
digitalWrite(output[3], HIGH);
delay(HIGH_PHASE);
digitalWrite(output[3], LOW);
delay(200);
while(state[3] != s) {
digitalWrite(output[3], HIGH);
delay(HIGH_PHASE);
digitalWrite(output[3], LOW);
delay(200);
state[3] = (state[3] + 1) % 6;
Serial.print("LED state: ");
Serial.println(state[3]);
}
}
}