아두이노프로세싱 프로그래밍

아두이노 이더넷(Ethernet) 웹서버 코딩에 의한 LED ON OFF

coding art 2018. 7. 6. 17:57
728x90

______________________________________________________________________________________


아두이노 보드에 설치된 가변 저항의 전압 값을 아날로그 핀에서 읽어 이더넷 네트워킹에 의해 웹에 출력이 가능하였다

. 다음 단계로는 웹서버 코딩에 의해 웹에 ON OFF 버튼을 제공하여 클릭하므로써 아두이노 디지털 핀에 설치된 LEDON OFF 시켜보자.


나머지 내용은 아래의 스팀잇 사이트에서 계속 읽으세요.


https://steemit.com/kr/@codingart/89-ethernet-led-on-off



//Webserver_ethernet_LED_ON_OFF_01

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 0, 11 }; // fixed IP addr in LAN

EthernetServer server(80); //server port
String readString;
int ledPin = 2;
void setup(){
pinMode(ledPin, OUTPUT); //pin selected to control LED
Ethernet.begin(mac, ip);
server.begin();
Serial.begin(9600);
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop(){
EthernetClient client = server.available();

  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        //read char by char HTTP request
        if (readString.length() < 100) {
        //store characters to string
          readString += c;
        //Serial.print(c);
        }

  if (c == '\n') {
    Serial.println(readString); //print to serial monitor for debuging
    client.println("HTTP/1.1 200 OK"); //send new page
    client.println("Content-Type: text/html");
    client.println();
    client.println("<head>");
    client.println("<TITLE>Home Automation</TITLE>");
    //배경 색 문자 색 사이즈 HTML CSS 설정  
    client.println("<style>");
    client.println("body {");
    client.println("background-color: lightblue;");
    client.println("}");
    client.println("p {");
    client.println("color:red;");
    client.println("font-size:250%;");
    client.println("}");  
    //버튼 HTML CSS 설정  
    client.println(".button {");
    client.println("background-color: green;");
    client.println("border: none;");
    client.println("color: white;");
    client.println("padding: 15px 32px;");
    client.println("text-align: center;");
    client.println("text-decoration: none;");
    client.println("display: inline-block;");
    client.println("font-size: 14px;");
    client.println("margin: 14px 20px;");
    client.println("cursor: pointer;");
    client.println("}");
    client.println("</style>");
    client.println("</head>");
    client.println("<body>");
    client.println("<p>");
       if(readString.indexOf("/LED=ON") != -1)  {
         digitalWrite(ledPin, HIGH);
         client.print("LED on");
         client.println("<br><br>");
        }
        else  {
        if(readString.indexOf("LED=OFF") != -1)  {
          digitalWrite(ledPin, LOW);
          client.print("LED Off");
          client.println("<br><br>");
        }
      }
    client.println("<a href=\"/LED=ON\"\" class='button'>Turn on </button></a>");
    client.println("<a href=\"/LED=OFF\"\" class='button'>Turn Off </button></a>");
    client.println("</p>");
    client.println("</body>");
    client.println("</HTML>");
    delay(1);
client.stop();
    readString=""; //clearing string for next read
    }//if (c == '\n') {
   
      }//if (client.available()) {
    }//while (client.connected()) {
  }// if (client) {
}//끝