웹서버 BLINKING 제어

댓글 2

전기차 제작

2011. 12. 7.

Webserver_nodemcu_LED_ON_OFF_BLINKING_01









































Webserver_nodemcu_LED_ON_OFF_BLINKING_01

 #include <ESP8266WiFi.h>

 const char* ssid = "android1234";
 const char* password = "dddddddddd";

 int ledPin = 2; //
 
 WiFiServer server(80);

 void setup() {
   Serial.begin(115200);
   delay(10);

   pinMode(ledPin, OUTPUT);
   digitalWrite(ledPin, HIGH);

   // Set WiFi to station mode and disconnect from an AP if it was previously connected
   WiFi.mode(WIFI_STA);
   WiFi.disconnect();
   delay(100);

   Serial.println("Setup done");

   // Connect to WiFi network
   Serial.println();
   Serial.println();
   Serial.print("Connecting to ");
   Serial.println(ssid);

   WiFi.begin(ssid, password);

   while (WiFi.status() != WL_CONNECTED) {
     delay(500);
     Serial.print(".");
   }
   Serial.println("");
   Serial.println("WiFi connected");

   // Start the server
   server.begin();
   Serial.println("Server started");

   // Print the IP address
   Serial.print("Use this URL to connect: ");
   Serial.print("http://");
   Serial.print(WiFi.localIP());
   Serial.println("/");

 }

 void loop() {
   // Check if a client has connected
   WiFiClient client = server.available();
   if (!client) {
     return;
   }

   // Wait until the client sends some data
   Serial.println("new client");
   while(!client.available()){
     delay(1);
   }

   // Read the first line of the request
   String request = client.readStringUntil('\r');
   Serial.println(request);
   client.flush();

   // Match the request

   int value = LOW;

   if (request.indexOf("/LED=ON") != -1)  {
     digitalWrite(ledPin, LOW);
     value = HIGH;
   }
   if (request.indexOf("/LED=OFF") != -1)  {
     digitalWrite(ledPin, HIGH);
     value = LOW;
   }
            
   // Return the response
   client.println("HTTP/1.1 200 OK");
   client.println("Content-Type: text/html");
   client.println(""); //  do not forget this one
   client.println("<!DOCTYPE HTML>");
   client.println("<html>");
   client.println("<head>");
   //배경 색 문자 색 사이즈 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: blue;");
    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: 20px;");
    client.println("margin: 12px 15px;");
    client.println("cursor: pointer;");
    client.println("}");
    client.println("</style>");
    client.println("</head>");
   client.println("<body>");
   client.println("<p>");
   client.print(" LED pin is now: ");
  
   if (request.indexOf("/LED=BLINK") != -1 )  {
      client.print("BLINK");
       for( int i = 0; i<19; i++) {
         if(value == HIGH) {
           digitalWrite(ledPin, LOW);
           delay(50);
           digitalWrite(ledPin, HIGH);
           delay(50);  
          }
          else {
           digitalWrite(ledPin, HIGH);
           delay(50);
           digitalWrite(ledPin, LOW);
           delay(50);
           }
       }
    }
    else {
       if(value == HIGH) {
        client.print(" on");
        }
       if(value == LOW) {
       client.print("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("<a href=\"/LED=BLINK\"\" class='button'>BLINK </button></a>");
   client.println("</p>");
   client.println("</body>");
   client.println("</html>");

   delay(1);
   Serial.println("Client disonnected");
   Serial.println("");

 }//프로그렘 끝