Hardware Reference
In-Depth Information
Write a Test Server Program
The previous program allowed you to connect to a remote
server and test the exchange of messages. The remote
server was beyond your control, however, so you can't say
for sure that the server ever received your messages. If
you never made a connection, you have no way of knowing
whether the module can connect to any server. To test this,
write your own server program to which it can connect.
Here is a short Processing program
that you can run on your PC. It listens
for incoming connections, and prints
out any messages sent over those con-
nections. It sends an HTTP response
and a simple web page.
8
/*
Test Server Program
Context: Processing
Creates a server that listens for clients and prints
what they say. It also sends the last client anything that's
typed on the keyboard.
*/
To use this, first make sure your
Ethernet module and your PC are
on the same network. Then run this
program, and connect to it from
a browser, using the URL http://
localhost:80 .
// include the net library:
import processing.net.*;
int port = 80; // the port the server listens on
Server myServer; // the server object
int counter = 0;
void setup()
{
myServer = new Server(this, port); // Start the server
}
When you know that works, write a
client program for the Arduino that
connects to your PC's IP address, port
80. You'll be able to see both sides of
the communication, and determine
where the problem lies. Once you've
seen messages coming through to
this program in the right sequence,
just change the connect string in your
microcontroller code to the address
of the web server you want to connect
to, and everything should work fine. If
it doesn't, the problem is most likely
with your web server. Contact your
service provider for details on how to
access any of their server diagnostic
tools, especially any error logs for your
server.
X
void draw()
{
// get the next client that sends a message:
Client thisClient = myServer.available();
// if the message is not null, display what it sent:
if (thisClient != null) {
// read bytes incoming from the client:
while(thisClient.available() > 0) {
print(char(thisClient.read()));
}
// send an HTTP response:
thisClient.write("HTTP/1.1 200 OK\r\n");
thisClient.write("Content-Type: text/html\r\n\r\n");
thisClient.write("<html><head><title>Hello</title></head>");
thisClient.write("<body>Hello, Client! " + counter);
thisClient.write("</body></html>\r\n\r\n");
// disconnect:
thisClient.stop();
counter++;
}
}
!
If this doesn't work for you,
change to port 8080, which is a
common alternative port for many web
servers. If you're running a web server
on your PC, you might have to change
the port number inthis program.
 
Search WWH ::




Custom Search