0

I need to modify the WiFiWebServer example on the Arduino website but I am having some trouble understanding the code. I am hoping to get some advice from the community on the client-server relationship in this example so I can better modify it to suit my needs.

The problem I am having is the jump between server commands and client commands. Initially I see server commands; server.begin() in setup and server.available() in the loop. The commands quickly shift, however, as nearly all the other commands in the loop become client.println()'s.

I understand the initial client commands that deal with whether the client is available and whether it is still connected. The part I don't understand, however, are the client.read and client.println commands. Even client.read makes some sense to me since I want some communication from the client side but why does the example use client.print? The Arduino webpage defines the WiFiClientPrintln as data you write from the client to the server that it is connected to. Wouldn't I want to use server.print since the Arduino in this example is the server and the client is the computer I use to view the webpage that I built on the server?

I am using the WiFIWebServer example as a template for my project. The goal is to have the Arduino take sensor readings and post them on a webpage it hosts by acting on the server. This is quite similar to the example but I notice that I have slow update/transmission speeds and errors when a different client connects to the server. For example, if I view the website on my computer, I get the data fairly consistently If somewhat slow. If I try to view the same website off my mobile device, however, I have problems connecting, may need to refresh several times and the webpage crashes on the computer.

I am trying to troubleshoot the issue but going through the code I can't figure out the reason for the client.println()'s instead of server.println(). Even if that isn't the cause of my multi-client issue, I would like a better understanding of the code before going forward so would appreciate any guidance you can provide.

1

The confusion you have arises from the context of the command.

The server.* commands manipulate the server instance - that is, the actively listening socket that is waiting for connections.

The client.* commands change their meaning depending on if you are a server waiting for connections or a client that is making a connection.

In the latter case client.print() sends data to the server.

In the former case, on a server, the client.* commands refer to a specific client that has made a connection to the server. Using client.print() in this instance sends data to the currently connected client.

So "client" can be thought of as "the connection between me and the remote end" however that connection was created - either by "me" specifically connecting to the remote end, or the remote end connecting to my "server" instance and a new "client" being spawned from it for this one connection.

Remember - a client can only connect to one server, but a server can have many connections from many different clients, and it is those client connections that you talk to.

0

client.println() is sending data to a specific client.

server.println() is sending data to every client that is connected to the server.

Also, i believe that the arduino keeps sending data to the client instead of stopping the request. You should have some javascript that updates the value say... every 5 seconds or so... Or you could have a

<meta http-equiv="refresh" content="5">

inside the part which would tell the browser to refresh the page every 5 seconds instead of using the header

See http://tentencraft.net/demo1.php vs http://tentencraft.net/demo2.php

  • Thank you for the response and the suggestions. I initially assumed the same about sending data to every client and refreshing but it does not produce the desired effect. I set updates to every second but it feels like it takes something like 30s updating on the computer and even worse on a mobile device. I have to force the mobile device's reload more often than not. To circle back to the server.println(), if that is the command that sends data to every client on the server, wouldn't that make it the one to use for multi-client setups. Would client.read/available be affected? Thanks again – Yusif_Nurizade Feb 25 '15 at 22:24
  • @DaveP, forgive me for not following the proper forum etiquette but I am not able to upvote until I have 15 rep. While the reply was useful, my question has not yet been answered since I still don't understand why a WiFiWebServer example would use a client.print versus a server.print. – Yusif_Nurizade Feb 27 '15 at 15:09
  • You would want to refresh it instead of sending more data unless you want it to keep logging the temperature to the client. If you add refresh code to the top then close the connection it will update the web page every x seconds. – tenten8401 Feb 28 '15 at 19:45
  • @user3303997 my code, which follows the Arduino WebServer example, includes two lines "Connection: close" and "Refresh : 1". Based on the example notes, this means that it closes the connection after the completion of the response and refreshes every second. Despite the setting, the refresh rate is nowhere near 1 second - do I need to close the connection in Arduino code? I thought the http code was sufficient. – Yusif_Nurizade Mar 6 '15 at 17:10
  • I suggest using the meta refresh i posted above and close the connection in arduino. – tenten8401 Mar 7 '15 at 16: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.