arduino-libraries / Ethernet

Ethernet Library for Arduino
http://arduino.cc/
261 stars 264 forks source link

W5500 loses sockets after a random time since boot, and other bugs #92

Open RileyStarlight opened 5 years ago

RileyStarlight commented 5 years ago

Hello

I've been struggling for several months with a prototype that uses the W5500 and I can't get it to work properly. The assembly is an ATmega2560 and a W5500 and I'm working on official shields (Arduino ethernet shield 2, wiznet W5500 ethernet shield), Chinese modules and own prototypes following the datasheet and the application note. But none of them work properly, not even with the example codes and I'm going mad because I don't understand what the hell is going on. This development is an update of a previous development done with the W5100, which worked correctly (done with version 1.0.4 of the ethernet library). The final design will be running 24/7, so reliability is a requirement.

I am finding several strange bugs, which occur in an indeterminate period from the start of the program, which can be from minutes to more than a week:

Besides, I've found other faults:

I am not able to isolate the source of the problem, because things that work for some boards do not work for others, there being no common nexus. As I use several mega arduino host boards, I use the same HEX program for all of them, so as not to introduce discrepancy in the different tests. I have also tried to vary (lower) the spi clock frequency, with no apparent change except that it increases data corruption, when there is any.

In my system there are no other SPI devices that can affect communication or other tasks in the firmware.

The code I use for the tests is the following, opening several instances of it.

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10, 42, 0, 177);
IPAddress myDns(10, 42, 0, 4);
IPAddress gateway(10, 42, 0, 4);
IPAddress subnet(255, 255, 0, 0);

EthernetServer server(23);

void setup() {
    // W5500 reset
    pinMode(9, OUTPUT);
    digitalWrite(9, LOW);
    delay(10);
    digitalWrite(9, HIGH);
    Ethernet.init(53);  // My board
    //Ethernet.init(10);  // Most Arduino shields
    Ethernet.begin(mac, ip, myDns, gateway, subnet);
    Serial.begin(9600);
    while (!Serial);
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
        Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
        while (1);
    }
    else Serial.println("Ethernet shield was found. =)");
    if (Ethernet.linkStatus() == LinkOFF) Serial.println("Ethernet cable is not connected.");
    else if (Ethernet.linkStatus() == LinkON) Serial.println("Ethernet cable is connected.");
    else Serial.println("Link status unknown.");
    server.begin();
    Serial.print("Chat server address:");
    Serial.println(Ethernet.localIP());
}

void loop() {
    static boolean alreadyConnected = false;
    char elapsed_time[64];
    static unsigned long t = 0;
    static time_t counter = 0;
    tm counter_time;

    EthernetClient client = server.available();

    if (client) {
        if (!alreadyConnected) {
            client.flush();
            Serial.println("We have a new client");
            client.println("Hello, client!");
            alreadyConnected = true;
        }
        if (client.available() > 0) {
            char thisChar = client.read();
            server.write(thisChar);
            Serial.write(thisChar);
        }
    }
    if (millis() - t >= 1000) {
        t = millis();
        counter++;
        memcpy(&counter_time, localtime(&counter), sizeof (struct tm));
        strftime(elapsed_time, 64, "Running for %H hours, %M minutes, %S seconds of day %j.", &counter_time);
        server.println(elapsed_time);
    }
}

Can you think of what might be happening? I've searched a lot about it and I'm surprised that I haven't found solutions but neither have I mentioned this kind of errors on a chip that is widely used. I am absolutely desperate and lost with this. I have seen that there are other issues that mention things similar to those that happen to me, but not all of them, and no solution yet.

SapientHetero commented 5 years ago

A couple of quick observations - if you use the same HEX file on multiple devices on the same network, then each will have the same MAC and IP address. That won't work; each device on the same network segment must have a unique MAC and a unique IP address in order to communicate. I suspect you'll find bit 7 of the W5500's Interrupt Register (IR) is set to indicate an IP Conflict has been detected.

Second, both the Arduino Ethernet Shield 2 and the Wiznet W5500 shield have a SD card reader that shares the SPI bus with the W5500. Whether you use it or not, you should set its CS pin as an output and set that pin to HIGH in setup() to prevent SPI communication problems.

Try addressing these 2 issues and see if it doesn't help.

RileyStarlight commented 5 years ago

Thank you for your answer, both issues I have already taken in consideration:

SapientHetero commented 5 years ago

I know from your posts on the Wiznet W5500 forum that you had issues with overheated solder joints, but wondered if you still plan to experiment with my new version of the library?

RileyStarlight commented 5 years ago

I know from your posts on the Wiznet W5500 forum that you had issues with overheated solder joints, but wondered if you still plan to experiment with my new version of the library?

Yes, I plan to try it out when I finish a hardware design from another part of the same project.

MadDogMayCry0 commented 2 years ago

@kwendenarmo hi! Did you fix it? It's seems that my w5500 has same issue :(

RileyStarlight commented 2 years ago

@MadDogMayCry0 This project was quite a while ago and I don't quite remember all the details, but I do know that a lot of the problems were solved when the chip was soldered at the factory, and not soldered manually. Apparently it is VERY sensitive to this, something that had not happened to me with the previous W5100.

What I know for sure is that all of this was fixed in the final version, not the prototype, and that the project has been running uninterruptedly for several years without incident.

I'm not saying that all these problems were caused by the hardware, nor am I saying that these possible bugs are solved, I'm saying that after making the boards with PCBA and doing a thousand code tests, we managed to make it work somehow.

shahkaush commented 6 months ago

I am facing most of the issues mentioned by you. Did you have to do any modifications to the Ethernet.h or W5100.h libraries? Or only the manual soldering was the culprit?

RileyStarlight commented 6 months ago

As far as I remember, all the problems we had were solved when we manufactured with PCBA. I would say we didn't make any library modifications.

SapientHetero commented 6 months ago

If you want to try my improved version of the W5500 library, you can find it at https://github.com/SapientHetero/Ethernet