簡體   English   中英

在 Arduino Uno R3 和以太網屏蔽上運行 web 服務器時出現問題

[英]Problem running the web server on Arduino Uno R3 and Ethernet shield

我正在嘗試使用堆疊在 Arduino Uno R3 頂部的以太網屏蔽(Wiznet W5100)從 Arduino IDE 運行下面的 WebServer 示例。 以太網屏蔽使用 RJ45 電纜連接到 Internet 路由器。 將代碼上傳到板子后,我看到請求的 IP 地址(192.168.1.177)打印到控制台。 我在這里遇到的奇怪行為是,當我 ping IP 地址時,我收到一個響應,表明 IP 可以從我的筆記本電腦訪問。 此外,我看到 Tx、Rx LED 燈閃爍,從而表明開發板正在接收 ping 消息並回復它們。 這意味着板子成功收到了一個 IP 地址,現在已經連接到 LAN。 但是,當我嘗試從瀏覽器訪問相同的 IP 以接收 HTML 頁面時,沒有返回任何響應,並且瀏覽器在返回站點無法訪問消息之前需要大約 30 秒顯示加載。 我從連接到同一 LAN 的筆記本電腦和 iPhone 上嘗試了不同的瀏覽器,但沒有收到 web 頁面。 任何關於問題可能是什么的提示都非常感謝。

在實驗過程中,請參見下面的代碼和以太網屏蔽的圖片。

        /*
      Web Server

     A simple web server that shows the value of the analog input pins.
     using an Arduino Wiznet Ethernet shield.

     Circuit:
     * Ethernet shield attached to pins 10, 11, 12, 13
     * Analog inputs attached to pins A0 through A5 (optional)

     created 18 Dec 2009
     by David A. Mellis
     modified 9 Apr 2012
     by Tom Igoe
     modified 02 Sept 2015
     by Arturo Guadalupi

     */

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

    // Enter a MAC address and IP address for your controller below.
    // The IP address will be dependent on your local network:
    byte mac[] = {
      0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
    };
    IPAddress ip(192, 168, 1, 177);

    // Initialize the Ethernet server library
    // with the IP address and port you want to use
    // (port 80 is default for HTTP):
    EthernetServer server(80);

    void setup() {
      // Open serial communications and wait for port to open:
      Serial.begin(9600);
      while (!Serial) {
        ; // wait for serial port to connect. Needed for native USB port only
      }


      // start the Ethernet connection and the server:
      Ethernet.begin(mac, ip);
      server.begin();
      Serial.print("server is at ");
      Serial.println(Ethernet.localIP());
    }


    void loop() {
      // listen for incoming clients
      EthernetClient 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) {
              // send a standard http response header
              client.println("HTTP/1.1 200 OK");
              client.println("Content-Type: text/html");
              client.println("Connection: close");  // the connection will be closed after completion of the response
              client.println("Refresh: 5");  // refresh the page automatically every 5 sec
              client.println();
              client.println("<!DOCTYPE HTML>");
              client.println("<html>");
              // output the value of each analog input pin
              for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
                int sensorReading = analogRead(analogChannel);
                client.print("analog input ");
                client.print(analogChannel);
                client.print(" is ");
                client.print(sensorReading);
                client.println("<br />");
              }
              client.println("</html>");
              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(1);
        // close the connection:
        client.stop();
        Serial.println("client disconnected");
      }
    }

在此處輸入圖像描述

對於后來遇到同樣問題的人來說,互聯網路由器的 DHCP 似乎存在問題。 為通過以太網端口連接的設備提供 IP 地址時,我的路由器的 DHCP 似乎不穩定。 我更換了路由器,問題就消失了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM