1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
| #include <SPI.h>
#include <Ethernet.h>
#define ETH_CS 10
#define SD_CS 4
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {0xB0,0xCD,0xAE,0x0F,0xDE,0x10};
IPAddress ip(192,168,1,17);
IPAddress server(192,168,1,10);
EthernetClient client;
void setup()
{
Serial.begin(9600);
Serial.println("Starting Ethernet Connection");
pinMode(ETH_CS,OUTPUT);
pinMode(SD_CS,OUTPUT);
digitalWrite(ETH_CS,LOW); // Select the Ethernet Module.
digitalWrite(SD_CS,HIGH); // De-Select the internal SD Card
if (Ethernet.begin(mac) == 0) // Start in DHCP Mode
{
Serial.println("Failed to configure Ethernet using DHCP, using Static Mode");
// If DHCP Mode failed, start in Static Mode
Ethernet.begin(mac, ip);
}
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++)
{
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, 5000))
{
Serial.println("connected");
}
else
{
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available())
{
char c = client.read();
client.write(c);
Serial.print(c);
}
// as long as there are bytes in the serial queue,
// read them and send them out the socket if it's open:
while (Serial.available() > 0)
{
char inChar = Serial.read();
if (client.connected())
{
client.print(inChar);
}
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing:
while(true);
}
} |