Electronic – Arduino EthernetServer with interrupt

arduinoethernetinterruptsprogramming

I have an arduino application doing two things in the loop:

  1. A webserver that waits for a connection and renders some html forms to change configuration values.
  2. Process some external data and show them on an matrix of leds.

The problem is that the processing of the data takes much time (~20 seconds). So while this time, the webserver can't do anything. My programm structure looks like this:

EthernetServer server(80);

void loop() {
  webServer()

  processAndShowData();
}

void webServer() {
  EthernetClient serverClient = server.available();

  if (serverClient) {
    while (serverClient.connected() {
      // handle the web server stuff in here
    }
  }
}

So my question is, if there is any posibility to use a interrupt handler for handling the web server stuff? Then the web server could react while the other task is running.

UPDATE: I now also tried to attach an interrupt to the ethernet pins (using a Mega this are 50, 51 and 52), but this doesn't work too. I tried sth. like this:

attachInterrupt(50, demoFunction, CHANGE);

But the method does not get called, even when changing the pins to 51 or 52 on both, input from a client like a HTTP request and output sending HTML to a browser.

Best Answer

In Arduino sketches, the loop() function is called repeatedly. Your loop function calls webServer() which then blocks in a while loop.

Instead, use if (serverClient.connected()) in your loop and implement your web server logic as a state machine capable of doing a little work then returning control to the main loop.

This way, your program will not block execution.

Related Topic