UIPEthernet library for Arduino IDE, Eclipse with arduino plugin and MBED/SMeshStudio (AVR,STM32F,ESP8266,Intel ARC32,Nordic nRF51,Teensy boards,Realtek Ameba(RTL8195A,RTL8710)), ENC28j60 network chip. Compatible with Wiznet W5100 Ethernet library API. Compiled and tested on Nucleo-F302R8. Master repository is: https://github.com/UIPEthernet/UIPEthernet/

Committer:
cassyarduino
Date:
Wed Dec 21 16:58:10 2016 +0100
Revision:
0:e3fb1267e3c3
Child:
11:3fb19220d9ec
initial release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cassyarduino 0:e3fb1267e3c3 1 /*
cassyarduino 0:e3fb1267e3c3 2 Advanced Chat Server
cassyarduino 0:e3fb1267e3c3 3
cassyarduino 0:e3fb1267e3c3 4 A simple server that distributes any incoming messages to all
cassyarduino 0:e3fb1267e3c3 5 connected clients but the client the message comes from.
cassyarduino 0:e3fb1267e3c3 6 To use telnet to your device's IP address and type.
cassyarduino 0:e3fb1267e3c3 7 You can see the client's input in the serial monitor as well.
cassyarduino 0:e3fb1267e3c3 8 Using an Arduino Wiznet Ethernet shield.
cassyarduino 0:e3fb1267e3c3 9
cassyarduino 0:e3fb1267e3c3 10 Circuit:
cassyarduino 0:e3fb1267e3c3 11 * Ethernet shield attached to pins 10, 11, 12, 13
cassyarduino 0:e3fb1267e3c3 12 * Analog inputs attached to pins A0 through A5 (optional)
cassyarduino 0:e3fb1267e3c3 13
cassyarduino 0:e3fb1267e3c3 14 created 18 Dec 2009
cassyarduino 0:e3fb1267e3c3 15 by David A. Mellis
cassyarduino 0:e3fb1267e3c3 16 modified 9 Apr 2012
cassyarduino 0:e3fb1267e3c3 17 by Tom Igoe
cassyarduino 0:e3fb1267e3c3 18 redesigned to make use of operator== 25 Nov 2013
cassyarduino 0:e3fb1267e3c3 19 by Norbert Truchsess
cassyarduino 0:e3fb1267e3c3 20
cassyarduino 0:e3fb1267e3c3 21 */
cassyarduino 0:e3fb1267e3c3 22
cassyarduino 0:e3fb1267e3c3 23 #include <UIPEthernet.h>
cassyarduino 0:e3fb1267e3c3 24 #include <utility/logging.h>
cassyarduino 0:e3fb1267e3c3 25
cassyarduino 0:e3fb1267e3c3 26 // Enter a MAC address and IP address for your controller below.
cassyarduino 0:e3fb1267e3c3 27 // The IP address will be dependent on your local network.
cassyarduino 0:e3fb1267e3c3 28
cassyarduino 0:e3fb1267e3c3 29 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
cassyarduino 0:e3fb1267e3c3 30 IPAddress ip(192,168,0,6);
cassyarduino 0:e3fb1267e3c3 31
cassyarduino 0:e3fb1267e3c3 32 // telnet defaults to port 23
cassyarduino 0:e3fb1267e3c3 33 EthernetServer server(23);
cassyarduino 0:e3fb1267e3c3 34
cassyarduino 0:e3fb1267e3c3 35 EthernetClient clients[4];
cassyarduino 0:e3fb1267e3c3 36
cassyarduino 0:e3fb1267e3c3 37 void setup() {
cassyarduino 0:e3fb1267e3c3 38 // initialize the ethernet device
cassyarduino 0:e3fb1267e3c3 39 Ethernet.begin(mac, ip);
cassyarduino 0:e3fb1267e3c3 40 // start listening for clients
cassyarduino 0:e3fb1267e3c3 41 server.begin();
cassyarduino 0:e3fb1267e3c3 42 // Open serial communications and wait for port to open:
cassyarduino 0:e3fb1267e3c3 43 #if ACTLOGLEVEL>LOG_NONE
cassyarduino 0:e3fb1267e3c3 44 LogObject.begin(9600);
cassyarduino 0:e3fb1267e3c3 45 while (!LogObject)
cassyarduino 0:e3fb1267e3c3 46 {
cassyarduino 0:e3fb1267e3c3 47 ; // wait for serial port to connect. Needed for Leonardo only
cassyarduino 0:e3fb1267e3c3 48 }
cassyarduino 0:e3fb1267e3c3 49 #endif
cassyarduino 0:e3fb1267e3c3 50
cassyarduino 0:e3fb1267e3c3 51 #if ACTLOGLEVEL>=LOG_INFO
cassyarduino 0:e3fb1267e3c3 52 LogObject.uart_send_str(F("Chat server address:"));
cassyarduino 0:e3fb1267e3c3 53 LogObject.println(Ethernet.localIP());
cassyarduino 0:e3fb1267e3c3 54 #endif
cassyarduino 0:e3fb1267e3c3 55 }
cassyarduino 0:e3fb1267e3c3 56
cassyarduino 0:e3fb1267e3c3 57 void loop() {
cassyarduino 0:e3fb1267e3c3 58 // wait for a new client:
cassyarduino 0:e3fb1267e3c3 59 EthernetClient client = server.available();
cassyarduino 0:e3fb1267e3c3 60
cassyarduino 0:e3fb1267e3c3 61 if (client) {
cassyarduino 0:e3fb1267e3c3 62
cassyarduino 0:e3fb1267e3c3 63 bool newClient = true;
cassyarduino 0:e3fb1267e3c3 64 for (byte i=0;i<4;i++) {
cassyarduino 0:e3fb1267e3c3 65 //check whether this client refers to the same socket as one of the existing instances:
cassyarduino 0:e3fb1267e3c3 66 if (clients[i]==client) {
cassyarduino 0:e3fb1267e3c3 67 newClient = false;
cassyarduino 0:e3fb1267e3c3 68 break;
cassyarduino 0:e3fb1267e3c3 69 }
cassyarduino 0:e3fb1267e3c3 70 }
cassyarduino 0:e3fb1267e3c3 71
cassyarduino 0:e3fb1267e3c3 72 if (newClient) {
cassyarduino 0:e3fb1267e3c3 73 //check which of the existing clients can be overridden:
cassyarduino 0:e3fb1267e3c3 74 for (byte i=0;i<4;i++) {
cassyarduino 0:e3fb1267e3c3 75 if (!clients[i] && clients[i]!=client) {
cassyarduino 0:e3fb1267e3c3 76 clients[i] = client;
cassyarduino 0:e3fb1267e3c3 77 // clead out the input buffer:
cassyarduino 0:e3fb1267e3c3 78 client.flush();
cassyarduino 0:e3fb1267e3c3 79 // clead out the input buffer:
cassyarduino 0:e3fb1267e3c3 80 client.flush();
cassyarduino 0:e3fb1267e3c3 81 #if ACTLOGLEVEL>=LOG_INFO
cassyarduino 0:e3fb1267e3c3 82 LogObject.uart_send_strln(F("We have a new client"));
cassyarduino 0:e3fb1267e3c3 83 #endif
cassyarduino 0:e3fb1267e3c3 84 client.println(F("Hello, client!"));
cassyarduino 0:e3fb1267e3c3 85 client.print(F("my IP: "));
cassyarduino 0:e3fb1267e3c3 86 client.println(Ethernet.localIP());
cassyarduino 0:e3fb1267e3c3 87 break;
cassyarduino 0:e3fb1267e3c3 88 }
cassyarduino 0:e3fb1267e3c3 89 }
cassyarduino 0:e3fb1267e3c3 90 }
cassyarduino 0:e3fb1267e3c3 91
cassyarduino 0:e3fb1267e3c3 92 if (client.available() > 0) {
cassyarduino 0:e3fb1267e3c3 93 // read the bytes incoming from the client:
cassyarduino 0:e3fb1267e3c3 94 char thisChar = client.read();
cassyarduino 0:e3fb1267e3c3 95 // echo the bytes back to all other connected clients:
cassyarduino 0:e3fb1267e3c3 96 for (byte i=0;i<4;i++) {
cassyarduino 0:e3fb1267e3c3 97 if (clients[i] && clients[i]!=client) {
cassyarduino 0:e3fb1267e3c3 98 clients[i].write(thisChar);
cassyarduino 0:e3fb1267e3c3 99 }
cassyarduino 0:e3fb1267e3c3 100 }
cassyarduino 0:e3fb1267e3c3 101 // echo the bytes to the server as well:
cassyarduino 0:e3fb1267e3c3 102 #if ACTLOGLEVEL>=LOG_INFO
cassyarduino 0:e3fb1267e3c3 103 LogObject.write(thisChar);
cassyarduino 0:e3fb1267e3c3 104 #endif
cassyarduino 0:e3fb1267e3c3 105 }
cassyarduino 0:e3fb1267e3c3 106 }
cassyarduino 0:e3fb1267e3c3 107 for (byte i=0;i<4;i++) {
cassyarduino 0:e3fb1267e3c3 108 if (!(clients[i].connected())) {
cassyarduino 0:e3fb1267e3c3 109 // client.stop() invalidates the internal socket-descriptor, so next use of == will allways return false;
cassyarduino 0:e3fb1267e3c3 110 clients[i].stop();
cassyarduino 0:e3fb1267e3c3 111 }
cassyarduino 0:e3fb1267e3c3 112 }
cassyarduino 0:e3fb1267e3c3 113 }