http://wiznetacademy.com/wp/?p=211
이더넷 실드에 있는 SD 카드에 LED ON/OFF를 위한 웹 페이지 파일들을 만들어 넣고, 인터넷에 연결되면(브라우저로 연결되면) SD 카드에 저장되어 있는 파일을 읽어 보내기로 하였습니다. led_on.htm 파일과 led_off.htm 파일 2개로 만들어 놓고, LED가 OFF되어 있을 때는 led_on.htm을 읽어 보내고, LED가 ON되어 있으면, led_off.htm을 읽어 보내게 하였습니다.
준비물은 이더넷 실드, LED 1개, 220Ω 저항 1개 , 몇개의 점퍼선이 있으면 됩니다. 물론 인터넷 공유기가 있어야 하며, SD 카드와 PC에서 SD 카드를 사용할 수 있는 SD 카드 USB 리더기 혹은 SD 카드를 사용할 수 있는 노트북이나 PC 경우에는 SD 카드 어탭터가 필요합니다.
준비물
=====
- 이더넷 실드 1개
- LED 1개
- 220Ω 저항 1개
- 여러 점퍼선들
- 인터넷 공유기
- SD 카드 및 리더기
아두이노 D8 <—> 220Ω저항 <—> + LED | – LED <—> GND
2. 스케치 작성
스케치는 전에 작성한 WebServer 스케치를 수정 보완하여 LED의 상태에 따라 SD 카드에 저장되어 있는 led_on.htm 파일 혹은 led_off.htm 파일을 읽어 보내는 것으로 하였습니다. 만약 SD 카드가 없거나 파일이 없을 경우에는 전과 같이 스케치에서 HTML 페이지를 만들어 보내는 것으로 하였습니다.
SD 카드 라이브러리는 아두이노에서 제공하는 SD 라이브러리를 이용하였으며, SD.begin을 이용하여 SD 카드에서 사용할 SPI 인터페이스의 SS(Slave Selection) 핀 번호를 지정하여 주었습니다.
스케치 파일
———
|
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
|
/*
Web Server with SD card
A simple web server that turns LED ON/OFF
Circuit:
LED is connected to D8
created 18 Dec 2009 by David A. Mellis
modified 9 Apr 2012 by Tom Igoe
modified 26 July 2014 by Wayfarer
*/
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#define SSD 4
#define LED 8
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,100);
char led_on[] = “led_on.htm”;
char led_off[] = “led_off.htm”;
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
String line;
boolean bLED = false;
boolean bCNT = false;
int length = 0;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print(“server is at “);
Serial.println(Ethernet.localIP());
line.reserve(200);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
if(!SD.begin(SSD)) {
Serial.println(F(“Card failed, or not present”));
}
else {
Serial.println(F(“Card initialized.”));
}
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println(F(“new client”));
// an http request ends with a blank line
boolean currentLineIsBlank = true;
line = “”;
while (client.connected()) {
if (client.available()) {
char c = client.read();
line += c;
// if you’ve gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == ‘n’ && currentLineIsBlank ) {
Serial.println();
if(bCNT) {
line = “”;
for(int i=0; i < length; i++) {
c = client.read();
line += c;
}
Serial.println(line);
if(line.indexOf(“do=ON”) >= 0) {
bLED = true;
digitalWrite(LED, HIGH);
}
else if(line.indexOf(“do=OFF”) >= 0) {
bLED = false;
digitalWrite(LED, LOW);
}
}
line = “”;
bCNT = false;
File dataFile;
// LED가 꺼져 있으면, led_on.htm 파일을 open합니다.
if(!bLED) {
dataFile = SD.open(led_on);
}
// LED가 켜져 있으면, led_off.htm 파일을 open합니다.
else {
dataFile = SD.open(led_off);
}
// 파일이 성공적으로 열리면, 이를 읽어 보냅니다.
if(dataFile) {
while(dataFile.available()) {
client.write(dataFile.read());
}
// 열린 파일을 닫습니다.
dataFile.close();
}
// SD 카드가 없거나 파일이 없을 경우, 메시지를 직접 보냅니다.
else {
client.println(F(“HTTP/1.1 200 OK”));
client.println(F(“Content-Type: text/html”));
client.println(F(“Connection: close”)); // the connection will be closed after completion of the response
client.println(F(“Refresh: 10”)); // refresh the page automatically every 10 sec
client.println();
client.println(F(“<!DOCTYPE HTML>”));
client.println(F(“<html>”));
client.println(F(” <head>”));
client.println(F(” <meta http-equiv=”Content-Type” content=”text/html;charset=UTF-8″>”));
client.println(F(” </head>”));
client.println(F(” <body>”));
client.println((” <p>아두이노 웹 서버<br>”));
client.println((” ================</p>”));
client.println((“SD 카드가 장착되어 있지 않습니다.<br>”));
client.println((“확인한 후에 다시 시도하여 주세요!<br>”));
client.println(F(” </body>”));
client.println(F(“</html>”));
}
break;
}
if (c == ‘n’) {
Serial.print(line);
// you’re starting a new line
currentLineIsBlank = true;
if(line.indexOf(“Content-Length:”) >= 0) {
String s = line.substring(line.indexOf(“:”) + 1);
s.trim();
length = s.toInt();
bCNT = true;
}
line=“”;
}
else if (c != ‘r’) {
// you’ve gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
//*
// close the connection:
client.stop();
Serial.println(“client disonnected”);
//*/
}
}
|
그리고 led_on.htm 파일과 led_off.htm 파일을 만들어 SD 카드로 복사하여 놓습니다. 물론 파일들을 UTF-8로 만들었습니다. 아두이노 IDE를 사용하는 것도 한 방법입니다.
led_on.htm
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
HTTP/1.1 200 OK
Content-Type: text/html
Connection: close
Refresh: 10
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv=“Content-Type” content=“text/html;charset=UTF-8”>
</head>
<body>
<p>아두이노 웹 서버<br>
================</p>
<form method=“post”>
<input type=“hidden” name=“do” value=“ON”>
LED
<input type=“button” name=“B1” value=“ON” onclick=“submit();” >
</form>
</body>
</html>
|
led_off.htm
———-
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
HTTP/1.1 200 OK
Content-Type: text/html
Connection: close
Refresh: 10
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv=“Content-Type” content=“text/html;charset=UTF-8”>
</head>
<body>
<p>아두이노 웹 서버<br>
================</p>
<form method=“post”>
<input type=“hidden” name=“do” value=“OFF”>
LED
<input type=“button” name=“B1” value=“OFF” onclick=“submit();” >
</form>
</body>
</html>
|
3. 시험 결과
이더넷 모듈과 SD 카드를 읽고 쓰기 위하여 SPI 인터페이스를 사용하고 있고, 이더넷 모듈을 선택하기 위하여 디지털 10번 핀이, SD 카드를 선택하기 위해서 디지털 4번 핀이 사용되고 있습니다. 동시는 아니지만 SD 카드에 있는 파일을 읽어내 이를 이더넷 모듈을 통하여 PC로 전달하는데 있어 문제가 일어나지 않을까 우려하였는데, 아무런 문제 없이 잘 동작하였습니다.
SD 카드가 없을 때와 있을 때 둘 모두 잘 동작합니다. SD 카드 인식하는 부분을 setup 부분에 넣었더니 뺏다 꽂았을 경우, reset을 해줘야 인식하는 부분을 빼 놓고는 원하는 대로 잘 동작합니다.
스마트 폰으로 접속하여 버튼을 누르면, Refresh를 10초로 놓아 느리지만 PC 웹 브라우저의 내용도 스마트 폰과 같은 상태로 변합니다.
4. 마무리
정적(static) 웹 페이지들은 SD 카드에 넣고 아두이노로 동적(Dynamic) 웹 페이지 서비스를 한다면 WOT(Web Of Things)도 가능할 수도 있겠다는 느낌을 받은 하루였습니다.
버튼을 누를 때, HTTP Request를 만들어 웹 서버로 전송을 하는데, GET을 사용하면, URL에 정보가 노출되고, POST를 사용하면 HTTP Request 헤더에 정보를 넣어 보냅니다. 여기에 들어 있는 정보들을 읽어내야 하는 어려움이 있습니다. 앞으로 아두이노에서 웹 서비스들을 간단하게 만들 수 있는 HTTP Request와 HTTP Response 라이브러리의 등장을 기다리면서,…
댓글 0
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
|---|---|---|---|---|
| 6 | wget 사용법 | proin | 2018.09.19 | 2 |
| 5 | 압축(gzip, bzip2, xz ) | proin | 2018.09.05 | 3 |
| 4 | 볼륨 크기 조정 후 Linux 파일 시스템 확장 | proin | 2018.08.30 | 5 |
| 3 | Linux 보안설정에 관한 모든것 | proin | 2018.08.28 | 2 |
| 2 | 리눅스 서버 보안 강화 팁 20선 | proin | 2018.08.28 | 1 |
| » | 아두이노를 인터넷과 연결하다 2 :SD 카드 이용 | proin | 2018.07.18 | 2 |
