정보나눔

오픈소스하드웨어 프로젝트에 대한 다양한 정보를 나누는 공간입니다.

웹 브라우저 열기 코드를 실행중인데 브라우저가 열리지않습니다.
신경철 | 2020-06-12

조도센서값을 웹 브라우저에 출력하기위해서 이 예제를 실행했는데 아이파 값 까지는 출력이 됐지만 웹 브라우저에 연결이 안됩니다.

 

 

연결은 브라우저에서 연결시간이 너무 길어서 안된다고 뜹니다.

 

어케해야되나요

 

관련코드)

#include <SPI.h>
#include "WizFi250.h"
#include <LiquidCrystal_I2C.h>


char ssid[] = "KT_GiGA_2G_Wave2_ADD5";  // WIFI 연결해 놓은 공유기 이름(SSID)입력할것.
char pass[] = "0cf2fbx552";        // 공유기비번
int status = WL_IDLE_STATUS;     // the Wifi radio's status
int reqCount = 0;                // number of requests received

LiquidCrystal_I2C lcd(0x27, 16, 2);
int echoPin = 9;
int trigPin = 10;

unsigned long duration;
float distance;

 

WiFiServer server(80);

void printWifiStatus();

void setup()
{
  // initialize serial for debugging
  Serial.begin(115200);

  //해당 보드 셋업 코드

  // lcd.begin();
  // lcd.backlight();
  
 //lcd.print("Paper: ");
  
  //Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  // initialize serial for ESP module
  WiFi.init();

  // 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);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }
  

  Serial.println("You're connected to the network");
  printWifiStatus();

  // start the web server on port 80
  server.begin(); 

}

 

void loop()
{

  //해당 코드 루프문
  digitalWrite(trigPin, LOW);
  digitalWrite(echoPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  //unsigned long duration = pulseIn(echoPin, HIGH);    
  //float distance = ((float)(340 * duration) / 10000) / 2;   
  duration = pulseIn(echoPin, HIGH);
  distance = ((float)(340 * duration) / 10000) / 2;

  if(distance < 10 ){
    printPercentage(100);
  }else if(distance < 20){
    printPercentage(90);
  }else if(distance < 30){
    printPercentage(80);
  }else if(distance < 40){
    printPercentage(70);
  }else if(distance < 50){
    printPercentage(60);
  }else if(distance < 60){
    printPercentage(50);
  }else if(distance < 70){
    printPercentage(40);
  }else if(distance < 80){
    printPercentage(30);
  }else if(distance < 90){
    printPercentage(20);
  }else if(distance < 100){
    printPercentage(10);
  }else{
    printPercentage(0);
  }

  // listen for incoming clients
  WiFiClient client = server.available();
  if (client) {
    Serial.println("New client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        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' && currentLineIsBlank) {
          Serial.println("Sending response");

          // send a standard http response header
          // use \r\n instead of many println statements to speedup data send
          client.print(
            "HTTP/1.1 200 OK\r\n"
            "Content-Type: text/html\r\n"
            "Connection: close\r\n"  // the connection will be closed after completion of the response
            "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>Hello World!</h1>\r\n");
          client.print("Requests received: ");
          client.print(++reqCount);
          client.print("<br>\r\n");
          client.print("Analog input A0: ");
          client.print(analogRead(0));
          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 printPercentage(int percentage){
  lcd.setCursor(0, 1);
  lcd.print(" ");
  lcd.setCursor(4, 1);
//  lcd.print("%d%     ",percentage);
  //lcd.print("cm     ");
  lcd.setCursor(0,0);
  Serial.print(distance);
  Serial.println("cm");
  delay(500);
}

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.print("distanceValue : ");
  Serial.println(distance);
  Serial.println();
}

이전글   |    아두이노 우노 노크센서 2020-06-11
다음글   |    보드 Arduino Uno 컴파일 에러가 계속 뜹니다.... 2020-06-12