Chat Server Using Arduino

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.

Chat Server using Arduino

Circuit

The Ethernet shield allows you to connect a WizNet Ethernet controller to the Arduino 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 slave 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 would be stacked below the Ethernet shield.

Schematic

Chat Server using Arduino schematic

Code:

Please note: according to your hardware setup, you need to comment / uncomment the libraries at the beginning of the sketch. UseΒ Ethernet.hΒ with the Arduino Ethernet Shield orΒ Ethernet2.hΒ with the Arduino Ethernet Shield 2 and Leonardo Ethernet.

/*
Chat ServerA simple server that distributes any incoming messages to all
connected clients. Β To use, telnet to your device's IP address and type.
You can see the client's input in the serial monitor as well.
Using an Arduino Wiznet Ethernet shield.

Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13

created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe

*/

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byteΒ mac[]Β =Β {
0xDE,Β 0xAD,Β 0xBE,Β 0xEF,Β 0xFE,Β 0xEDΒ };
IPAddress ip(192,Β 168,Β 1,Β 177);
IPAddress myDns(192,Β 168,Β 1,Β 1);
IPAddress gateway(192,Β 168,Β 1,Β 1);
IPAddress subnet(255,Β 255,Β 0,Β 0);

// telnet defaults to port 23
EthernetServer server(23);
bool alreadyConnectedΒ =Β false;Β // whether or not the client was connected previously

voidΒ setup()Β {
// You can use Ethernet.init(pin) to configure the CS pin
//Ethernet.init(10); Β // Most Arduino shields
//Ethernet.init(5); Β  // MKR ETH shield
//Ethernet.init(0); Β  // Teensy 2.0
//Ethernet.init(20); Β // Teensy++ 2.0
//Ethernet.init(15); Β // ESP8266 with Adafruit Featherwing Ethernet
//Ethernet.init(33); Β // ESP32 with Adafruit Featherwing Ethernet

// initialize the ethernet device
Ethernet.begin(mac,Β ip,Β myDns,Β gateway,Β subnet);

// Open serial communications and wait for port to open:
Serial.begin(9600);
whileΒ (!Serial)Β {
;Β // wait for serial port to connect. Needed for native USB port only
}

// Check for Ethernet hardware present
ifΒ (Ethernet.hardwareStatus()Β ==Β EthernetNoHardware)Β {
Serial.println("Ethernet shield was not found. Β Sorry, can't run without hardware. :(");
whileΒ (true)Β {
delay(1);Β // do nothing, no point running without Ethernet hardware
}
}
ifΒ (Ethernet.linkStatus()Β ==Β LinkOFF)Β {
Serial.println("Ethernet cable is not connected.");
}

// start listening for clients
server.begin();

Serial.print("Chat server address:");
Serial.println(Ethernet.localIP());
}

voidΒ loop()Β {
// wait for a new client:
EthernetClient clientΒ =Β server.available();

// when the client sends the first byte, say hello:
ifΒ (client)Β {
ifΒ (!alreadyConnected)Β {
// clear out the input buffer:
client.flush();
Serial.println("We have a new client");
client.println("Hello, client!");
alreadyConnectedΒ =Β true;
}

ifΒ (client.available()Β >Β 0)Β {
// read the bytes incoming from the client:
charΒ thisCharΒ =Β client.read();
// echo the bytes back to the client:
server.write(thisChar);
// echo the bytes to the server as well:
Serial.write(thisChar);
}
}
}

Major Components in Project

Hardware Required

Source : Chat Server using Arduino

 

 

About The Author

Scroll to Top