Hardware Reference
In-Depth Information
Write a Test Client Program
It's easiest to work through the steps of the program if you
can step through the sequence of events. More complex
development environments allow you to step through a
program one line at a time. Arduino doesn't give you that
ability, but it can link to other environments. The following
program simply passes anything that comes in the serial
port to the Ethernet port, and vice versa. It turns your
Arduino into a serial-to-Ethernet gateway. Using this code,
you can connect to a serial terminal or to Processing—or
to any other development environment that can communi-
cate serially—to test the Ethernet connection.
Test It
The handy thing about
this program is that you
can test the exchange of messages
manually, or by writing a program in a
desktop environment. Once you know
you have the sequence right, you can
translate it into code for the Arduino
module.
/*
Serial To Ethernet
Context: Arduino
*/
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
IPAddress ip( 192,168,1,20 );
IPAddress server( 208,201,239,101 );
8 Change these to
match your own device
and server.
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
Client client;
void setup() {
// start the serial library:
Serial.begin(9600);
// start the Ethernet connection:
if (!Ethernet.begin(mac)) {
Serial.println("DHCP failed, configuring manually.");
// configure manually with your own IP address:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("Ready to go.");
}
void loop() {
if (client.connected()) {
//if you're connected, pass bytes from client to serial:
if (client.available()) {
char netChar = client.read();
Serial.write(netChar);
}
//pass bytes from serial to client:
if (Serial.available()) {
char serialChar = Serial.read();
»
 
Search WWH ::




Custom Search