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/
examples/AdvancedChatServer/AdvancedChatServer.ino@32:e77cbe3783e5, 2017-02-10 (annotated)
- Committer:
- cassyarduino
- Date:
- Fri Feb 10 09:10:47 2017 +0100
- Revision:
- 32:e77cbe3783e5
- Parent:
- 11:3fb19220d9ec
Changes
Who changed what in which revision?
User | Revision | Line number | New 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 | 32:e77cbe3783e5 | 23 | // Enter a MAC address and IP address for your controller below. |
cassyarduino | 32:e77cbe3783e5 | 24 | // The IP address will be dependent on your local network. |
cassyarduino | 32:e77cbe3783e5 | 25 | |
cassyarduino | 32:e77cbe3783e5 | 26 | #define MACADDRESS 0x00,0x01,0x02,0x03,0x04,0x05 |
cassyarduino | 32:e77cbe3783e5 | 27 | #define MYIPADDR 192,168,1,6 |
cassyarduino | 32:e77cbe3783e5 | 28 | #define MYIPMASK 255,255,255,0 |
cassyarduino | 32:e77cbe3783e5 | 29 | #define MYDNS 192,168,1,1 |
cassyarduino | 32:e77cbe3783e5 | 30 | #define MYGW 192,168,1,1 |
cassyarduino | 32:e77cbe3783e5 | 31 | // telnet defaults to port 23 |
cassyarduino | 32:e77cbe3783e5 | 32 | #define LISTENPORT 23 |
cassyarduino | 32:e77cbe3783e5 | 33 | #define UARTBAUD 115200 |
cassyarduino | 32:e77cbe3783e5 | 34 | |
cassyarduino | 11:3fb19220d9ec | 35 | #if defined(__MBED__) |
cassyarduino | 11:3fb19220d9ec | 36 | #include <mbed.h> |
cassyarduino | 11:3fb19220d9ec | 37 | #include "mbed/millis.h" |
cassyarduino | 11:3fb19220d9ec | 38 | #define delay(x) wait_ms(x) |
cassyarduino | 11:3fb19220d9ec | 39 | #define PROGMEM |
cassyarduino | 11:3fb19220d9ec | 40 | #include "mbed/Print.h" |
cassyarduino | 11:3fb19220d9ec | 41 | #endif |
cassyarduino | 11:3fb19220d9ec | 42 | |
cassyarduino | 0:e3fb1267e3c3 | 43 | #include <UIPEthernet.h> |
cassyarduino | 0:e3fb1267e3c3 | 44 | #include <utility/logging.h> |
cassyarduino | 0:e3fb1267e3c3 | 45 | |
cassyarduino | 32:e77cbe3783e5 | 46 | uint8_t mac[6] = {MACADDRESS}; |
cassyarduino | 32:e77cbe3783e5 | 47 | uint8_t myIP[4] = {MYIPADDR}; |
cassyarduino | 32:e77cbe3783e5 | 48 | uint8_t myMASK[4] = {MYIPMASK}; |
cassyarduino | 32:e77cbe3783e5 | 49 | uint8_t myDNS[4] = {MYDNS}; |
cassyarduino | 32:e77cbe3783e5 | 50 | uint8_t myGW[4] = {MYGW}; |
cassyarduino | 0:e3fb1267e3c3 | 51 | |
cassyarduino | 32:e77cbe3783e5 | 52 | EthernetServer server(LISTENPORT); |
cassyarduino | 0:e3fb1267e3c3 | 53 | |
cassyarduino | 0:e3fb1267e3c3 | 54 | EthernetClient clients[4]; |
cassyarduino | 0:e3fb1267e3c3 | 55 | |
cassyarduino | 11:3fb19220d9ec | 56 | #if defined(ARDUINO) |
cassyarduino | 0:e3fb1267e3c3 | 57 | void setup() { |
cassyarduino | 11:3fb19220d9ec | 58 | #endif |
cassyarduino | 11:3fb19220d9ec | 59 | #if defined(__MBED__) |
cassyarduino | 11:3fb19220d9ec | 60 | int main() { |
cassyarduino | 11:3fb19220d9ec | 61 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 62 | // initialize the ethernet device |
cassyarduino | 32:e77cbe3783e5 | 63 | //Ethernet.begin(mac,myIP); |
cassyarduino | 32:e77cbe3783e5 | 64 | Ethernet.begin(mac,myIP,myDNS,myGW,myMASK); |
cassyarduino | 0:e3fb1267e3c3 | 65 | // start listening for clients |
cassyarduino | 0:e3fb1267e3c3 | 66 | server.begin(); |
cassyarduino | 0:e3fb1267e3c3 | 67 | // Open serial communications and wait for port to open: |
cassyarduino | 0:e3fb1267e3c3 | 68 | #if ACTLOGLEVEL>LOG_NONE |
cassyarduino | 11:3fb19220d9ec | 69 | |
cassyarduino | 11:3fb19220d9ec | 70 | #if defined(ARDUINO) |
cassyarduino | 32:e77cbe3783e5 | 71 | LogObject.begin(UARTBAUD); |
cassyarduino | 11:3fb19220d9ec | 72 | #endif |
cassyarduino | 11:3fb19220d9ec | 73 | #if defined(__MBED__) |
cassyarduino | 11:3fb19220d9ec | 74 | Serial LogObject(SERIAL_TX,SERIAL_RX); |
cassyarduino | 32:e77cbe3783e5 | 75 | LogObject.baud(UARTBAUD); |
cassyarduino | 11:3fb19220d9ec | 76 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 77 | while (!LogObject) |
cassyarduino | 0:e3fb1267e3c3 | 78 | { |
cassyarduino | 0:e3fb1267e3c3 | 79 | ; // wait for serial port to connect. Needed for Leonardo only |
cassyarduino | 0:e3fb1267e3c3 | 80 | } |
cassyarduino | 0:e3fb1267e3c3 | 81 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 82 | |
cassyarduino | 0:e3fb1267e3c3 | 83 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 32:e77cbe3783e5 | 84 | LogObject.uart_send_str(F("Chat server listen on:")); |
cassyarduino | 11:3fb19220d9ec | 85 | #if defined(ARDUINO) |
cassyarduino | 32:e77cbe3783e5 | 86 | LogObject.print(Ethernet.localIP()[0]); |
cassyarduino | 32:e77cbe3783e5 | 87 | LogObject.print(F(".")); |
cassyarduino | 32:e77cbe3783e5 | 88 | LogObject.print(Ethernet.localIP()[1]); |
cassyarduino | 32:e77cbe3783e5 | 89 | LogObject.print(F(".")); |
cassyarduino | 32:e77cbe3783e5 | 90 | LogObject.print(Ethernet.localIP()[2]); |
cassyarduino | 32:e77cbe3783e5 | 91 | LogObject.print(F(".")); |
cassyarduino | 32:e77cbe3783e5 | 92 | LogObject.print(Ethernet.localIP()[3]); |
cassyarduino | 32:e77cbe3783e5 | 93 | LogObject.print(F(":")); |
cassyarduino | 32:e77cbe3783e5 | 94 | LogObject.println(LISTENPORT); |
cassyarduino | 11:3fb19220d9ec | 95 | #endif |
cassyarduino | 11:3fb19220d9ec | 96 | #if defined(__MBED__) |
cassyarduino | 32:e77cbe3783e5 | 97 | LogObject.printf("%d.%d.%d.%d:%d\r\n",Ethernet.localIP()[0],Ethernet.localIP()[1],Ethernet.localIP()[2],Ethernet.localIP()[3],LISTENPORT); |
cassyarduino | 11:3fb19220d9ec | 98 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 99 | #endif |
cassyarduino | 11:3fb19220d9ec | 100 | |
cassyarduino | 11:3fb19220d9ec | 101 | #if defined(ARDUINO) |
cassyarduino | 0:e3fb1267e3c3 | 102 | } |
cassyarduino | 0:e3fb1267e3c3 | 103 | |
cassyarduino | 0:e3fb1267e3c3 | 104 | void loop() { |
cassyarduino | 11:3fb19220d9ec | 105 | #endif |
cassyarduino | 11:3fb19220d9ec | 106 | |
cassyarduino | 11:3fb19220d9ec | 107 | #if defined(__MBED__) |
cassyarduino | 11:3fb19220d9ec | 108 | while(true) { |
cassyarduino | 11:3fb19220d9ec | 109 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 110 | // wait for a new client: |
cassyarduino | 0:e3fb1267e3c3 | 111 | EthernetClient client = server.available(); |
cassyarduino | 0:e3fb1267e3c3 | 112 | |
cassyarduino | 32:e77cbe3783e5 | 113 | if (client) |
cassyarduino | 32:e77cbe3783e5 | 114 | { |
cassyarduino | 32:e77cbe3783e5 | 115 | //check whether this client refers to the same socket as one of the existing instances: |
cassyarduino | 0:e3fb1267e3c3 | 116 | bool newClient = true; |
cassyarduino | 32:e77cbe3783e5 | 117 | uint8_t i=0; |
cassyarduino | 32:e77cbe3783e5 | 118 | while ((i<4) && (clients[i]!=client)) |
cassyarduino | 32:e77cbe3783e5 | 119 | {i++;} |
cassyarduino | 32:e77cbe3783e5 | 120 | if (i<4) {newClient = false;} |
cassyarduino | 0:e3fb1267e3c3 | 121 | |
cassyarduino | 32:e77cbe3783e5 | 122 | if (newClient) |
cassyarduino | 32:e77cbe3783e5 | 123 | { |
cassyarduino | 32:e77cbe3783e5 | 124 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 32:e77cbe3783e5 | 125 | LogObject.uart_send_strln(F("New client try connect")); |
cassyarduino | 32:e77cbe3783e5 | 126 | #endif |
cassyarduino | 32:e77cbe3783e5 | 127 | //Search unused client: |
cassyarduino | 32:e77cbe3783e5 | 128 | uint8_t j=0; |
cassyarduino | 32:e77cbe3783e5 | 129 | while ((j<4) && (clients[j])) |
cassyarduino | 32:e77cbe3783e5 | 130 | {j++;} |
cassyarduino | 32:e77cbe3783e5 | 131 | if (j>=4) |
cassyarduino | 32:e77cbe3783e5 | 132 | { |
cassyarduino | 32:e77cbe3783e5 | 133 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 32:e77cbe3783e5 | 134 | LogObject.uart_send_strln(F("Too many client")); |
cassyarduino | 32:e77cbe3783e5 | 135 | #endif |
cassyarduino | 32:e77cbe3783e5 | 136 | client.stop(); |
cassyarduino | 32:e77cbe3783e5 | 137 | } |
cassyarduino | 32:e77cbe3783e5 | 138 | else |
cassyarduino | 32:e77cbe3783e5 | 139 | { |
cassyarduino | 32:e77cbe3783e5 | 140 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 32:e77cbe3783e5 | 141 | LogObject.uart_send_str(F("Save client to ID:")); |
cassyarduino | 32:e77cbe3783e5 | 142 | LogObject.uart_send_decln(j); |
cassyarduino | 32:e77cbe3783e5 | 143 | #endif |
cassyarduino | 32:e77cbe3783e5 | 144 | clients[j] = client; |
cassyarduino | 32:e77cbe3783e5 | 145 | // clead out the input buffer: |
cassyarduino | 32:e77cbe3783e5 | 146 | client.flush(); |
cassyarduino | 32:e77cbe3783e5 | 147 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 32:e77cbe3783e5 | 148 | LogObject.uart_send_strln(F("We have a new client")); |
cassyarduino | 32:e77cbe3783e5 | 149 | #endif |
cassyarduino | 32:e77cbe3783e5 | 150 | client.println(F("Hello, client!")); |
cassyarduino | 32:e77cbe3783e5 | 151 | client.print(F("my IP: ")); |
cassyarduino | 32:e77cbe3783e5 | 152 | client.println(Ethernet.localIP()); |
cassyarduino | 0:e3fb1267e3c3 | 153 | } |
cassyarduino | 0:e3fb1267e3c3 | 154 | } |
cassyarduino | 0:e3fb1267e3c3 | 155 | |
cassyarduino | 32:e77cbe3783e5 | 156 | if (client.available() > 0) |
cassyarduino | 32:e77cbe3783e5 | 157 | { |
cassyarduino | 32:e77cbe3783e5 | 158 | i=0; |
cassyarduino | 32:e77cbe3783e5 | 159 | while ((i<4) && (clients[i]!=client)) |
cassyarduino | 32:e77cbe3783e5 | 160 | {i++;} |
cassyarduino | 32:e77cbe3783e5 | 161 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 32:e77cbe3783e5 | 162 | LogObject.uart_send_str(F("Message received from client ID:")); |
cassyarduino | 32:e77cbe3783e5 | 163 | LogObject.uart_send_decln(i); |
cassyarduino | 32:e77cbe3783e5 | 164 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 165 | // read the bytes incoming from the client: |
cassyarduino | 0:e3fb1267e3c3 | 166 | char thisChar = client.read(); |
cassyarduino | 0:e3fb1267e3c3 | 167 | // echo the bytes back to all other connected clients: |
cassyarduino | 32:e77cbe3783e5 | 168 | for (uint8_t j=0;j<4;j++) |
cassyarduino | 32:e77cbe3783e5 | 169 | { |
cassyarduino | 32:e77cbe3783e5 | 170 | if (clients[j] && clients[j]!=client) |
cassyarduino | 32:e77cbe3783e5 | 171 | { |
cassyarduino | 32:e77cbe3783e5 | 172 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 32:e77cbe3783e5 | 173 | LogObject.uart_send_str(F("Message forwarded to client ID:")); |
cassyarduino | 32:e77cbe3783e5 | 174 | LogObject.uart_send_decln(j); |
cassyarduino | 32:e77cbe3783e5 | 175 | #endif |
cassyarduino | 32:e77cbe3783e5 | 176 | clients[j].write(thisChar); |
cassyarduino | 32:e77cbe3783e5 | 177 | } |
cassyarduino | 0:e3fb1267e3c3 | 178 | } |
cassyarduino | 0:e3fb1267e3c3 | 179 | // echo the bytes to the server as well: |
cassyarduino | 0:e3fb1267e3c3 | 180 | #if ACTLOGLEVEL>=LOG_INFO |
cassyarduino | 11:3fb19220d9ec | 181 | #if defined(ARDUINO) |
cassyarduino | 11:3fb19220d9ec | 182 | LogObject.write(thisChar); |
cassyarduino | 11:3fb19220d9ec | 183 | #endif |
cassyarduino | 11:3fb19220d9ec | 184 | #if defined(__MBED__) |
cassyarduino | 11:3fb19220d9ec | 185 | LogObject.putc(thisChar); |
cassyarduino | 11:3fb19220d9ec | 186 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 187 | #endif |
cassyarduino | 0:e3fb1267e3c3 | 188 | } |
cassyarduino | 0:e3fb1267e3c3 | 189 | } |
cassyarduino | 11:3fb19220d9ec | 190 | for (uint8_t i=0;i<4;i++) { |
cassyarduino | 0:e3fb1267e3c3 | 191 | if (!(clients[i].connected())) { |
cassyarduino | 0:e3fb1267e3c3 | 192 | // client.stop() invalidates the internal socket-descriptor, so next use of == will allways return false; |
cassyarduino | 0:e3fb1267e3c3 | 193 | clients[i].stop(); |
cassyarduino | 0:e3fb1267e3c3 | 194 | } |
cassyarduino | 0:e3fb1267e3c3 | 195 | } |
cassyarduino | 0:e3fb1267e3c3 | 196 | } |
cassyarduino | 11:3fb19220d9ec | 197 | |
cassyarduino | 11:3fb19220d9ec | 198 | #if defined(__MBED__) |
cassyarduino | 11:3fb19220d9ec | 199 | } |
cassyarduino | 11:3fb19220d9ec | 200 | #endif |