2012-03-04 23:22:24 +01:00
|
|
|
/*
|
|
|
|
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 the client's input in the serial monitor as well.
|
|
|
|
|
|
|
|
This example is written for a network using WPA encryption. For
|
|
|
|
WEP or WPA, change the Wifi.begin() call accordingly.
|
|
|
|
|
|
|
|
|
|
|
|
Circuit:
|
|
|
|
* WiFi shield attached
|
|
|
|
|
|
|
|
created 18 Dec 2009
|
|
|
|
by David A. Mellis
|
2012-03-12 18:57:42 +01:00
|
|
|
modified 12 Mar 2012
|
2012-03-04 23:22:24 +01:00
|
|
|
by Tom Igoe
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <SPI.h>
|
|
|
|
#include <WiFi.h>
|
|
|
|
|
2012-03-04 23:23:01 +01:00
|
|
|
char ssid[] = "YourNetwork"; // your network SSID (name)
|
|
|
|
char pass[] = "password"; // your network password (use for WPA, or use as key for WEP)
|
2012-03-12 18:57:42 +01:00
|
|
|
|
2012-03-04 23:22:24 +01:00
|
|
|
int keyIndex = 0; // your network key Index number (needed only for WEP)
|
|
|
|
|
|
|
|
int status = WL_IDLE_STATUS;
|
|
|
|
|
|
|
|
WiFiServer server(23);
|
|
|
|
|
2012-03-12 18:57:42 +01:00
|
|
|
boolean alreadyConnected = false; // whether or not the client was connected previously
|
2012-03-04 23:22:24 +01:00
|
|
|
|
|
|
|
void setup() {
|
|
|
|
// initialize serial:
|
|
|
|
Serial.begin(9600);
|
|
|
|
Serial.println("Attempting to connect to Wifi network...");
|
|
|
|
Serial.print("SSID: ");
|
|
|
|
Serial.println(ssid);
|
|
|
|
|
|
|
|
status = WiFi.begin(ssid, pass);
|
|
|
|
if ( status != WL_CONNECTED) {
|
|
|
|
Serial.println("Couldn't get a wifi connection");
|
|
|
|
while(true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
server.begin();
|
|
|
|
Serial.print("Connected to wifi.");
|
|
|
|
printWifiStatus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void loop() {
|
|
|
|
// wait for a new client:
|
|
|
|
WiFiClient client = server.available();
|
|
|
|
|
2012-03-12 18:57:42 +01:00
|
|
|
|
2012-03-04 23:22:24 +01:00
|
|
|
// when the client sends the first byte, say hello:
|
|
|
|
if (client) {
|
2012-03-12 18:57:42 +01:00
|
|
|
if (!alreadyConnected) {
|
|
|
|
// clead out the input buffer:
|
|
|
|
client.flush();
|
2012-03-04 23:22:24 +01:00
|
|
|
Serial.println("We have a new client");
|
|
|
|
client.println("Hello, client!");
|
2012-03-12 18:57:42 +01:00
|
|
|
alreadyConnected = true;
|
|
|
|
}
|
2012-03-04 23:22:24 +01:00
|
|
|
|
2012-03-12 18:57:42 +01:00
|
|
|
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);
|
|
|
|
}
|
2012-03-04 23:22:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void printWifiStatus() {
|
|
|
|
// print the SSID of the network you're attached to:
|
|
|
|
Serial.print("SSID: ");
|
|
|
|
Serial.println(WiFi.SSID());
|
|
|
|
|
|
|
|
// print your WiFi shield's IP address:
|
|
|
|
IPAddress ip = WiFi.localIP();
|
|
|
|
Serial.print("IP Address: ");
|
|
|
|
Serial.println(ip);
|
|
|
|
|
|
|
|
// print the received signal strength:
|
|
|
|
long rssi = WiFi.RSSI();
|
|
|
|
Serial.print("signal strength (RSSI):");
|
|
|
|
Serial.print(rssi);
|
|
|
|
Serial.println(" dBm");
|
|
|
|
}
|
|
|
|
|