This repository has been archived by the owner on Dec 29, 2017. It is now read-only.
/
sketch.c
executable file
·463 lines (443 loc) · 11.2 KB
/
sketch.c
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
Controlling two servo like a radar
see https://github.com/0x7c0/RadArduino2 for more info
modified on 7 Apr 2014
0x7c0 <0x7c0@teboss.tk>
http://www.gnu.org/licenses/gpl.html GPL v3 License
*/
// Include
#include <SPI.h>
#include <SD.h>
#include <Ethernet.h>
#include <Servo.h>
#include <math.h>
// Define
#define HTTP_BUFF 20
#define HTTP_FILE 7
#define MAX_X 180
#define MAX_Y 90
// Pin
const int xPin = 7; //pwm
const int yPin = 8; //pwm
const int resetPin = 40; //digital
const int sdcPin = 4; //digital
const int ethPin = 10; //digital
const int greLed = 52; //digital
const int redLed = 53; //digital
const int photoRes = A10; //analog
const int thermRes = A8; //analog
// HTTP Capture
char getRequest[HTTP_BUFF] = "";
unsigned int getIndi = 0;
unsigned int X = 1000;
unsigned int Y = 1000;
// Servo
Servo servoX;
Servo servoY;
const int LIMIT = 10;
// File
File webFile;
File dataFile;
// Ethernet
byte mac[] = {0xFE,0xAD,0xBE,0xEF,0xFE,0xED};
byte ip[] = {192,168,2,110}; // CHANGE THIS!
byte gateway[] = {192,168,2,1}; // CHANGE THIS!
byte subnet[] = {255,255,255,0};
//byte dns[] = {8,8,8,8};
EthernetServer server(80);
// Web Output
char* GET[HTTP_FILE]= {
"index.htm",
"style_.gz",
"script.gz",
"angle_.gz",
"menu_.png",
"home_.png",
"favic.ico"
};
char* TYP[3][HTTP_FILE]= {
{
"/index.html",
"/style.css",
"/script.js",
"/angle.png",
"/menu.png",
"/home.png",
"/favicon.ico",
},
{
"Content-Type: text/html",
"Content-Type: text/css",
"Content-Type: application/javascript",
"Content-Type: image/png",
"Content-Type: image/png",
"Content-Type: image/png",
"Content-Type: image/x-icon",
},
{
"0",
"1",
"1",
"1",
"0",
"0",
"0",
}
};
char* LOG = "log.txt";
// End
void setup() {
//debug
Serial.begin(9600);
//pin
pinMode(resetPin, OUTPUT);
pinMode(sdcPin, OUTPUT);
//pinMode(ethPin, OUTPUT);
pinMode(greLed, OUTPUT);
pinMode(redLed, OUTPUT);
// initialize SD card
//digitalWrite(ethPin, LOW);
//digitalWrite(sdcPin, HIGH);
Serial.println("Initializing SD card...");
if (!SD.begin(sdcPin)) {
Serial.println("ERROR - SD card initialization failed!");
return;
}
// check for file
for (int i=0; i<HTTP_FILE; i++) {
if (!SD.exists(GET[i])) {
Serial.print("ERROR - Can't find ");
Serial.println(GET[i]);
return; // can't find index file
}
}
if (!SD.exists(LOG)) {
Serial.print("ERROR - Can't find ");
Serial.println(LOG);
return; // can't find index file
}
else {
dataFile = SD.open(LOG, FILE_READ);
if(dataFile) {
char temp[3],c,d;
while(dataFile.available()) { // X=123-Y=45-
d = (char) dataFile.read(); // X or Y
dataFile.read(); // =
c = (char) dataFile.read();
for(int i=0;c!='-';i++) {
temp[i] = c;
c = (char) dataFile.read();
}
String Coord(temp);
if(d=='X') {
Update('X',Coord.toInt());
}
else if(d=='Y') {
Update('Y',Coord.toInt());
}
memset(temp, 0, sizeof temp);
}
dataFile.close();
}
}
Serial.println("SUCCESS - SD card initialized.");
// initialize Ethernet device
//digitalWrite(sdcPin, LOW);
//digitalWrite(ethPin, HIGH);
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
Serial.println("SUCCESS - Ethernet initialized.");
// servo Pin
servoX.attach(xPin);
servoY.attach(yPin);
if(servoX.attached() && servoY.attached()) {
Serial.println("SUCCESS - Servo initialized.");
}
else {
Serial.println("ERROR - Servo initialization failed!");
return;
}
digitalWrite(redLed,LOW);
digitalWrite(greLed,LOW);
Serial.println("START");
}
void loop() {
// try to get client
EthernetClient client = server.available();
String getStr;
if (client) {
digitalWrite(greLed,HIGH);
boolean currentLineIsBlank = true;
boolean indice = false;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
// filter GET request
// GET /index.html HTTP/1.1
if (c == 'G') {
c = client.read();
if (c == 'E') {
c = client.read();
if (c == 'T') {
Serial.print("GET=");
c = client.read(); // space
while (true) {
c = client.read();
if (c == ' ') {
break;
}
//Serial.println(c);
getRequest[getIndi] = c;
getIndi++;
}
Serial.print(getRequest);
Serial.println("@");
}
}
}
if (c == '\n' && currentLineIsBlank) {
// convert to string and clear char array
String getStr(getRequest);
Clear();
// if check
// first page
if (getStr == "/" || getStr == "/index.html") {
indice = true;
}
// ajax GET info
else if (getStr.startsWith("/Set?")) {
indice = true;
int getRequestStart = getStr.indexOf('?' );
int getRequestFirst = getStr.indexOf('=' );
int getRequestFinal = getStr.indexOf('/',2);
int getRequestDuall = getStr.indexOf('&' ); // 2 GET
if (getRequestDuall>0) { // /index.html?X=20&Y=30
int getRequestSecon = getStr.indexOf('=', getRequestFirst+1);
String inputFirst_1 = getStr.substring(getRequestStart+1, getRequestFirst); // X
String valueFirst_1 = getStr.substring(getRequestFirst+1, getRequestDuall); // 20
String inputFirst_2 = getStr.substring(getRequestDuall+1, getRequestSecon); // Y
String valueFirst_2 = getStr.substring(getRequestSecon+1, getRequestFinal); // 30
if (inputFirst_1 == "X") {
Update('X',valueFirst_1.toInt());
Update('Y',valueFirst_2.toInt());
}
// reverse
else if (inputFirst_1 == "Y") {
Update('Y',valueFirst_1.toInt());
Update('X',valueFirst_2.toInt());
}
}
else { // /index.html?Y=50 or X=50
String inputFirst = getStr.substring(getRequestStart+1, getRequestFirst); // Y
String valueFirst = getStr.substring(getRequestFirst+1, getRequestFinal); // 50
if (inputFirst == "X") {
Update('X',valueFirst.toInt());
}
else if (inputFirst == "Y") {
Update('Y',valueFirst.toInt());
}
}
}
//ajax SAVE info
else if (getStr.startsWith("/Save/")) {
digitalWrite(redLed,HIGH);
client.println("HTTP/1.1 200 OK");
client.println("Connection: close");
Serial.println("SAVE");
Serial.println("");
SD.remove(LOG);
dataFile = SD.open(LOG, FILE_WRITE);
if(dataFile) { // X=123-Y=45-
dataFile.print("X=");
dataFile.print(X);
dataFile.print("-Y=");
dataFile.print(Y);
dataFile.print("-");
dataFile.close();
}
digitalWrite(redLed,LOW);
}
//ajax RESET info
else if (getStr.startsWith("/Reset/")) {
digitalWrite(redLed,HIGH);
client.println("HTTP/1.1 200 OK");
client.println("Connection: close");
Serial.println("RESET");
Serial.println("");
delay(1);
servoX.detach();
servoY.detach();
digitalWrite(redLed,LOW);
digitalWrite(resetPin, LOW);
}
// ajax SET info
else if (getStr.startsWith("/Coordinate/")) {
digitalWrite(redLed,HIGH);
GetValue();
double temp = analogRead(thermRes);
int phot = analogRead(photoRes);
client.println("HTTP/1.1 200 OK");
client.println("Connection: close");
client.println();
// JSON
client.print("{\"coordinate\":{\"X\":");
client.print(X);
client.print(",\"Y\":");
client.print(Y);
client.print("},\"temp\":");
client.print(GetTemp(temp),1);
client.print(",\"light\":");
client.print(phot);
client.print(",\"network\":\"");
client.print(Ethernet.localIP());
client.print("\",\"file\":[");
for (int i=0; i<HTTP_FILE; i++) {
File dFile = SD.open(GET[i]);
if (dFile) {
if(i>0) {
client.print(",");
}
client.print("{");
client.print("\"name\":\"");
client.print(GET[i]);
client.print("\",\"size\":");
client.print(dFile.size());
client.print("}");
}
}
client.println("]}");
digitalWrite(redLed,LOW);
}
// print other file
else {
for(int i=1; i<HTTP_FILE; i++) {
if (getStr == TYP[0][i]) {
webFile = SD.open(GET[i]);
if (webFile) {
client.println("HTTP/1.1 200 OK");
client.println(TYP[1][i]);
if(TYP[2][i] == "1") {
client.println("Content-Encoding: gzip");
}
client.print("Content-Length: ");
client.println(webFile.size());
client.println("Cache-Control: max-age=302400, public");
client.println("Connection: close");
client.println();
}
break;
}
}
}
// endif check
// print index.html
if (indice) {
webFile = SD.open(GET[0]);
if (webFile) {
client.println("HTTP/1.1 200 OK");
client.println(TYP[1][0]);
client.print("Content-Length: ");
client.println(webFile.size());
client.println("Connection: close");
client.println();
}
}
// read file and write into web client
if (webFile) {
while(webFile.available()) {
client.write(webFile.read());
}
webFile.close();
}
break;
}
if (c == '\n') {
// last character on line of received text. Starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
//delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
digitalWrite(greLed,LOW);
}
}
//update value in memory
void Update(const char T,const int N) {
if (T == 'X') {
if (N>=0 && N<=MAX_X && N!=X) {
Serial.print("Update X=");
Serial.print(X);
Serial.print(" To=");
Serial.print(N);
Serial.print(" and ServoX=");
X = N;
if(N<=0+LIMIT) {
servoX.write(0+LIMIT);
}
else if (N>=MAX_X-LIMIT) {
servoX.write(MAX_X-LIMIT);
}
else {
servoX.write(N);
}
Serial.println(servoX.read());
return;
}
}
else if (T == 'Y') {
if (N>=0 && N<=MAX_Y && N!=Y) {
Serial.print("Update Y=");
Serial.print(Y);
Serial.print(" To=");
Serial.print(N);
Serial.print(" and ServoY=");
Y = N;
if(N<=0+LIMIT) {
servoY.write(0+LIMIT);
}
else if (N>=MAX_Y-LIMIT) {
servoY.write(MAX_Y-LIMIT);
}
else {
servoY.write(N);
}
Serial.println(servoY.read());
return;
}
}
}
//print value in memory (global)
void GetValue() {
Serial.print("Now X=");
Serial.print(X);
Serial.print(" and Y=");
Serial.println(Y);
return;
}
//clear http buffer
void Clear() {
getIndi = 0;
memset(getRequest, 0, sizeof getRequest);
/*for (int i=0; i<HTTP_BUFF; i++) {
getRequest[i] = ' ';
}*/
getRequest[0] = '\0';
return;
}
//function to transform volt to temperature
double GetTemp(const double rd) {
//value for 5k resistor
const double A = 0.00076647;
const double B = 0.00023051;
const double C = 0.000000073815;
//Resistance conversion used later
const double R = ( 1023 / rd ) * 50000;
//Temperature conversion equation
const double T = A + B * (log(R)+ C * log(R) * log(R) * log(R));
return ( 1 / T ) - 273.15;
}