|
| 1 | +/* |
| 2 | + Advanced 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 | + Using an Arduino Wiznet Ethernet shield. |
| 9 | +
|
| 10 | + Circuit: |
| 11 | + * Ethernet Wiznet5500/Wiznet5100/ENC28J60 on esp8266 |
| 12 | +
|
| 13 | + created 18 Dec 2009 |
| 14 | + by David A. Mellis |
| 15 | + modified 9 Apr 2012 |
| 16 | + by Tom Igoe |
| 17 | + redesigned to make use of operator== 25 Nov 2013 |
| 18 | + by Norbert Truchsess |
| 19 | +
|
| 20 | + */ |
| 21 | + |
| 22 | +// specific to esp8266 w/lwIP |
| 23 | +#include <EthernetCompat.h> |
| 24 | +ArduinoWiznet5500lwIP Ethernet(/*SS*/ 16); // <== adapt to your hardware |
| 25 | +// ArduinoWiznet5100lwIP Ethernet(/*SS*/ 16); // <== adapt to your hardware |
| 26 | +// ArduinoENC28J60lwIP Ethernet(/*SS*/ 16); // <== adapt to your hardware |
| 27 | + |
| 28 | + |
| 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: |
| 33 | +byte notNeededButAllowed_mac[] = { |
| 34 | + 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED |
| 35 | +}; |
| 36 | +byte* mac = nullptr; // automatic mac |
| 37 | +IPAddress ip(192, 168, 1, 177); |
| 38 | +IPAddress myDns(192, 168, 1, 1); |
| 39 | +IPAddress gateway(192, 168, 1, 1); |
| 40 | +IPAddress subnet(255, 255, 0, 0); |
| 41 | + |
| 42 | + |
| 43 | +// telnet defaults to port 23 |
| 44 | +EthernetServer server(23); |
| 45 | + |
| 46 | +EthernetClient clients[8]; |
| 47 | + |
| 48 | +void setup() { |
| 49 | + // You can use Ethernet.init(pin) to configure the CS pin |
| 50 | + // Ethernet.init(10); // Most Arduino shields |
| 51 | + // Ethernet.init(5); // MKR ETH shield |
| 52 | + // Ethernet.init(0); // Teensy 2.0 |
| 53 | + // Ethernet.init(20); // Teensy++ 2.0 |
| 54 | + // Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet |
| 55 | + // Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet |
| 56 | + // // esp8266 w/lwIP: SS set in Ethernet constructor |
| 57 | + |
| 58 | + // initialize the Ethernet device |
| 59 | + Ethernet.begin(mac, ip, myDns, gateway, subnet); |
| 60 | + |
| 61 | + // Open serial communications and wait for port to open: |
| 62 | + Serial.begin(9600); |
| 63 | + while (!Serial) { |
| 64 | + ; // wait for serial port to connect. Needed for native USB port only |
| 65 | + } |
| 66 | + |
| 67 | + // Check for Ethernet hardware present |
| 68 | + if (Ethernet.hardwareStatus() == EthernetNoHardware) { |
| 69 | + Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); |
| 70 | + while (true) { |
| 71 | + delay(1); // do nothing, no point running without Ethernet hardware |
| 72 | + } |
| 73 | + } |
| 74 | + if (Ethernet.linkStatus() == LinkOFF) { |
| 75 | + Serial.println("Ethernet cable is not connected."); |
| 76 | + } |
| 77 | + |
| 78 | + // start listening for clients |
| 79 | + server.begin(); |
| 80 | + |
| 81 | + Serial.print("Chat server address:"); |
| 82 | + Serial.println(Ethernet.localIP()); |
| 83 | +} |
| 84 | + |
| 85 | +void loop() { |
| 86 | + // check for any new client connecting, and say hello (before any incoming data) |
| 87 | + EthernetClient newClient = server.accept(); |
| 88 | + if (newClient) { |
| 89 | + for (byte i = 0; i < 8; i++) { |
| 90 | + if (!clients[i]) { |
| 91 | + Serial.print("We have a new client #"); |
| 92 | + Serial.println(i); |
| 93 | + newClient.print("Hello, client number: "); |
| 94 | + newClient.println(i); |
| 95 | + // Once we "accept", the client is no longer tracked by EthernetServer |
| 96 | + // so we must store it into our list of clients |
| 97 | + clients[i] = newClient; |
| 98 | + break; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // check for incoming data from all clients |
| 104 | + for (byte i = 0; i < 8; i++) { |
| 105 | + if (clients[i] && clients[i].available() > 0) { |
| 106 | + // read bytes from a client |
| 107 | + byte buffer[80]; |
| 108 | + int count = clients[i].read(buffer, 80); |
| 109 | + // write the bytes to all other connected clients |
| 110 | + for (byte j = 0; j < 8; j++) { |
| 111 | + if (j != i && clients[j].connected()) { |
| 112 | + clients[j].write(buffer, count); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + // stop any clients which disconnect |
| 119 | + for (byte i = 0; i < 8; i++) { |
| 120 | + if (clients[i] && !clients[i].connected()) { |
| 121 | + Serial.print("disconnect client #"); |
| 122 | + Serial.println(i); |
| 123 | + clients[i].stop(); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments