Ethernet Shield DHCP Chat Server
Connects to a Telnet server using an Ethernet shield. Messages from the server are printed out via the serial port.
This example connects to a Telnet server using an Ethernet shield. Messages from the server are printed out via the serial port. Messages can be sent to the remote server serially as well. The Serial Monitor works well for this purpose.
This version attempts to get an IP address using DHCP. An IP address can be assigned via DHCP when Ethernet.begin(mac) is called. Be careful, when using the DHCP extensions, sketch size increases significantly.
Hardware Required
Arduino Board
Software Required
A telnet server
Alternatively, Processing has a ChatServer example that works well for this purpose
Circuit
The Ethernet shield allows you to connect a WIZNet Ethernet controller to the Arduino boards via the SPI bus. It uses the ICSP header pins and pin 10 as chip select for the SPI connection to the Ethernet controller chip. Later models of the Ethernet shield also have an SD Card on board. Digital pin 4 is used to control the chip select pin on the SD card.
The shield should be connected to a network with an Ethernet cable. You will need to change the network settings in the program to correspond to your network.
Image developed using Fritzing. For more circuit examples, see the Fritzing project page
In the above image, the Arduino board would be stacked below the Ethernet shield.
Code
1/*2
3 DHCP Chat Server4
5 A simple server that distributes any incoming messages to all6
7 connected clients. To use, telnet to your device's IP address and type.8
9 You can see the client's input in the serial monitor as well.10
11 Using an Arduino Wiznet Ethernet shield.12
13 THis version attempts to get an IP address using DHCP14
15 Circuit:16
17 * Ethernet shield attached to pins 10, 11, 12, 1318
19 created 21 May 201120
21 modified 9 Apr 201222
23 by Tom Igoe24
25 modified 02 Sept 201526
27 by Arturo Guadalupi28
29 Based on ChatServer example by David A. Mellis30
31 */32
33#include <SPI.h>34#include <Ethernet.h>35
36// Enter a MAC address and IP address for your controller below.37// The IP address will be dependent on your local network.38// gateway and subnet are optional:39byte mac[] = {40
41 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x0242};43
44IPAddress ip(192, 168, 1, 177);45
46IPAddress myDns(192, 168, 1, 1);47
48IPAddress gateway(192, 168, 1, 1);49
50IPAddress subnet(255, 255, 0, 0);51
52// telnet defaults to port 2353
54EthernetServer server(23);55
56bool gotAMessage = false; // whether or not you got a message from the client yet57
58void setup() {59
60 // You can use Ethernet.init(pin) to configure the CS pin61
62 //Ethernet.init(10); // Most Arduino shields63
64 //Ethernet.init(5); // MKR ETH shield65
66 //Ethernet.init(0); // Teensy 2.067
68 //Ethernet.init(20); // Teensy++ 2.069
70 //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet71
72 //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet73
74 // Open serial communications and wait for port to open:75
76 Serial.begin(9600);77
78 while (!Serial) {79
80 ; // wait for serial port to connect. Needed for native USB port only81
82 }83
84 // start the Ethernet connection:85
86 Serial.println("Trying to get an IP address using DHCP");87
88 if (Ethernet.begin(mac) == 0) {89
90 Serial.println("Failed to configure Ethernet using DHCP");91
92 // Check for Ethernet hardware present93
94 if (Ethernet.hardwareStatus() == EthernetNoHardware) {95
96 Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");97
98 while (true) {99
100 delay(1); // do nothing, no point running without Ethernet hardware101
102 }103
104 }105
106 if (Ethernet.linkStatus() == LinkOFF) {107
108 Serial.println("Ethernet cable is not connected.");109
110 }111
112 // initialize the Ethernet device not using DHCP:113
114 Ethernet.begin(mac, ip, myDns, gateway, subnet);115
116 }117
118 // print your local IP address:119
120 Serial.print("My IP address: ");121
122 Serial.println(Ethernet.localIP());123
124 // start listening for clients125
126 server.begin();127}128
129void loop() {130
131 // wait for a new client:132
133 EthernetClient client = server.available();134
135 // when the client sends the first byte, say hello:136
137 if (client) {138
139 if (!gotAMessage) {140
141 Serial.println("We have a new client");142
143 client.println("Hello, client!");144
145 gotAMessage = true;146
147 }148
149 // read the bytes incoming from the client:150
151 char thisChar = client.read();152
153 // echo the bytes back to the client:154
155 server.write(thisChar);156
157 // echo the bytes to the server as well:158
159 Serial.print(thisChar);160
161 Ethernet.maintain();162
163 }164}
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.