3
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Oh sry if the question was not clear. processAndShowData(); blocks, not the webServer(); so while the processing of the data, the web server can't react to responses. (Actually I try to do the processing with interrupts) \$\endgroup\$ – tbraun89 Oct 9 '12 at 9:44
  • 1
    \$\begingroup\$ So, break up processAndShowData() into a state machine that does a little work each time it's called \$\endgroup\$ – Toby Jaffey Oct 9 '12 at 9:52
  • \$\begingroup\$ Tried this, this works for the most cases, but not for mine. Because this part of the code draws to an matrix of leds each loop the text on the matrix is moved 1 pixel (LED) to the left. But when I use the web server now the led matrix starts to lag while the server sends its response. \$\endgroup\$ – tbraun89 Oct 9 '12 at 13:07
  • 1
    \$\begingroup\$ Drive the LED display from a timer's interrupt context (arduino.cc/playground/code/timer1). Use a state machine in your loop() function to update a global variable/array with the data to be written. \$\endgroup\$ – Toby Jaffey Oct 9 '12 at 13:48
0
\$\begingroup\$

For all others with an equal problem here is what I did:

#include <SPI.h>
#include "TimerOne.h"

void setup() {
  Timer1.initialize(200000);
  Timer1.attachInterrupt(displayInterrupt);
}

void loop() {
  webServer();
}

void displayInterrupt() {
  processAndShowData();
}

processAndShowData is now no blocking method anylonger, each time the interrupt is called a small pice of data is processed.

To use the timer interrupt I added the TimerOne Library.

Hope this will help someone.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.