WiFi Chat Server

Set up a simple chat server with WiFi Shield. LAST REVISION: 09/14/2022, 08:09 AM

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

Hardware Required

Circuit

The WiFi shield uses pins 10, 11, 12, and 13 for SPI connection to HDG104 module. Digital pin 4 is used to control chip select pin on SD card.

You should have access to a 802.11b/g wireless network that connects to internet for this example. You will need to change network settings in sketch to correspond to your particular networks SSID.

For networks using WPA/WPA2 Personal encryption, you need SSID and password. The shield will not connect to networks using WPA2 Enterprise encryption.

WEP network passwords are hexadecimal strings known as keys. A WEP network can have 4 different keys; each key is assigned a "Key Index" value. For WEP encrypted networks, you need SSID, key, and key number.

image developed using Fritzing . For more circuit examples, see Fritzing project page

In above image, Arduino board would be stacked below WiFi shield.

Code

/*H*******************************************************
 Chat  Server
 A 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 client's input in serial monitor as well.
 This example is written for a network using WPA encryption. For
 WEP or WPA, change Wifi.begin( ) call accordingly.
 Circuit:
 * WiFi shield attached
 created 18 Dec 2009
 by David A. Mellis
 modified 31 May 2012
 by Tom Igoe
 ********************************************************/
#include 
#include 

//************************* DEFINES ************************************

//************************* PROTOTYPES ************************************
void printWifiStatus( ); 

//************************* VARIABLES ************************************
char ssid[] = "yourNetwork";                    //  YOUR NETWORK SSID (NAME )
char pass[] = "secretPassword";                     // YOUR NETWORK PASSWORD
int keyIndex = 0;     // YOUR NETWORK KEY iNDEX NUMBER (NEEDED ONLY FOR WEP )
int status = WL_IDLE_STATUS;
WiFiServer server(23 );
boolean alreadyConnected = false;         // CLIENT WAS CONNECTED PREVIOUSLY

/*F********************************************************************
*
**********************************************************************/
void 
setup( ) 
{
	Serial.begin( BAUD  );     // INITIALIZE SERIAL AND WAIT FOR PORT TO OPEN
	while( !Serial  ) 
	{ ; }// WAIT FOR SERIAL PORT TO CONNECT. NEEDED FOR NATIVE USB PORT ONLY
	if( WiFi.status( ) == WL_NO_SHIELD )       // CHECK FOR PRESENCE OF SHIELD
	{
		Serial.println( "WiFi shield not present" );
		while( true  );                                     // DON'T CONTINUE
	}
	String fv = WiFi.firmwareVersion( );
	if( fv != "1.1.0" ) 
		Serial.println( "Please upgrade firmware" );
	while( status != WL_CONNECTED )     // ATTEMPT TO CONNECT TO Wifi NETWORK
	{
		Serial.print("Attempting to connect to SSID: " );
		Serial.println(ssid );
	  	 // CONNECT TO WPA/WPA2 NET. CHNG THIS LINE IF USING OPEN OR WEP NET
		status = WiFi.begin( ssid, pass );
		delay( 10000  );                    // WAIT 10 SECONDS FOR CONNECTION
	}
	// start server:
	server.begin( );
	// you're connected now, so print out status:
	printWifiStatus( );
}
/*F********************************************************************
*
**********************************************************************/
void 
loop( ) 
{
	WiFiClient client = server.available( );         // WAIT FOR A NEW CLIENT
	if( client  )                  // WHEN CLIENT SENDS FIRST BYTE, SAY HELLO
	{
		if( !alreadyConnected ) 	
		{
			client.flush( );                        // CLEAN OUT INPUT BUFFER
			Serial.println( "have a new client"  );
			client.println( "Hello, client!"  );
			alreadyConnected = true;
		}
		if( client.available( ) > 0 ) 
		{
			char thisChar = client.read( );// READ BYTES INCOMING FROM CLIENT
			server.write( thisChar  );           // ECHO BYTES BACK TO CLIENT
			Serial.write( thisChar  );        // ECHO BYTES TO SERVER AS WELL
		}
	}
}
/*F********************************************************************
*
**********************************************************************/
void 
printWifiStatus( ) 
{
	Serial.print( "SSID: " );    // PRINT SSID OF NETWORK YOU'RE ATTACHED TO
	Serial.println( WiFi.SSID() );
	IPAddress ip = WiFi.localIP();
	Serial.print( "IP Address: " );
	Serial.println( ip );             // PRINT YOUR WiFi SHIELD'S IP ADDRESS
	long rssi = WiFi.RSSI();
	Serial.print( "signal strength ( RSSI ):" );
	Serial.print( rssi );                  // PRINT RECEIVED SIGNAL STRENGTH
	Serial.println( " dBm" );
}

Last revision 2018/08/23 by SM