[solved]Robust Ethernet connection

Hi,

I'm looking for a robust way to maintain an ethernet connection with a Wiz820 or Wiz850 Board. Especially I'm thinking of what happens if the ethernet cable is unplugged for a while. Initally my server starts with the desired ip adress, but when I unplug and reconnect the network cable the server starts with IP: 0.0.0.0. Do you have an idea how to reconnect successfully to my network with the desired IP? Waiting 10 seconds before starting ethernet_initserver() didn't help.

Thanks!

main program:

#include "NK_Eth.h"

#define BAUDRATE 115200


void setup() {
  // put your setup code here, to run once:
  delay(3000); //to allow user to open serial monitor
  Serial.begin(BAUDRATE);
  ethernet_setup();
}

void loop() {
  // put your main code here, to run repeatedly:
  ethernet_loop();
  delay(500);
}

NK_Eth.h:

#include "Ethernet.h"

uint8_t mymac[] = { 0x54, 0x10, 0xEC, 0x3B, 0x47, 0x5A };
IPAddress myip(172, 21, 24, 76);
//uint8_t ethernet_sspin = 10; //for 44CH_0015 use 10
EthernetServer myserver(80);

uint8_t ethernet_setup(void)
{
  SPI.begin();
  //Ethernet.init(ethernet_sspin); //if ss pin of W5200 isn't on teensy pin 10
  Ethernet.begin(mymac, myip);
  ethernet_gethardware();
  return 0;
}

uint8_t ethernet_loop(void)
{
  // DESCRIPTION

  // COMMENTS
  // if(server) is 1 as long as server is running and no data received. When data arrives it turns 0 for some time. -> Not useful to detect state of server.

  // State Detection
  if ((Ethernet.linkStatus() == LinkON) && (!myserver)) //start new server (due to pos. edge of link status and no running server)
  {
    debugln("Link is OK. No server running. -> Wait 10 seconds, then Starting server");
    delay(10000);
    ethernet_initserver();
  }
  else if (myserver) 
  {
    ethernet_checkclients();
  }
  else //might be an error -> print reason
  {
    if (Ethernet.linkStatus() != LinkON)
    {
      debugln("Error: No Link.");
    }

    if (!myserver)
    {
      debugln("Error: No Server.");
    }
  }
  return 0;
}

int ethernet_initserver(void)
{
  int returnvalue = -1;
  //Ethernet.begin(mymac, myip);
  //ethernet_gethardware();

  myserver.begin();

  if (myserver) //TRUE if myserver is listening for clients
  {
    Serial.print("Server started. IP: ");
    Serial.println(Ethernet.localIP());
    returnvalue = 1;
  }
  else
  {
    debugln("Starting server failed.");
    returnvalue = 0;
  }
  return returnvalue;
}


int ethernet_checkclients(void) //check for clients and read incoming data if available
{
  EthernetClient myclient = myserver.available(); //if an incoming client connects, there will be bytes available to read

  if (myclient) //if there is a client
  {
    while (myclient.connected())
    {
      while (myclient.available())
      {
        char char_read = myclient.read();
        Serial.print(char_read);
      }
      Serial.println();
    }
    myclient.stop();
  }

  return 0;
}


int ethernet_gethardware(void)
{
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    debugln("Ethernet shield was not found.");
  }
  else if (Ethernet.hardwareStatus() == EthernetW5100) {
    debugln("W5100 Ethernet controller detected.");
  }
  else if (Ethernet.hardwareStatus() == EthernetW5200) {
    debugln("W5200 Ethernet controller detected.");
  }
  else if (Ethernet.hardwareStatus() == EthernetW5500) {
    debugln("W5500 Ethernet controller detected.");
  }
  return (Ethernet.hardwareStatus());
}

If I disconnect the cable and then connect it back, the server still works. I don't call begin() again

Juraj:
If I disconnect the cable and then connect it back, the server still works. I don't call begin() again

Thanks Juraj, with my Teensy 3.2 and Wiz850 it didn't work always without using begin().

So, this is my suggestion for everyone who is looking for a robust way of reconnecting:

Use following after link is reconnected:

Ethernet.begin(mymac, myip);
myserver.begin();