-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arduino AutoHome Code
299 lines (219 loc) · 9.27 KB
/
Arduino AutoHome Code
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
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html
//for use with W5100 based ethernet shields
// this project is hosted at
// http://code.google.com/p/arduino-autohome/
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x78, 0xE0 }; // <------- PUT YOUR MAC Address Here
byte ip[] = { 192, 168, 1, 102 }; // <------- PUT YOUR IP Address Here
byte gateway[] = { 192, 168, 1, 254 }; // <------- PUT YOUR ROUTERS IP Address to which your shield is connected Here
byte subnet[] = { 255, 255, 255, 0 }; // <------- It will be as it is in most of the cases
EthernetServer server(80); // <------- It's Defaulf Server Port for Ethernet Shield
String readString;
//////////////////////
void setup()
{
pinMode(6, OUTPUT); // Pin Assignment through which relay will be controlled
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
//enable serial data print
Serial.begin(9600);
Serial.println("server LED test 1.0"); // so that we can know what is getting loaded
}
void loop()
{
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}
//if HTTP request has ended
if (c == '\n') {
///////////////
Serial.println(readString); //print to serial monitor for debuging
/* Start OF HTML Section. Here Keep everything as it is unless you understands its working */
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
//client.println("<meta http-equiv=\"refresh\" content=\"5\">");
client.println("<HTML>");
client.println("<HEAD>");
client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
client.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"http://arduino-autohome.googlecode.com/svn/trunk/autohome.css\" />");
client.println("</HEAD>");
client.println("<body bgcolor=\"#D0D0D0\">");
client.println("<hr/>");
client.println("<hr/>");
client.println("<h4><center><img border=\"2\" src=\"https://lh3.googleusercontent.com/-C6BoJrRUFko/UEUFeCwkvdI/AAAAAAAAAOc/E7gcYvPV6r4/s960/Logo.jpg\" /></center></h4>");
client.println("<hr/>");
client.println("<hr/>");
client.println("<br />");
client.println("<br />");
client.println("<br />");
client.println("<br />");
client.println("<br />");
// Relay Control Code
client.println("<a href=\"/?relay1on\"\">Turn On Light 1</a>");
client.println("<a href=\"/?relay1off\"\">Turn Off Light 1</a><br />");
client.println("<br />");
client.println("<br />");
client.println("<br />");
client.println("<br />");
client.println("<a href=\"/?relay2on\"\">Turn On Light 2</a>");
client.println("<a href=\"/?relay2off\"\">Turn Off Light 2</a><br />");
client.println("<br />");
client.println("<br />");
client.println("<br />");
client.println("<br />");
client.println("<a href=\"/?relay3on\"\">Turn On Light 3</a>");
client.println("<a href=\"/?relay3off\"\">Turn Off Light 3</a><br />");
client.println("<br />");
client.println("<br />");
client.println("<br />");
client.println("<br />");
client.println("<a href=\"/?relay4on\"\">Turn On Light 4</a>");
client.println("<a href=\"/?relay4off\"\">Turn Off Light 4</a><br />");
client.println("<br />");
client.println("<br />");
// control arduino pin via ethernet Start //
if(readString.indexOf("?relay1on") >0)//checks for on
{
digitalWrite(6, HIGH); // set pin 4 high
Serial.println("Led On");
client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");
//client.println("Light 1 Is On");
client.println("<br />");
}
else{
if(readString.indexOf("?relay1off") >0)//checks for off
{
digitalWrite(6, LOW); // set pin 4 low
Serial.println("Led Off");
client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");
//client.println("Light 1 Is Off");
client.println("<br />");
}
}
if(readString.indexOf("?relay2on") >0)//checks for on
{
digitalWrite(7, HIGH); // set pin 4 high
Serial.println("Led On");
client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");
//client.println("Light 2 Is On");
client.println("<br />");
}
else{
if(readString.indexOf("?relay2off") >0)//checks for off
{
digitalWrite(7, LOW); // set pin 4 low
Serial.println("Led Off");
client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");
//client.println("Light 2 Is Off");
client.println("<br />");
}
}
if(readString.indexOf("?relay3on") >0)//checks for on
{
digitalWrite(8, HIGH); // set pin 4 high
Serial.println("Led On");
client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");
// client.println("Light 3 Is On");
client.println("<br />");
}
else{
if(readString.indexOf("?relay3off") >0)//checks for off
{
digitalWrite(8, LOW); // set pin 4 low
Serial.println("Led Off");
client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");
//client.println("Light 3 Is Off");
client.println("<br />");
}
}
if(readString.indexOf("?relay4on") >0)//checks for on
{
digitalWrite(9, HIGH); // set pin 4 high
Serial.println("Led On");
client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");
//client.println("Light 4 Is On");
client.println("<br />");
}
else{
if(readString.indexOf("?relay4off") >0)//checks for off
{
digitalWrite(9, LOW); // set pin 4 low
Serial.println("Led Off");
client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");
//client.println("Light 4 Is Off");
client.println("<br />");
}
}
// control arduino pin via ethernet End //
// Relay Status Display
client.println("<center>");
client.println("<table border=\"5\">");
client.println("<tr>");
if (digitalRead(6))
{
client.print("<td>Light 1 is ON</td>");
}
else
{
client.print("<td>Light 1 is OFF</td>");
}
client.println("<br />");
if (digitalRead(7))
{
client.print("<td>Light 2 is ON</td>");
}
else
{
client.print("<td>Light 2 is OFF</td>");
}
client.println("</tr>");
client.println("<tr>");
if (digitalRead(8))
{
client.print("<td>Light 3 is ON</td>");
}
else
{
client.print("<td>Light 3 is OFF</td>");
}
if (digitalRead(9))
{
client.print("<td>Light 4 is ON</td>");
}
else
{
client.print("<td>Light 4 is OFF</td>");
}
client.println("</tr>");
client.println("</table>");
client.println("</center>");
//clearing string for next read
readString="";
client.println("</body>");
client.println("</HTML>");
delay(1);
//stopping client
client.stop();
}
}
}
}
}