Ethernet Shield Chat Server

Set up a simple chat server htat distributes any incoming messages to all connected clients.

A simple server that distributes any incoming messages to all connected clients. To use, open a terminal window, Telnet to your devices IP address, and type away. Any incoming text will be sent to all connected clients (including the one typing). Additionally, you will be able to see the client's input in your serial monitor as well.

Hardware Required

Circuit

The Ethernet shield allows you to connect a WizNet Ethernet controller to the Arduino boards via the SPI bus. It uses pins 10, 11, 12, and 13 for the SPI connection to the WizNet. 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.

The circuit for this tutorial.
The circuit for this tutorial.

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.

Schematic

The schematic for this tutorial.
The schematic for this tutorial.

Code:

1/*
2
3 Chat Server
4
5 A simple server that distributes any incoming messages to all
6
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 Circuit:
14
15 * Ethernet shield attached to pins 10, 11, 12, 13
16
17 created 18 Dec 2009
18
19 by David A. Mellis
20
21 modified 9 Apr 2012
22
23 by Tom Igoe
24
25 */
26
27#include <SPI.h>
28#include <Ethernet.h>
29
30// Enter a MAC address and IP address for your controller below.
31// The IP address will be dependent on your local network.
32// gateway and subnet are optional:
33byte mac[] = {
34
35 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
36
37IPAddress ip(192, 168, 1, 177);
38
39IPAddress myDns(192, 168, 1, 1);
40
41IPAddress gateway(192, 168, 1, 1);
42
43IPAddress subnet(255, 255, 0, 0);
44
45// telnet defaults to port 23
46
47EthernetServer server(23);
48
49bool alreadyConnected = false; // whether or not the client was connected previously
50
51void setup() {
52
53 // You can use Ethernet.init(pin) to configure the CS pin
54
55 //Ethernet.init(10); // Most Arduino shields
56
57 //Ethernet.init(5); // MKR ETH shield
58
59 //Ethernet.init(0); // Teensy 2.0
60
61 //Ethernet.init(20); // Teensy++ 2.0
62
63 //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
64
65 //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
66
67 // initialize the ethernet device
68
69 Ethernet.begin(mac, ip, myDns, gateway, subnet);
70
71 // Open serial communications and wait for port to open:
72
73 Serial.begin(9600);
74
75 while (!Serial) {
76
77 ; // wait for serial port to connect. Needed for native USB port only
78
79 }
80
81 // Check for Ethernet hardware present
82
83 if (Ethernet.hardwareStatus() == EthernetNoHardware) {
84
85 Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
86
87 while (true) {
88
89 delay(1); // do nothing, no point running without Ethernet hardware
90
91 }
92
93 }
94
95 if (Ethernet.linkStatus() == LinkOFF) {
96
97 Serial.println("Ethernet cable is not connected.");
98
99 }
100
101 // start listening for clients
102
103 server.begin();
104
105 Serial.print("Chat server address:");
106
107 Serial.println(Ethernet.localIP());
108}
109
110void loop() {
111
112 // wait for a new client:
113
114 EthernetClient client = server.available();
115
116 // when the client sends the first byte, say hello:
117
118 if (client) {
119
120 if (!alreadyConnected) {
121
122 // clear out the input buffer:
123
124 client.flush();
125
126 Serial.println("We have a new client");
127
128 client.println("Hello, client!");
129
130 alreadyConnected = true;
131
132 }
133
134 if (client.available() > 0) {
135
136 // read the bytes incoming from the client:
137
138 char thisChar = client.read();
139
140 // echo the bytes back to the client:
141
142 server.write(thisChar);
143
144 // echo the bytes to the server as well:
145
146 Serial.write(thisChar);
147
148 }
149
150 }
151}

Last revision 2018/08/23 by SM

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.