반응형
한참 시간이 지났지만, 저번에 못다한 이야기를 해야 할 것 같아서 계속 합니다.
저번에는 웹페이지를 그냥 빈화면, 그냥 텍스트로 구성했다면 이번은 5개의 온도값을 표시하는것으로 진행하며,
좀더 나아가 웹페이 링크라는 개념도 들어 가는것으로 구현 할 것이다.
추가로 이야기 하지만 이것은 ESP8266으로 구현해도 동일한 개념이라서 같은 방식으로 이해해도 될듯 합니다.
일단 지난번 소스코드 그냥 원제작자의 소스코드라면 이번에는 수정이 들어간 소스코드로 정리하는것으로 이해하면 될듯 합니다.
소스코드의 수정본과 원본 코드의 비교만 하더라도 차이가 뭔지 이해하면 될듯 합니다.
그리고 그렇게 어렵지 않으니 참고만 하시면 될듯 합니다.
/*Web Server*/
#include <SPI.h>
#include <Ethernet.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);
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());
}
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())
{
// 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)
{
webPage1(client);
break;
}
if (c == '\n')
{
// 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");
}
}
void read_temp(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 webPage1(EthernetClient& c)
{
// send a standard http response header
char buf[210];
char*p=buf;
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_temp(p);
p+=sprintf(p,"</html>\n");
Serial.println(strlen(buf));
c.println(buf);
return ;
}
void webPage2(EthernetClient& c)
{
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
'아두이노' 카테고리의 다른 글
아두이노 이더넷 웹서버 만들기 -#8 (0) | 2020.05.01 |
---|---|
아두이노 이더넷 웹서버 만들기 -#7 (0) | 2020.05.01 |
아두이노 이더넷 웹서버 만들기 -#5 (0) | 2020.04.07 |
아두이노 이더넷 웹서버 만들기 -#4 (0) | 2020.04.05 |
아두이노 이더넷 웹서버 만들기 -#3 (0) | 2020.04.05 |
댓글