1
votes

I have an Arduino Uno with a Wiznet Ethernet Shield and it connects fine to the router. But after some time (most 30s) it loses the connection to the router and goes offline. Do somebody see the reason for it in the code?

#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
What do you mean by "goes offline"? Stops responding to http requests, stops responding to ARP requests, router no longer detects link on that port, or perhaps something else?Craig
Not a complete answer, but variable c is a char. It will never match any of the switch cases.jdr5ca

1 Answers

0
votes

Does your Arduino answer incoming ARP requests from router? Seems like it doesn't, but it should.