I have an Arduino webserver that displays many buttons over a webpage (73 at the moment) that toggle different switches. I shortened up the button-drawing code with this function:

void displayButton(String toggleName, byte toggle, byte link1, EthernetClient & client, boolean last) {
  if (toggle == 0) client.print("<a href=\"/?");
  else client.print("<a class=\"linkSelected\" href=\"/?");
  if (last) client.print("<br><br>");

And this is a shortened version of my loop function:

void loop() {
  EthernetClient client = server.available();
  if (client) {
    boolean done = false;
    while (client.connected() && !done)
      while (client.available () > 0 && !done)
        done = processIncomingByte (client.read ());
    }  // end of while client connected
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      displayButton("F8", f8Toggle, 57, client, 0);
      displayButton("F9", f9Toggle, 58, client, 0);
      displayButton("F10", f10Toggle, 59, client, 1);
      delay(5); client.stop();
  } // end of "if client"
} // end of loop

I'm wondering if there's any way to have the displayButton function use the EthernetClient instance without it having to be passed every time. I'm interested in this in order to clean up my code a little (since I would be able to remove 73 instances of the word "client"), and also as a learning experience.

  • If client is global then it already knows about it. – Majenko Oct 31 '15 at 13:43
  • When I tried to remove the passed client, I was getting "'client' was not declared in this scope" for the displayButton function. Would it affect the webserver at all if I defined "EthernetClient client;" before the Setup and Loop functions? I just tested this out, and it seems to work, but I feel hesitant about it because I've never seen that in any other webserver code. – Jerry Oct 31 '15 at 14:12
  • As long as you remove the EthernetClient from before the client in loop(). – Majenko Oct 31 '15 at 14:13
  • Thanks! It looks like this is a great solution! I'll check back later, and if there are no answers then I'll post your answer as the solution. – Jerry Oct 31 '15 at 14:54
  • What if two clients connect at the same time? It looks like you need two different client instances, one for each client. Am I right? – user31481 Feb 6 '18 at 21:25

As Majenko has noted in the comments, the solution was to declare "EthernetClient client;" as a global variable, outside of the loop. Normally I would've tried something like this on my own, but was hesitant to do so because I had never seen anything like that in other code.

| improve this answer | |
  • 1
    Using global variables is usually considered a sign of poor design (it can leaves you open to some kinds of errors that local variable are less subject to), is why you don't see it very often. The client variable in your program is one of the exceptions, where globalizing it probably reduces, rather than increases, the potential for errors. – JRobert Dec 31 '15 at 14:38

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.