본문 바로가기
아두이노

아두이노 이더넷 웹서버 만들기 -#8

by 오징어땅콩2 2020. 5. 1.
반응형

자 그러면 두개 소스코드도 정리가 되었으니 두개 페이지도 나누고 링크의 개념도 적용해 보죠.
클라이언트가 보통은 웹 주소로 접근합니다.
요즘 사람들은 약간 생소할 수도 있죠. 그냥 포탈에서 링크로 클릭 하거나, 
쉬리나 알렉사가 찾아 주니까요.
그러나 10년전, 지금도 주소로 접근하는 사람들이 있죠.
예를 들이,
http://192.168.5.177/index.html
http://192.168.5.177/page1.html
http://192.168.5.177/page2.html

이렇게 접근하는것으로 모르는 사람도 있겠지만 이게 기본이라 알고는 있어야 겠죠.
웹페이지의 링크의 개념도 비슷합니다.

    p+=sprintf(p,"<A href=\"http://192.168.5.177/page1.html\">Page1</A><BR>\n");  
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter


이전 소스코드에서 링크와 페이지 두장까지 만들어 보죠.

 

/*Web Server*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
byte mac[] = {0xDE0xAD0xBE0xEF0xFE0xED};
IPAddress ip(1921685177);
 
// 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;
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());
 
  m =msg;
}
 
 
void loop() 
{
  int select= 1;
  // 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) = c = client.read();
        m++;
        //Serial.write(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')
        {
            *m='\0';
            Serial.write(msg);
            m=msg;          
            if(currentLineIsBlank) 
            {
              if (select == 1) webPage1(client);
              else webPage2(client);
              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");
  }
}
 
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,"<A href=\"http://192.168.5.177/page1.html\">Page1</A><BR>\n");  
    p+=sprintf(p,"<A href=\"http://192.168.5.177/page2.html\">Page2</A><BR>\n");       
    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,"<A href=\"http://192.168.5.177/page1.html\">Page1</A><BR>\n");  
    p+=sprintf(p,"<A href=\"http://192.168.5.177/page2.html\">Page2</A><BR>\n");        
    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

 

댓글