|
| 1 | +/* |
| 2 | + Advanced WiFi Chat Server |
| 3 | +
|
| 4 | + A more advanced server that distributes any incoming messages |
| 5 | + to all connected clients but the client the message comes from. |
| 6 | + To use, telnet to your device's IP address and type. |
| 7 | + You can see the client's input in the serial monitor as well. |
| 8 | +
|
| 9 | + */ |
| 10 | + |
| 11 | +#include <WiFiS3.h> |
| 12 | + |
| 13 | +#include "arduino_secrets.h" |
| 14 | +///////please enter your sensitive data in the Secret tab/arduino_secrets.h |
| 15 | +char ssid[] = SECRET_SSID; // your network SSID (name) |
| 16 | +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) |
| 17 | + |
| 18 | +int status = WL_IDLE_STATUS; |
| 19 | + |
| 20 | +// telnet defaults to port 23 |
| 21 | +WiFiServer server(23); |
| 22 | + |
| 23 | +WiFiClient clients[8]; |
| 24 | + |
| 25 | +void setup() { |
| 26 | + //Initialize serial and wait for port to open: |
| 27 | + Serial.begin(9600); |
| 28 | + while (!Serial) { |
| 29 | + ; // wait for serial port to connect. Needed for native USB port only |
| 30 | + } |
| 31 | + |
| 32 | + // check for the WiFi module: |
| 33 | + if (WiFi.status() == WL_NO_MODULE) { |
| 34 | + Serial.println("Communication with WiFi module failed!"); |
| 35 | + // don't continue |
| 36 | + while (true); |
| 37 | + } |
| 38 | + |
| 39 | + String fv = WiFi.firmwareVersion(); |
| 40 | + if (fv < WIFI_FIRMWARE_LATEST_VERSION) { |
| 41 | + Serial.println("Please upgrade the firmware"); |
| 42 | + } |
| 43 | + |
| 44 | + // attempt to connect to WiFi network: |
| 45 | + while (status != WL_CONNECTED) { |
| 46 | + Serial.print("Attempting to connect to SSID: "); |
| 47 | + Serial.println(ssid); |
| 48 | + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: |
| 49 | + status = WiFi.begin(ssid, pass); |
| 50 | + |
| 51 | + // wait 10 seconds for connection: |
| 52 | + delay(10000); |
| 53 | + } |
| 54 | + |
| 55 | + // start the server: |
| 56 | + server.begin(); |
| 57 | + |
| 58 | + Serial.print("Chat server address:"); |
| 59 | + Serial.println(WiFi.localIP()); |
| 60 | +} |
| 61 | + |
| 62 | +void loop() { |
| 63 | + // check for any new client connecting, and say hello (before any incoming data) |
| 64 | + WiFiClient newClient = server.accept(); |
| 65 | + if (newClient) { |
| 66 | + for (byte i=0; i < 8; i++) { |
| 67 | + if (!clients[i]) { |
| 68 | + Serial.print("We have a new client #"); |
| 69 | + Serial.println(i); |
| 70 | + newClient.print("Hello, client number: "); |
| 71 | + newClient.println(i); |
| 72 | + // Once we "accept", the client is no longer tracked by WiFiServer |
| 73 | + // so we must store it into our list of clients |
| 74 | + clients[i] = newClient; |
| 75 | + break; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // check for incoming data from all clients |
| 81 | + for (byte i=0; i < 8; i++) { |
| 82 | + if (clients[i] && clients[i].available() > 0) { |
| 83 | + // read bytes from a client |
| 84 | + byte buffer[80]; |
| 85 | + int count = clients[i].read(buffer, 80); |
| 86 | + // write the bytes to all other connected clients |
| 87 | + for (byte j=0; j < 8; j++) { |
| 88 | + if (j != i && clients[j].connected()) { |
| 89 | + clients[j].write(buffer, count); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // stop any clients which disconnect |
| 96 | + for (byte i=0; i < 8; i++) { |
| 97 | + if (clients[i] && !clients[i].connected()) { |
| 98 | + Serial.print("disconnect client #"); |
| 99 | + Serial.println(i); |
| 100 | + clients[i].stop(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments