Ethernet Shield looses the link after few seconds

I'm collecting data using a web server with pages stored in the SD.

I copied a adapted a code found in Internet. However seems won't work. After few seconds the Link is lost. And I'm not able to ping Arduino

Any idea? Thank you

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


//
int IP0 = EEPROM.read(0); // first IP octet
int IP1 = EEPROM.read(1);
int IP2 = EEPROM.read(2);
int IP3 = EEPROM.read(3);
int NT0 = EEPROM.read(4); // first octet netmask
int NT1 = EEPROM.read(5);
int NT2 = EEPROM.read(6);
int NT3 = EEPROM.read(7);





// size of buffer used to capture HTTP requests
#define REQ_BUF_SZ   20

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {IP0, IP1, IP2, IP3};   // IP address stored in the EEProm
byte subnet[] = {NT0, NT1, NT2, NT3};
//byte gateway[] = {GW0, GW1, GW2, GW3};
EthernetServer server(80);       // create a server at port 80
File webFile;                    // handle to files on SD card
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0;              // index into HTTP_req buffer

void setup()
{
    // disable Ethernet chip
    //pinMode(10, OUTPUT);
    //digitalWrite(10, HIGH);
    
    Serial.begin(9600);       // for debugging
    Ethernet.begin(mac, ip, subnet);  // initialize Ethernet device
    // initialize SD card
    Serial.println("Initializing SD card...");
    if (!SD.begin(4)) {
        Serial.println("ERROR - SD card initialization failed!");
        return;    // init failed
    }
    Serial.println("SUCCESS - SD card initialized.");
    // check for index.htm file
    if (!SD.exists("index.htm")) {
        Serial.println("ERROR - Can't find index.htm file!");
        return;  // can't find index file
    }
    Serial.println("SUCCESS - Found index.htm file.");
    server.begin();           // start to listen for clients
    Serial.println("IP Address:");
    Serial.println(Ethernet.localIP());    
}

void loop()
{
    EthernetClient client = server.available();  // try to get client

    if (client) {  // got client?
        Serial.println("new client");
        bool currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {
                 // client data available to read
                char c = client.read(); // read 1 byte (character) from client
                // buffer first part of HTTP request in HTTP_req array (string)
                // leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
               if (req_index < (REQ_BUF_SZ - 1)) {
                    HTTP_req[req_index] = c;          // save HTTP request character
                    req_index++;
                }
                Serial.print(c);    // print HTTP request character to serial monitor
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                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("Connnection: close");
                    client.println();
                    // open requested web page file
                    if (StrContains(HTTP_req, "GET / ")
                                 || StrContains(HTTP_req, "GET /index.htm")) {
                        webFile = SD.open("index.htm");        // open web page file
                    }
                    if (StrContains(HTTP_req, "GET /set_data.htm")) {
                        webFile = SD.open("set_data.htm");        // open web page file
                    }
                    if (StrContains(HTTP_req, "GET /set_eth.htm")) {
                        webFile = SD.open("set_eth.htm");        // open web page file
                    }
                    if (StrContains(HTTP_req, "GET /index.htm")) {
                        webFile = SD.open("index.htm");        // open web page file
                    }
                    // send web page to client
                    if (webFile) {
                        while(webFile.available()) {
                            client.write(webFile.read());
                        }
                        webFile.close();
                    }
                    // reset buffer index and all buffer elements to 0
                    req_index = 0;
                    StrClear(HTTP_req, REQ_BUF_SZ);
                    break;
                }
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                } 
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
                }
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop();
        Serial.println("client disconnected"); // close the connection
    } // end if (client)
}

// sets every element of str to 0 (clears array)
void StrClear(char *str, char length)
{
    for (int i = 0; i < length; i++) {
        str[i] = 0;
    }
}

// searches for the string sfind in the string str
// returns 1 if string found
// returns 0 if string not found
char StrContains(char *str, char *sfind)
{
    char found = 0;
    char index = 0;
    char len;

    len = strlen(str);
    
    if (strlen(sfind) > len) {
        return 0;
    }
    while (index < len) {
        if (str[index] == sfind[found]) {
            found++;
            if (strlen(sfind) == found) {
                return 1;
            }
        }
        else {
            found = 0;
        }
        index++;
    }

    return 0;
}

I'm building a controller that upon some calculation opens or closes a relay. I drive the relay via a simple circuit like the following:

Arduino is powered via USB and the 12 Source from a external Power supply. I put the GND together.

Now.. it seems that this damages the Ethernet Shield.
How can Be possible?

Thank you
M.

most relays will include an optocoupler to ensure full galvanic isolation

What current goes through your 2N22222? (it can only dissipate 500 or 600mW if I remember correctly)

the relay coil I is about 70/80 mA @ 12V. too much?
How can effect the Ethernet Shield health?

The relay is a "Standard relay" no opto isolation

can you share the circuit and the code?
which arduino?

Arduino Mega. The code is wide...
However I've just started another topic asking if the code can be related with this failure:

it's not good to have two topics related to the same issue.
let's keep the conversation there then. I'm joining both discussions

if you want to test if the issue is related to the relay, write a very simple code using the relay and ethernet and see if you can reproduce the error

Ok clear...
however, the problem is not the circuit but the code.
I attached a Brand New Ethernet Shield and a simple web server code (the example one) and it works.
Viceversa with the code above randomly the the Link drops and the lights on the connector switch off.

Any Idea?

but that's not using the relay. Add a blinking code using the relay in the loop for example

The code above doesn’t work.
The blinking part of it doesn’t matter

This proves the Ethernet works
But you did not activate the relay so if there is a current or EMF issue you did not see it.
So I don’t think you can exclude the hardware

I confirm that the Ethernet Shield works.
If I load the WebServer code you can find in the examples, It works.
The board is stand alone with nothing attached.

If I load the above code, randomly it looses the link.

Good

before incriminating your code above, my point was that the next step could be to see if the relay is creating issues.

so

  • plug and power the relay exactly as you did in the first code
  • take the "WebServer code you can find in the examples"
  • add in the loop the blink without delay code to flip the relay every second

run the code and see if it all works.

if it does then you have a solid ground to build upon. If it does not then you know the relay is creating some issues and that's what needs debugging

makes sense ?

The scenario is more complicated.
And I probably did a mistake at the beginning thinking that the relay driving was the problem.

The software consists in three parts: three namespaces

The first namespace is activated via digitalRead in two Pins.

The first namespace collects user data via a web form the second connects itself via telnet to a server that provides some variables. And after some calculations open or closes the relays.
the third makes simple operation forcing the relays as user wishes)

Basically the second (that uses ethernet link to a server) and the third (it doesn't use the Ethernet) namespaces work.

The part that collects the user data from the webform (that basically is the part above), does't work

confusing :slight_smile:

so you get a web request for a web page probably called "set_data.htm" but I don't see where you handle the submission from that web page

what I posted above is just the code I started to build the below.

namespace Setup_Mode 
{

  void setup1()
  {
    //digitalWrite(reset_pin, HIGH);
    //pinMode(reset_pin, OUTPUT); //Please disconnect the pin when Upload new sketch
    Serial.println("Start Ethernet");
    EthernetServer server(80);
    Ethernet.begin(mac,ip,subnet);// initialize Ethernet device
    EthernetClient client = server.available();  // try to get client
    // initialize SD card
    Serial.println("Initializing SD card...");
    if (!SD.begin(4)) 
    {
     Serial.println("ERROR - SD card initialization failed!");
     return;    // init failed
    }
     Serial.println("SD card initialized.");
       // check for index.htm file
      if (!SD.exists("index.htm")) 
      {
        Serial.println("ERROR - Can't find index.htm file!");
        return;  // can't find index file
      }
      Serial.println("SUCCESS - Found index.htm file.");
      delay(100);
      
      Serial.println("Server available at:");  
      Serial.println(Ethernet.localIP());
      Serial.println("Stored Values:");
      Serial.println(LowAng_A+LowAng_A180);
      Serial.println(HiAng_A+HiAng_A180);
      Serial.println(LowAng_B+LowAng_B180);
      Serial.println(HiAng_B+HiAng_B180);
      Serial.print("IP A:");    
      Serial.println(EEPROM.read(16)); 
      Serial.print("IP B:");    
      Serial.println(EEPROM.read(17));
     


  }

 void loop1()
  {
    digitalWrite(setup_err_pin, HIGH); //Led in Setup Mode
    //Serial.println("Search for Client");
    EthernetClient client = server.available();  // try to get client
    buttons();

    if (client) 
    {  // got client?
      boolean currentLineIsBlank = true;
         
        while (client.connected()) 
        { 
          

            if (client.available()) {   // client data available to read
                char c = client.read(); // read 1 byte (character) from client
              
                // buffer first part of HTTP request in HTTP_req array (string)
                // leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
                if (req_index < (REQ_BUF_SZ - 1)) {
                    HTTP_req[req_index] = c;          // save HTTP request character
                    req_index++;
                    readString += c;
                }
                Serial.print(c);    // print HTTP request character to serial monitor
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                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("Connnection: close");
                    client.println();
                    // open requested web page file
                    if (StrContains(HTTP_req, "GET / ")
                                 || StrContains(HTTP_req, "GET /index.htm")) {
                        webFile = SD.open("index.htm");        // open web page file
                    }
                    if (StrContains(HTTP_req, "GET /set_data.htm")) {
                        webFile = SD.open("set_data.htm");        // open web page file
                    }
                    if (StrContains(HTTP_req, "GET /set_eth.htm")) {
                        webFile = SD.open("set_eth.htm");        // open web page file
                    }
                    if (StrContains(HTTP_req, "GET /index.htm")) {
                        webFile = SD.open("index.htm");        // open web page file
                    }
                    if (StrContains(HTTP_req, "GET /?ang1")) { //setto angoli e rete delle antenne A e B
                        webFile = SD.open("thnx.htm");
                        ind1 = readString.indexOf('=');
                        Serial.println(readString);
                        
                        angstring1 = readString.substring(ind1+1,ind1+4);
                        LowAng_A = angstring1.toInt();
                        if (LowAng_A <= 180)
                        {
                          LowAng_A = LowAng_A;
                          LowAng_A180 = 0;
                        }
                        if (LowAng_A > 180)
                        {
                          LowAng_A = (LowAng_A - 180);
                          LowAng_A180 = 180;
                        } 
                        Serial.println(LowAng_A+LowAng_A180);              
                        
                        angstring2 = readString.substring(ind1+10,ind1+13);
                        HiAng_A = angstring2.toInt();
                        if (HiAng_A <= 180)
                        {
                          HiAng_A = HiAng_A;
                          HiAng_A180 = 0;
                        }
                        if (HiAng_A > 180)
                        {
                          HiAng_A = (HiAng_A - 180);
                          HiAng_A180 = 180;
                        }                        
                        Serial.println(HiAng_A+HiAng_A180);
                        
                        angstring3 = readString.substring(ind1+19,ind1+22);
                        LowAng_B = angstring3.toInt();
                        if (LowAng_B <= 180)
                        {
                          LowAng_B = LowAng_B;
                          LowAng_B180 = 0;
                        }
                        if (LowAng_B > 180)
                        {
                          LowAng_B = (LowAng_B - 180);
                          LowAng_B180 = 180;
                        }
                        Serial.println(LowAng_B+LowAng_B180);

                        angstring4 = readString.substring(ind1+28,ind1+31);
                        HiAng_B = angstring4.toInt();
                        if (HiAng_B <= 180)
                        {
                          HiAng_B = HiAng_B;
                          HiAng_B180 = 0;
                        }
                        if (HiAng_B > 180)
                        {
                          HiAng_B = (HiAng_B - 180);
                          HiAng_B180 = 180;
                        }                        
                        Serial.println(HiAng_B+HiAng_B180);
                        
                        ipantAstring = readString.substring(ind1+39,ind1+42);
                        ipantA = ipantAstring.toInt();
                        Serial.println(ipantA);
                        ipantBstring = readString.substring(ind1+50,ind1+53);
                        ipantB = ipantBstring.toInt();
                        Serial.println(ipantB);
                        EEPROM.write(12, LowAng_A);
                        EEPROM.write(22, LowAng_A180);
                        EEPROM.write(13, HiAng_A);
                        EEPROM.write(23, HiAng_A180);
                        EEPROM.write(14, LowAng_B);
                        EEPROM.write(24, LowAng_B180);                        
                        EEPROM.write(15, HiAng_B);
                        EEPROM.write(25, HiAng_B180);
                        EEPROM.write(16, ipantA);
                        EEPROM.write(17, ipantB);
                        readString="";
                               // open web page file
                    }
                    if (StrContains(HTTP_req, "GET /?DT0")) { //setto Rete del SAS
                        webFile = SD.open("thnx.htm");
                        ind1 = readString.indexOf('=');
                        Serial.println(readString);
                        ipstring0 = readString.substring(ind1+1,ind1+4);                 
                        ipstring1 = readString.substring(ind1+9,ind1+12);
                        ipstring2 = readString.substring(ind1+17,ind1+20);
                        ipstring3 = readString.substring(ind1+25,ind1+28);
                        netstring0 = readString.substring(ind1+33,ind1+36);
                        netstring1 = readString.substring(ind1+41,ind1+44);
                        netstring2 = readString.substring(ind1+49,ind1+52);
                        netstring3 = readString.substring(ind1+57,ind1+60);
                        
                  
                        Serial.println("trovato IP SAS");
                        Serial.println(ipstring0);
                        Serial.println(ipstring1);
                        Serial.println(ipstring2);
                        Serial.println(ipstring3);
                        Serial.println(netstring0);
                        Serial.println(netstring1);
                        Serial.println(netstring2);
                        Serial.println(netstring3);
                        
                        
                        IP0 = ipstring0.toInt();
                        IP1 = ipstring1.toInt();
                        IP2 = ipstring2.toInt();
                        IP3 = ipstring3.toInt();
                        NT0 = netstring0.toInt();
                        NT1 = netstring1.toInt();
                        NT2 = netstring2.toInt();
                        NT3 = netstring3.toInt();
                        
                        EEPROM.write(0, IP0);
                        EEPROM.write(1, IP1);
                        EEPROM.write(2, IP2);
                        EEPROM.write(3, IP3);
                        EEPROM.write(4, NT0);
                        EEPROM.write(5, NT1);
                        EEPROM.write(6, NT2);
                        EEPROM.write(7, NT3);
                        
                        readString="";
                               // open web page file
                    }
                    // send web page to client
                    if (webFile) {
                        while(webFile.available()) {
                            client.write(webFile.read());
                        }
                        webFile.close();
                    }
                    // reset buffer index and all buffer elements to 0
                    req_index = 0;
                    StrClear(HTTP_req, REQ_BUF_SZ);
                    break;
                }
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                } 
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
                }
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
    } // end if (client)
  
  }

} //end namespace Setup Mode

However... the ethernet link stops randomly even if I don't handle anything.
The ethernet server begins... I'm able to Ping it few times, then it stops

how is that integrated in the real code?

I found the issue:

SD and Ethernet shares the same bus and cannot be used at the same time.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.