Arduino

아두이노 WiFi : Web Server 만들기

작성자 임베디드코리아 작성일25-05-16 00:49 조회135회 댓글0건

첨부파일

<* 아두이노 WiFi : Web Server 만들기 *>

----<  Arduino  WiFi  ESP8266 모듈을  Web Server 만들기 소스 코드 > ----------------------
#include <WiFiEsp.h>
#include <SoftwareSerial.h>

SoftwareSerial EspSerial(6, 7); // RX, TX

char ssid[] = "Twim";            // your network SSID (name)
char pass[] = "12345678";        // your network password

int status = WL_IDLE_STATUS;    // the Wifi radio's status
int reqCount = 0;                // number of requests received

WiFiEspServer server(80);

void setup() {
  Serial.begin(9600);
  EspSerial.begin(9600);    // initialize serial for ESP module
  WiFi.init(&EspSerial);    // initialize ESP module

  /* check for the presence of the shield */
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  /* attempt to connect to WiFi network */
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);

    status = WiFi.begin(ssid, pass);      // Connect to WPA/WPA2 network
  }

  Serial.println("You're connected to the network");
  printWifiStatus();
 
  server.begin();            // start the web server on port 80
}

void loop() {
 
  WiFiEspClient client = server.available();    // listen for incoming clients
  if (client) {
    Serial.println("New client");
   
    boolean currentLineIsBlank = true;    // an http request ends with a blank line
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
       
        if (c == '\n' && currentLineIsBlank) { 
          Serial.println("Sending response");
         
          /* html */
          client.print(
            "HTTP/1.1 200 OK\r\n"
            "Content-Type: text/html\r\n"
            "Connection: close\r\n" 
            "Refresh: 20\r\n"        // refresh the page automatically every 20 sec
            "\r\n");
          client.print("<!DOCTYPE HTML>\r\n");
          client.print("<html>\r\n");
          client.print("<h1> Embedded Korea!</h1>\r\n");
          client.print("<br>\r\n");
          client.print("</html>\r\n");
          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(10);

    // close the connection:
    client.stop();
    Serial.println("Client disconnected");
  }

}

void printWifiStatus(){
  /* print the SSID of the network you're attached to */
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  /*print your WiFi shield's IP address */
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
 
  /* print where to go in the browser */
  Serial.println();
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
  Serial.println();
}