-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartDoor-Project.ino
More file actions
305 lines (284 loc) · 7.96 KB
/
SmartDoor-Project.ino
File metadata and controls
305 lines (284 loc) · 7.96 KB
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
//includes
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include <Servo.h>
// size of buffer used to capture HTTP requests
#define REQ_BUF_SIZE 100
//used to decrease the size of the program
// by commenting out the Serial
#define debugMode false
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x93, 0x02 };
// IP address in the range of the IPv4 address of the computer
IPAddress ip(192, 168, 10, 108);
// create a server at port 80
EthernetServer server(80);
// stores the incoming HTTP request
String req;
// the web page file on the SD card
File webFile;
// incoming HTTP request stored as null terminated string
char HTTP_req[REQ_BUF_SIZE] = {0};
// index into HTTP_req buffer
char req_index = 0;
//Pin configurations for servo, speaker and switch
int servoPin = 6;
int spkrPin = 3;
int swPin = 5;
Servo myservo;
int pos;
//Positions of the servo when unlocked and locked
int posU = 104; int posL = 4; //TowerPro MG946R
//int posU = 4; int posL = 94; //Futaba S9303
boolean currLockState = 0; //1 if the lock is currently in locked state
boolean destLockState = 0; //desired state
boolean lastButton = LOW;
boolean currentButton = LOW;
//Frequencies of different tones
int c4 = 261, d4 = 293, e4 = 329, f4 = 349, g4 = 392;
void setup()
{
// disable Ethernet chip
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
#if debugMode
Serial.begin(9600);
Serial.println("Debugging");
Serial.println("Serial Connection established");
Serial.println("Initializing SD card...");
#endif
// initialize SD card
if (!SD.begin(4)) {
#if debugMode
Serial.println("ERROR - SD card initialization failed!");
#endif
return; // init failed
}
#if debugMode
Serial.println("SUCCESS - SD card initialized.");
#endif
// check for index.htm file
if (!SD.exists("index.htm")) {
#if debugMode||a
Serial.println("ERROR - Can't find index.htm file!");
#endif
return;
}
#if debugMode
Serial.println("SUCCESS - Found index.htm file.");
#endif
if (!SD.exists("lock.jpg")) {
#if debugMode
Serial.println("ERROR - Can't find lock.jpg file!");
#endif
return;
}
#if debugMode
Serial.println("SUCCESS - Found lock.jpg file.");
#endif
if (!SD.exists("unlock.jpg")) {
#if debugMode
Serial.println("ERROR - Can't find unlock.jpg file!");
#endif
return;
}
#if debugMode
Serial.println("SUCCESS - Found unlock.jpg file.");
#endif
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // initialize server and begin listening for clients
pinMode(swPin, INPUT);
myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
myservo.write(posU);
delay(1000);
}
void loop()
{
EthernetClient client = server.available();
// check if there is any client available
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()) {
// check if there's any client data available to read
if (client.available()) {
char c = client.read(); // read 1 byte (character)
if (req_index < (REQ_BUF_SIZE - 1)) {
HTTP_req[req_index] = c; // save HTTP request character
req_index++;
}
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
// remainder of header follows below, depending on if
// web page or XML page is requested
// Ajax request - send XML file
if (StrContains(HTTP_req, "ajax_inputs")) {
// send rest of HTTP header
client.println("Content-Type: text/xml");
client.println("Connection: keep-alive");
client.println();
ProcessLockRequest();
XML_response(client);
} else { // web page request
if (StrContains(HTTP_req, "GET / ")) {
client.println("Content-Type: text/html");
client.println("Connection: keep-alive");
client.println();
webFile = SD.open("index.htm");
//Serial.println("Tried to open index.html");
} else if (StrContains(HTTP_req, "GET /lock.jpg")) {
webFile = SD.open("lock.jpg");
if (webFile) {
client.println();
}
} else if (StrContains(HTTP_req, "GET /unlock.jpg")) {
webFile = SD.open("unlock.jpg");
if (webFile) {
client.println();
}
}
if (webFile) {
while(webFile.available()) {
//Serial.println("Available");
client.write(webFile.read()); // send web page to client
}
webFile.close();
}
}
#if debugMode||a
// display received HTTP request on serial port
Serial.print(HTTP_req);
#endif
// reset buffer index and all buffer elements to 0
req_index = 0;
StrClear(HTTP_req, REQ_BUF_SIZE);
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
} else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
}
}
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
}
processSwitch();
}
void ProcessLockRequest(void)
{
if (StrContains(HTTP_req, "Lock=1") && !currLockState) {
destLockState = true;
rotateServo();
} else if (StrContains(HTTP_req, "Lock=0") && currLockState) {
destLockState = false;
rotateServo();
}
}
void rotateServo()
{
if (!(destLockState^currLockState)) {
return;
}
if (destLockState) {
#if debugMode||a
Serial.println(" Locking");
#endif
playTones(c4, d4, g4, 200);
for (pos=posU; pos>=posL; pos-=5) {
myservo.write(pos);
delay(15);
}
#if debugMode
Serial.println(" Locked");
#endif
} else {
#if debugMode
Serial.println(" Unlocking");
#endif
playTones(g4, d4, c4, 200);
for (pos=posL; pos<=posU; pos+=5) {
myservo.write(pos);
delay(15);
}
#if debugMode
Serial.println(" Unlocked");
#endif
}
currLockState = destLockState;
delay(1000);
}
void playTones(int t1, int t2, int t3, int tlen)
{
tone(spkrPin, t1, tlen); delay(tlen/2);
tone(spkrPin, t2, tlen); delay(tlen/2);
tone(spkrPin, t3, tlen); delay(tlen/2);
}
void processSwitch()
{
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH) {
destLockState = !currLockState;
rotateServo();
}
lastButton = currentButton;
}
boolean debounce (boolean last)
{
boolean current = digitalRead(swPin);
if (last != current) {
delay(5);
current = digitalRead(swPin);
}
return current;
}
void XML_response(EthernetClient cl)
{
cl.print("<?xml version = \"1.0\" ?>");
cl.print("<inputs>");
cl.print("<Lock>");
if (currLockState) {
cl.print("locked");
}
else {
cl.print("unlocked");
}
cl.println("</Lock>");
cl.print("</inputs>");
}
// sets every element of str to 0 (clears array)
void StrClear(char *str, char length)
{
for (int i = 0; i < length; i++) {
str[i] = 0;
}
}
// searches for the string sfind in the string str
// returns 1 if string found
// returns 0 if string not found
char StrContains(char *str, char *sfind)
{
char found = 0;
char index = 0;
char len;
len = strlen(str);
if (strlen(sfind) > len) {
return 0;
}
while (index < len) {
if (str[index] == sfind[found]) {
found++;
if (strlen(sfind) == found) {
return 1;
}
} else {
found = 0;
}
index++;
}
return 0;
}