Arduino以太网在30秒后失去连接

时间:2014-02-07 14:05:29

标签: arduino ethernet

我有一个带有Wiznet以太网盾的Arduino Uno,它可以很好地连接到路由器。但是经过一段时间(大多数时间),它失去了与路由器的连接并脱机。有人在代码中看到它的原因吗?

#include <Ethernet.h>
#include <SPI.h>
boolean reading = false;

////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
  byte ip[] = { 192, 168, 178, 99 };
  //byte gateway[] = { 192, 168, 178, 1 };
  //byte subnet[] = { 255, 255, 255, 0 };
  byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  // if need to change the MAC address (Very Rare)
  EthernetServer server = EthernetServer(80); //port 80
  const int ledPin =  2; // LED Pin 
////////////////////////////////////////////////////////////////////////

void setup(){
  pinMode(ledPin, OUTPUT); //Pins 10,11,12 & 13 are used by the ethernet shield
  digitalWrite(ledPin, LOW);
  Ethernet.begin(mac);
  server.begin();
}

void loop(){
  checkForClient();  // listen for incoming clients, and process qequest.
}

void checkForClient(){
  EthernetClient client = server.available();
  if (client) {

    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    boolean sentHeader = false;

    while (client.connected()) {
      if (client.available()) {

        if(!sentHeader){
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          sentHeader = true;
        }

        char c = client.read();

        if(reading && c == ' ') reading = false;
        if(c == '?') reading = true; //found the ?, begin reading the info

        if(reading){
           switch (c) {
            case 'on':
              digitalWrite(ledPin, HIGH); 
              break;
            case 'off':
              digitalWrite(ledPin, LOW); 
              break;
          }

        }

        if (c == '\n' && currentLineIsBlank)  break;

        if (c == '\n') {
          currentLineIsBlank = true;
        }else if (c != '\r') {
          currentLineIsBlank = false;
        }

      }
    }

    delay(1); // give the web browser time to receive the data
    client.stop(); // close the connection:

  } 

}

1 个答案:

答案 0 :(得分:0)

你的Arduino是否接听来自路由器的传入ARP请求?似乎它没有,但它应该。