WebServer errors - Core Basic + LAN 13.2 (W5500)



  • Hi all,
    I have a webserver project in PlatformIO where i want to use the Core Basic together with LAN Module 13.2 (w5500). When I run the code below, I run into several problems:

    1. Wrong webserver IP. If I restart M5Stack with restart button, sometimes gives a different ip address to the server than what I add in setup(). This wrong IP are always 0.0.0.0, 192.168.0.176, 192.168.0.177, 192.168.1.176
    2. Client crash. When I can connect the device through ethernet cable and I received HTML page with 5 second autorefresh on pc. Client crash a few minute later.

    This code is M5Stack LAN 13.2 example from here: https://github.com/m5stack/M5Module-LAN-13.2/tree/main/examples/WebServer with minor changes. (direct ip and refresh_counter)

    Any help of advice would be gratefully received. I've tried looking online but can't seem to find anything!

    #include <Arduino.h>
    #include <M5Unified.h>
    #include <M5GFX.h>
    #include <SPI.h>
    #include <M5Module_LAN.h>

    #define THEME_COLOR 0x0760

    uint8_t cs_pin;
    uint8_t rst_pin;
    uint8_t int_pin;

    byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x89};
    IPAddress ip(192, 168, 1, 177);
    IPAddress dns(192, 168, 1, 1);
    IPAddress sn(255, 255, 255, 0);
    IPAddress gw(192, 168, 1, 1);

    M5Module_LAN LAN;
    EthernetServer server(80);

    int screen_height;
    int screen_width;
    int refresh_counter;

    void setup() {
    M5.begin();
    M5.Display.begin();
    M5.Display.setTextDatum(middle_center);
    M5.Display.setTextColor(WHITE);
    M5.Display.setTextFont(&fonts::FreeSansBoldOblique18pt7b);
    screen_height = M5.Display.height();
    screen_width = M5.Display.width();
    M5.Display.fillSmoothRoundRect(2, screen_height / 2 - 20, screen_width - 4,
    40, 4, THEME_COLOR);
    M5.Display.drawString("Wait For Ethernet", screen_width / 2,
    screen_height / 2);

    m5::board_t board = M5.getBoard();
    switch (board) {
        case m5::board_t::board_M5Stack: {
            cs_pin  = 5;
            rst_pin = 0;
            int_pin = 35;
        } break;
        case m5::board_t::board_M5StackCore2: {
            cs_pin  = 33;
            rst_pin = 0;
            int_pin = 35;
        } break;
        case m5::board_t::board_M5StackCoreS3: {
            cs_pin  = 1;
            rst_pin = 0;
            int_pin = 10;
        } break;
    }
    
    SPI.begin(SCK, MISO, MOSI, -1);
    
    LAN.setResetPin(rst_pin);
    LAN.reset();
    LAN.init(cs_pin);
    LAN.begin(mac, ip, dns, gw, sn);
    
    
    // start the server
    server.begin();
    Serial.print("server is at ");
    Serial.println(Ethernet.localIP());
    
    M5.Display.drawString("server is at:", screen_width / 2,
                          screen_height / 2 - 40);
    M5.Display.fillSmoothRoundRect(2, screen_height / 2 - 20, screen_width - 4,
                                   40, 4, THEME_COLOR);
    M5.Display.drawString(Ethernet.localIP().toString().c_str(),
                          screen_width / 2, screen_height / 2);
    

    }

    void loop() {
    // listen for incoming clients
    EthernetClient client = server.available();
    if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
    if (client.available()) {
    char c = client.read();
    Serial.write(c);
    // if you've gotten to the end of the line (received a newline
    // character) and the line is blank, the http request has ended,
    // so you can send a reply
    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(
    "Connection: close"); // the connection will be closed
    // after completion of the
    // response
    client.println("Refresh: 5"); // refresh the page
    // automatically every 5 sec
    client.println();
    client.println("<!DOCTYPE HTML>");
    client.println("<html>");
    client.print("<h2>Hello M5Stack LAN Module! Refresh count: " + String(refresh_counter) + "</h2>");
    client.println("</html>");
    refresh_counter++;
    break;
    }
    if (c == '\n') {
    // you're starting a new line
    currentLineIsBlank = true;
    } else if (c != '\r') {
    // you've gotten a character on the current line
    currentLineIsBlank = false;
    }
    }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
    }
    }



  • Hello @kokany

    I am running your code and cannot reproduce either of your issues. I've reset M5Stack multiple times (20) and it always used the correct IP address. I also tried to provoke the client crash (tested with Chrome and Firefox), I even increased the refresh to 1 second, but I don't see a crash.

    BTW: by client crash, you mean the browser, correct?

    Maybe it's a power supply issue? Maybe try a different one?

    Thanks
    Felix



  • Hello @felmue

    I tested again this morning. I can run my code for about 15 minutes, then the problems reappear.

    I have 2 different devices (basic + lan module) and the issues is same.
    I tested some browsers. (Chrome, Opera, Edge)

    Client crash, I mean when I debug the running code and the "crash" appaers then the client.connected() is true, but client.available() is false. And then the client.available() never go true (manually refreshing in browser or restart the device doesn't help)

    Is overheating possible?

    Thanks
    Tom



  • @kokany more Likely a memory error or a dhcp addressing issue



  • hello @ajb2k3

    I set up direct IP, but at now I tested this code with dhcp service disabled and the issue is same after 11 minutes running.



  • Hello @kokany

    I have the code running for more than two hours now - with no issues.

    That said, your debugging suggests a case where a client (browser) connects but then never sends any data which keeps the execution locked up inside the while loop. I guess you'll need to look into some measures to make the code more robust. Maybe try to add some timeout which when expired allows to exit the while loop as well?

    Thanks
    Felix



  • Hello @felmue

    Of course, I'm already beyond what you advised. I add timeout counter inside while loop, if client not available. It exits the while loop after 10 timeouts. After that the client is connected, but it never available again.
    If I trun off the device for some minutes and turn on, it work again for 3-5 refresh cycle.
    If I trun off the device for some hours and turn on, it work again for 10-15 minutes.
    This phenomenon is very interesting and I don't understand how it can be.

    I checked network tab on development window of browser. The request sent but not received and it pending "forever".

    I know it's unbelievable, but I can conjure up these phenomena at any time.
    I tested it on 2 different M5Stack Basic Cores, 2 different Lan 13.2 modules, on 2 different laptops with 4 different browsers. The device and the laptop are always connected directly, there is no router or switch between them. I used the power supply from usb and adapter.

    Thanks
    Tom



  • Hello @kokany

    hmm, I see. Well, at this point I am out of ideas, sorry.

    Thanks
    Felix



  • Hello @felmue

    Finally I solved the problem.

    The solution is as follows:
    in libdeps M5-Ethernet\src\utility\w5100.h must modify SPISettings clock param to 8000000. (in line 21-22)

    It's working fine now.

    Thanks for everything
    Tom



  • Hello @kokany

    interesting... and thank you for sharing.

    Thanks
    Felix