1 | /*
|
---|
2 | DHCP Chat Server
|
---|
3 |
|
---|
4 | A simple server that distributes any incoming messages to all
|
---|
5 | connected clients. To use telnet to your device's IP address and type.
|
---|
6 | You can see the client's input in the serial monitor as well.
|
---|
7 | Using an Arduino Wiznet Ethernet shield.
|
---|
8 |
|
---|
9 | THis version attempts to get an IP address using DHCP
|
---|
10 |
|
---|
11 | Circuit:
|
---|
12 | * Ethernet shield attached to pins 10, 11, 12, 13
|
---|
13 |
|
---|
14 | created 21 May 2011
|
---|
15 | modified 9 Apr 2012
|
---|
16 | by Tom Igoe
|
---|
17 | Based on ChatServer example by David A. Mellis
|
---|
18 |
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include <SPI.h>
|
---|
22 | #include <Ethernet2.h>
|
---|
23 |
|
---|
24 | // Enter a MAC address and IP address for your controller below.
|
---|
25 | // The IP address will be dependent on your local network.
|
---|
26 | // gateway and subnet are optional:
|
---|
27 | byte mac[] = {
|
---|
28 | 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02
|
---|
29 | };
|
---|
30 | IPAddress ip(192, 168, 1, 177);
|
---|
31 | IPAddress gateway(192, 168, 1, 1);
|
---|
32 | IPAddress subnet(255, 255, 0, 0);
|
---|
33 |
|
---|
34 | // telnet defaults to port 23
|
---|
35 | EthernetServer server(23);
|
---|
36 | boolean gotAMessage = false; // whether or not you got a message from the client yet
|
---|
37 |
|
---|
38 | void setup() {
|
---|
39 | // Open serial communications and wait for port to open:
|
---|
40 | Serial.begin(9600);
|
---|
41 | // this check is only needed on the Leonardo:
|
---|
42 | while (!Serial) {
|
---|
43 | ; // wait for serial port to connect. Needed for Leonardo only
|
---|
44 | }
|
---|
45 |
|
---|
46 |
|
---|
47 | // start the Ethernet connection:
|
---|
48 | Serial.println("Trying to get an IP address using DHCP");
|
---|
49 | if (Ethernet.begin(mac) == 0) {
|
---|
50 | Serial.println("Failed to configure Ethernet using DHCP");
|
---|
51 | // initialize the ethernet device not using DHCP:
|
---|
52 | Ethernet.begin(mac, ip, gateway, subnet);
|
---|
53 | }
|
---|
54 | // print your local IP address:
|
---|
55 | Serial.print("My IP address: ");
|
---|
56 | ip = Ethernet.localIP();
|
---|
57 | for (byte thisByte = 0; thisByte < 4; thisByte++) {
|
---|
58 | // print the value of each byte of the IP address:
|
---|
59 | Serial.print(ip[thisByte], DEC);
|
---|
60 | Serial.print(".");
|
---|
61 | }
|
---|
62 | Serial.println();
|
---|
63 | // start listening for clients
|
---|
64 | server.begin();
|
---|
65 |
|
---|
66 | }
|
---|
67 |
|
---|
68 | void loop() {
|
---|
69 | // wait for a new client:
|
---|
70 | EthernetClient client = server.available();
|
---|
71 |
|
---|
72 | // when the client sends the first byte, say hello:
|
---|
73 | if (client) {
|
---|
74 | if (!gotAMessage) {
|
---|
75 | Serial.println("We have a new client");
|
---|
76 | client.println("Hello, client!");
|
---|
77 | gotAMessage = true;
|
---|
78 | }
|
---|
79 |
|
---|
80 | // read the bytes incoming from the client:
|
---|
81 | char thisChar = client.read();
|
---|
82 | // echo the bytes back to the client:
|
---|
83 | server.write(thisChar);
|
---|
84 | // echo the bytes to the server as well:
|
---|
85 | Serial.print(thisChar);
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|