Arduino

아두이노 WiFi : 웹 서버로 센서값 출력하기

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

첨부파일

<* 아두이노 WiFi :  웹 서버로  센서값 출력하기 *>
  - 조도 센서를 A0에 연결한다.
  -  D12에 LED를 연결한다.
    - 시작 시에 LED가 "ON" 상태 이다.
    - 조도 센서 값이 250 보다 작으면 LED 가 "ON" 상태이고,
    - 조도 센서 값이 250 보다 크면  LED 가 "OFF" 상태이ek.
    - 조도 센서의 상태를 WEP에서 나타넨다.

------ < 아두이노 WiFi  웹 서버로  센서값 출력 소스 코드 > -------------------------
#include <WiFiEsp.h>
#include <SoftwareSerial.h>

SoftwareSerial EspSerial(6, 7); // RX, TX
 
const char* ssid    = "와이파이이름";
const char* password = "와이파이비밀번호";
 
WiFiEspServer server(80); //http의 기본 포트는 80입니다.

int CDS_Pin = A0;
int LED_Pin = 12;

int status = WL_IDLE_STATUS;    // the Wifi radio's status
 
void setup() {
  pinMode(CDS_Pin,INPUT);
  pinMode(LED_Pin, OUTPUT);

  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, password);      // 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() {
 
  int val = analogRead(CDS_Pin); // cds 센서값 저장
  delay(50);
  Serial.println(val);
  WiFiEspClient client = server.available();
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println("Connection: close");
  client.println("Refresh: 1");  // 자동으로 웹페이지 새로고침 (1초 설정)
  client.println();
  client.println("<!DOCTYPE html>");
  client.println("<html xmlns='http://www.w3.org/1999/xhtml'>");
  client.println("<head>\n<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />");
 
  client.println("<title>DIYver tistory blog test</title>"); // 웹 서버 페이지 제목 설정
  client.println("</head>\n<body>");
  client.println("<center>");
  client.println("<H1> Arduino WEB SERVER : Sensor Test </H1>"); // 페이지 내용 설정
 
  if (val < 250 ) {                     
    client.print("<H2>DARK !</H2> "); 
    client.println("<br>");
    client.println("<H3>LED ON</H3>");
    client.println("<br>");
    client.println("<H4>Sensor Value</H4>");
    client.println(val);
   
    digitalWrite(LED_Pin, HIGH);            // LED ON
 
  }
  else                                       
  {
    client.print("<H2>BRIGHT </H2>");     
    client.println("<br>");
    client.print("<H3>LED OFF</H3>");
    client.println("<br>");
    client.println("<H4>Sensor Value</H4>");
    client.println(val);
    digitalWrite(LED_Pin, LOW);        // LED OFF
  }
  client.println("<br>");
  client.println("<br>");
 
  client.println("<H1> http://www.embweedkorea.co.kr </H1>"); // 페이지 내용 설정
  client.println("<pre>");
  client.print("</body>\n</html>");
}

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();
}