Auto-refresh a page leads to hang the whole board

Post here first, or if you can't find a relevant section!
Post Reply
Step73
Posts: 27
Joined: Tue Jan 14, 2020 2:55 pm

Auto-refresh a page leads to hang the whole board

Post by Step73 »

I wrote a minimal web-server using a Nucleo F429ZI board:

Code: Select all

#include <LwIP.h>
#include <STM32Ethernet.h>

const int WEB_PORT = 80;
EthernetServer server(WEB_PORT);
char buffer[1024];

void setup() 
{
    Serial.begin(115200);
    // define variables
    Ethernet.begin(localIP, netmask, dns, gateway);
    server.begin();
}

void loop() 
{
    processServer();
}

void processServer()
{
  String readString; 
  EthernetClient client = server.available();
  if (client) 
  {
    while (client.connected()) 
    {
      if (client.available()) 
      {
        char c = client.read();
        readString += c; 

        if (c == '\n')
        {
          if (readString.startsWith("GET"))
          {
            readString.remove(0, 4);
            int pos = readString.indexOf(' ');
            String path = readString.substring(0, pos);

            if (path == "/status") pageStatus(client);
            break;
          }
        }
      }
    }
    delay(1);
    client.stop();
  }
}

void pageStatus(EthernetClient client)
{
    sectionHeader(client, 2);
    client.println("<html>");
    client.println("<style>table{font-family: verdana, sans-serif;border-collapse: collapse;width: 30%;}");
    client.println("td, th{border: 1px solid #dddddd;text-align: left;padding: 8px;}tr:nth-child(even)");
    client.println("{background-color: #dddddd;}</style>");
    client.println("<table>");
    client.println("<tr><th>Servo</th><th>Position</th><th>Running</th><th>Initialized</th></tr>");

    for (int i = 0; i < getParamUInt8(P_SERVO_SIZE); i++) 
    {
        sprintf(buffer, "<tr><td>%u</td><td>%d</td><td>%s</td><td>%s</td></tr>", i + 1, (int16_t) _servo[i].current, _servo[i].ended ? "NO" : "YES", _servo[i].initialized ? "YES" : "NO" );
        client.println(buffer);
    }
    client.println("</table>");

    client.println("<br><hr><br>");

    client.println("<table>");
    client.println("<tr><th>Led</th><th>PWM</th><th>Running</th></tr>");

    for (int i = 0; i < getParamUInt8(P_LED_SIZE); i++) 
    {
        sprintf(buffer, "<tr><td>%u</td><td>%d</td><td>%s</td></tr>", i + 1, (int16_t) _led[i].pwm, _led[i].ended ? "NO" : "YES");
        client.println(buffer);
    }
    client.println("</table>");

    client.println("<br><hr><br>");

    client.println("<table>");
    client.println("<tr><th>Stepper</th><th>Position</th><th>Running</th></tr>");

    for (int i = 0; i < getParamUInt8(P_STEPPER_SIZE); i++) 
    {
        sprintf(buffer, "<tr><td>%u</td><td>%d</td><td>%s</td></tr>", i + 1, (int16_t) _stepper[i].stepper.getCurrentPositionInMillimeters(), _stepper[i].stepper.motionComplete() ? "NO" : "YES");
        client.println(buffer);
    }
    client.println("</table>");
    client.println("</html>");
}

void sectionHeader(EthernetClient client, int refresh)
{
    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/html");
    client.println("Connection: close");
    if (refresh) client.println("Refresh: 2"); 
    client.println();
    client.println("<!DOCTYPE HTML>");
}
As you can see in the sectionHeader() I want to refresh the page every 2 seconds. It works, but after 3 or 4 refreshes it hangs and I have to reset the whole board. Of course with an embedded system I would use Ajax... here I thought reloading the page should be enough.

What might cause such a hang?
User avatar
fpiSTM
Posts: 1757
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Auto-refresh a page leads to hang the whole board

Post by fpiSTM »

Step73 wrote: Tue Jan 14, 2020 2:58 pm What might cause such a hang?
That is a good question.
This should request some debug. You should open an issue to the STM32Ethernet repository to track this.
Post Reply

Return to “General discussion”