아두이노
아두이노 이더넷 웹서버 만들기 -#12
오징어땅콩2
2020. 5. 3. 23:07
반응형
이전 코드에 비해 크게 변경된것은 없으니 비교해서 보면 쉽게 이해가 될듯 하다.
중요한것은 파싱 함수를 하나 추가 한것이다.
일반 서버급의 속도로 바로 바로 링크페이지가 전환 되지 않는다는점도 이해가 필요하다.
물론 더 반응속도를 올리는것은 가능해 보인다.
추가로 이야기 한다면 아파치 웹서버나 기타 웹서버들이 기본적으로 어떻게 반응하는지에 대한 이해를 했으면 하다.
따라져보면 더 엄청난 기능을 하지만, 기본적인 이해는 이것으로도 충분하다.
/*Web Server*/
#include <SPI.h>
#include <Ethernet.h>
#include <string.h>
// Enter a MAC address and IP address for your controller below.
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 5, 177);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
char msg[500];
char* m;
int select= 0;
void setup()
{
Serial.begin(115200);
while (!Serial)
{
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Ethernet WebServer Example");
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware)
{
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true)
{
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF)
{
Serial.println("Ethernet cable is not connected.");
}
// start the server
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
select= 0;
m =msg;
}
void loop()
{
// listen for incoming clients
EthernetClient client = server.available();
if (client)
{
Serial.println("new client");
// an http request ends with a blank line
bool currentLineIsBlank = true;
while (client.connected())
{
if (client.available())
{
char c;
m++;
// 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')
{
*m='\0';
if(select ==0) select = msg_parsing(msg);
m = msg;
if(currentLineIsBlank)
{
Serial.println(select);
if (select == 1) webPage1(client);
else webPage2(client);
select =0;
break;
}
else
{
// you're starting a new line
currentLineIsBlank = true;
}
}
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:
Serial.println("client disconnected");
}
}
int msg_parsing(char* str)
{
int nothing = 0;
char*p =str;
if( str==NULL || strlen(str)==0) return nothing;
while(*str!='\r' && *str!='\n' && *str!='\0' )
{
if(*str==':')
{
*str='\0';
if(strcmp(p, "Referer")==0)
{
str++;
}
break;
}
else str++;
}
return nothing;
}
void read_Temp1(char*& p)
{
for(int i = 0; i < 5; i++)
{
double t;
t = analogRead (A0+i); // A0의 값을 불러옵니다.
t *= 5.0 / 10.24; //온도값을 계산하는 공식입니다.
p += sprintf(p, "T(A%1d)=%s C<br/>\n", i, String(t,1).c_str());
}
return ;
}
void read_Temp2(char*& p)
{
double avg = 0;
for(int i = 0; i < 5; i++)
{
double t;
t = analogRead (A0+i); // A0의 값을 불러옵니다.
t *= 5.0 / 10.24; //온도값을 계산하는 공식입니다.
avg += t;
}
p += sprintf(p, "T(avg)=%s C<br/>\n", String(avg/5.0,1).c_str());
return ;
}
void webPage1(EthernetClient& c)
{
// send a standard http response header
char*p=msg;
p+=sprintf(p,"HTTP/1.1 200 OK\n");
p+=sprintf(p,"Content-Type: text/html\n");
p+=sprintf(p,"Connection: close\n");
p+=sprintf(p,"Refresh: 5\n\n"); // refresh the page automatically every xx sec
p+=sprintf(p,"<!DOCTYPE HTML>\n");
p+=sprintf(p,"<html>\n");
read_Temp1(p);
p+=sprintf(p,"</html>\n");
Serial.println(strlen(msg));
c.println(msg);
return ;
}
void webPage2(EthernetClient& c)
{
// send a standard http response header
char*p=msg;
p+=sprintf(p,"HTTP/1.1 200 OK\n");
p+=sprintf(p,"Content-Type: text/html\n");
p+=sprintf(p,"Connection: close\n");
p+=sprintf(p,"Refresh: 5\n\n"); // refresh the page automatically every xx sec
p+=sprintf(p,"<!DOCTYPE HTML>\n");
p+=sprintf(p,"<html>\n");
read_Temp2(p);
p+=sprintf(p,"</html>\n");
Serial.println(strlen(msg));
c.println(msg);
return ;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|