-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduino.ino
340 lines (288 loc) · 8.65 KB
/
Arduino.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Not Tested Just a Example
// INCLUDE
#include <Ethernet.h>
#include <SPI.h>
#include <RCSwitch.h>
#include <NewRemoteTransmitter.h>
boolean reading = false;
boolean readinglamp = false;
boolean readingvalue = false;
boolean reading2 = false;
// DECLARE VARIABLES
unsigned long currentMillis;
unsigned long previousMillis; //
// String PushSubject = "INPUT"; // For reading webserver input
// 433MHZ Switch Lights on I/O 6
NewRemoteTransmitter transmitter(99, 6, 260, 1);
RCSwitch mySwitch = RCSwitch();
// Define the OLD Remote Group Switches and the two Switche codes I use
char RCGroup [] = "11100";
char RCOutput1 [] = "01100";
char RCOutput2 []= "11100";
int RCoutputstate = 00;
int RCRetry = 1; // Also in the Switch void
int lamp = 0;
int lampold = 0;
int lampvar = 0;
int value = 0;
int level = 0;
int levelvar = 0;
int setlevel = 0;
// Setting Network Values for manual setup
//byte ip[] = { 192, 168, x, x }; //Manual setup only
//byte gateway[] = { 192, 168, x, x }; //Manual setup only
//byte subnet[] = { 255, 255, 255, 0 }; //Manual setup only
// if need to change the MAC address (Very Rare)
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetServer server = EthernetServer(80); //port 80
char inString[32]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?
// SETUP
void setup() {
// Serialport
Serial.begin(9600);
// delay (10);
// Serial.println("Serial Init");
// STARTUP
netstart(); // Initialise Ethernet
Serial.println("Startup complete - Running Code");
// 433MHZ SWITCH
mySwitch.enableTransmit(6);
}
void netstart()
{
//Ethernet.begin(mac, ip, gateway, subnet); //for manual setup
if (Ethernet.begin(mac) == 0) {
Serial.println("Ethernet Connection Failed");
} else {
//Start webserver
server.begin();
delay(1000);
Serial.print("IP ADRESS: ");
Serial.println(Ethernet.localIP());
}
}
/// Routine
void loop() {
CheckForClient(); // listen for incoming clients on webserver, and process url variable request.
}
void Lamp() {
if (level >= 1 && level <= 99) {
Serial.print("Dimming lamp ");
Serial.print(lamp);
Serial.print(" ");
Serial.print(level);
Serial.println("%");
if (level <= 7) {
setlevel = 1;
} else {
if (level >= 13 && level <=19){
setlevel = 2;
} else {
if (level >= 20 && level <=25){
setlevel = 3;
} else {
if (level >= 26 && level <=32){
setlevel = 4;
} else {
if (level >= 33 && level <=39){
setlevel = 5;
} else {
if (level >= 40 && level <=45){
setlevel = 6;
} else {
if (level >= 46 && level <=52){
setlevel = 7;
} else {
if (level >= 53 && level <=59){
setlevel = 8;
} else {
if (level >= 60 && level <=65){
setlevel = 9;
} else {
if (level >= 66 && level <=72){
setlevel = 10;
}else {
if (level >= 73 && level <=79){
setlevel = 11;
} else {
if (level >= 80 && level <=85){
setlevel = 12;
} else {
if (level >= 86 && level <=92){
setlevel = 13;
} else {
if (level >= 93 && level <=99){
setlevel = 14;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
transmitter.sendDim(lamp, setlevel);
}else {
switch (level) {
case 101:
Serial.print("Switching on lamp ");
Serial.println(lamp);
transmitter.sendUnit(lamp, true);
break;
case 100:
if (level == 100) {transmitter.sendDim(lamp, 15);}
Serial.print("Switching on lamp ");
Serial.print(lamp);
Serial.println(" 100%");
break;
case 0:
Serial.print("Switching off lamp ");
Serial.println(lamp);
transmitter.sendUnit(lamp, false);
break;
}
}
lamp = 0;
level = 0;
setlevel = 0;
}
}
void Switch() {
while (RCRetry > 0){
switch (RCoutputstate) {
case 11:
mySwitch.switchOn(RCGroup, RCOutput1);
Serial.println ("oude KAKU 1 Aan");
break;
case 10:
mySwitch.switchOff(RCGroup, RCOutput1);
Serial.println ("oude KAKU 1 uit");
break;
case 21:
mySwitch.switchOn(RCGroup, RCOutput2);
Serial.println ("oude KAKU 2 Aan");
break;
case 20:
mySwitch.switchOff(RCGroup, RCOutput2);
Serial.println ("oude KAKU 2 uit");
break;
}
RCRetry--;
}
RCRetry = 1;
}
}
void CheckForClient() {
EthernetClient client = server.available();
if (client) {
} else {
Lamp();
lamp = 0;
level = 0;
}
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
boolean sentHeader = false;
while (client.connected()) {
if (client.available()) {
if (!sentHeader) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
sentHeader = true;
}
char c = client.read();
if (reading && c == ' ') reading = false;
if (c == '?') reading = true;//found the ?, begin reading the info
if (reading2 && c == ' ') reading2 = false;
if (c == '!') reading2 = true; //found the !, begin reading the info
if (reading2) {
// Serial.print ("Licht Input");
// Serial.print(c);
switch (c) {
case 'L':
// Serial.print("Lamp");
c = client.read();
lampvar = ((c-48) * 100);
c = client.read();
lampvar = (lampvar + ((c-48) * 10));
c = client.read();
lampvar = (lampvar + (c-48));
// Serial.print (lampvar);
break;
case 'V':
// Serial.print(" Value ");
value = 0;
c = client.read();
value = (value + ((c-48) * 100));
c = client.read();
value = (value + ((c-48) * 10));
c = client.read();
if (c == ' '){
levelvar = (value / 10);
} else {
levelvar = (value + ((c-48) * 1));
}
// Serial.println (level);
if (lamp != 0 && lamp !=lampvar){
// Serial.println ("New Lamp, 1st one first!");
//Run Script to turn change old lamp
Lamp();
lamp = lampvar;
level = levelvar;
Lamp();
} else {
lamp = lampvar;
level = levelvar;
}
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection:
break;
}
}
if (reading) {
// Serial.print(c);
switch (c) {
case 'A':
RCoutputstate = 11;
Switch();
break;
case 'B':
RCoutputstate = 10;
Switch();
break;
case 'C':
RCoutputstate = 21;
Switch();
break;
case 'D':
RCoutputstate = 20;
Switch();
break;
}
}
if (c == '\n' && currentLineIsBlank) break;
if (c == '\n') {
currentLineIsBlank = true;
} else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection:
// Serial.println (" Close");
delay (70);
}
}