Hardware Reference
In-Depth Information
Once the network coni guration has been made, the next step is to connect
to the server. This is done on line 31 using the connect() function. Once again,
the result is used to see if the Arduino has connected or not. If it has, then on
line 35 the sketch sends three lines to the web server. First, a GET instruction.
Second, the server name. Finally, an empty line to inform the web server that
there is nothing else you want to send. It should reply. If the connection wasn't
made, an error message is printed on the serial port.
The loop() function is declared on line 46. First it detects to see if any bytes
are waiting in the buffer using the available() command. If there is data
waiting, then each byte is read from the buffer and printed to the serial port.
This is done on lines 51 and 52. On line 56, the sketch checks to see if it is still
connected to the server; once the server responds with a web page, it is free to
terminate the connection before serving another client. If the server has indeed
terminated the connection, a message is printed to the serial port and the sketch
sleeps until a reset is performed.
Arduino as a Server
You can use the Arduino as a network client, but it is also a capable network
server. Instead of connecting to a server, it becomes a server, waiting for clients
to connect before sending or receiving information.
To use your Arduino as an Ethernet server, you must initialize the
EthernetServer object.
EthernetServer server = EthernetServer(port);
It takes one parameter: the port to listen for incoming connections. Web servers
connect to port 80 and telnet on port 23. Remember, ports below 1024 are reserved
for specii c applications, and ports above are free to be used. If you create your
own protocol, use one of the high ports.
To listen for a client, you must create an EthernetClient object.
EthernetClient client;
This function is nonblocking, that is to say, if a client is not available, the object
will still be created and the rest of the sketch will continue to run. To verify if
a client has actually connected, test the client object. If a client has connected,
it will return true .
if (client == true)
{
// Client has connected, send data
}
 
Search WWH ::




Custom Search