Skip to content

Commit e1f3f5e

Browse files
committed
added server.accept()
1 parent 8816d8a commit e1f3f5e

File tree

6 files changed

+131
-4
lines changed

6 files changed

+131
-4
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
Circuit:
10+
* Board with NINA module (Arduino MKR WiFi 1010, MKR VIDOR 4000 and UNO WiFi Rev.2)
11+
12+
created 18 Dec 2009
13+
by David A. Mellis
14+
modified 9 Apr 2012
15+
by Tom Igoe
16+
redesigned to make use of operator== 25 Nov 2013
17+
by Norbert Truchsess
18+
changed for server.accept() in July 2018
19+
by Paul Stoffregen for Ethernet library
20+
21+
*/
22+
23+
#include <SPI.h>
24+
#include <WiFiNINA.h>
25+
26+
#include "arduino_secrets.h"
27+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
28+
char ssid[] = SECRET_SSID; // your network SSID (name)
29+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
30+
31+
int status = WL_IDLE_STATUS;
32+
33+
// telnet defaults to port 23
34+
WiFiServer server(23);
35+
36+
WiFiClient clients[8];
37+
38+
void setup() {
39+
//Initialize serial and wait for port to open:
40+
Serial.begin(9600);
41+
while (!Serial) {
42+
; // wait for serial port to connect. Needed for native USB port only
43+
}
44+
45+
// check for the WiFi module:
46+
if (WiFi.status() == WL_NO_MODULE) {
47+
Serial.println("Communication with WiFi module failed!");
48+
// don't continue
49+
while (true);
50+
}
51+
52+
String fv = WiFi.firmwareVersion();
53+
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
54+
Serial.println("Please upgrade the firmware");
55+
}
56+
57+
// attempt to connect to WiFi network:
58+
while (status != WL_CONNECTED) {
59+
Serial.print("Attempting to connect to SSID: ");
60+
Serial.println(ssid);
61+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
62+
status = WiFi.begin(ssid, pass);
63+
64+
// wait 10 seconds for connection:
65+
delay(10000);
66+
}
67+
68+
// start the server:
69+
server.begin();
70+
71+
Serial.print("Chat server address:");
72+
Serial.println(WiFi.localIP());
73+
}
74+
75+
void loop() {
76+
// check for any new client connecting, and say hello (before any incoming data)
77+
WiFiClient newClient = server.accept();
78+
if (newClient) {
79+
for (byte i=0; i < 8; i++) {
80+
if (!clients[i]) {
81+
Serial.print("We have a new client #");
82+
Serial.println(i);
83+
newClient.print("Hello, client number: ");
84+
newClient.println(i);
85+
// Once we "accept", the client is no longer tracked by EthernetServer
86+
// so we must store it into our list of clients
87+
clients[i] = newClient;
88+
break;
89+
}
90+
}
91+
}
92+
93+
// check for incoming data from all clients
94+
for (byte i=0; i < 8; i++) {
95+
if (clients[i] && clients[i].available() > 0) {
96+
// read bytes from a client
97+
byte buffer[80];
98+
int count = clients[i].read(buffer, 80);
99+
// write the bytes to all other connected clients
100+
for (byte j=0; j < 8; j++) {
101+
if (j != i && clients[j].connected()) {
102+
clients[j].write(buffer, count);
103+
}
104+
}
105+
}
106+
}
107+
108+
// stop any clients which disconnect
109+
for (byte i=0; i < 8; i++) {
110+
if (clients[i] && !clients[i].connected()) {
111+
Serial.print("disconnect client #");
112+
Serial.println(i);
113+
clients[i].stop();
114+
}
115+
}
116+
117+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""

src/WiFiServer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ WiFiClient WiFiServer::available(byte* status)
8080
return WiFiClient(255);
8181
}
8282

83+
WiFiClient WiFiServer::accept()
84+
{
85+
int sock = ServerDrv::availServer(_sock, true);
86+
return WiFiClient(sock);
87+
}
88+
8389
uint8_t WiFiServer::status() {
8490
if (_sock == NO_SOCKET_AVAIL) {
8591
return CLOSED;

src/WiFiServer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class WiFiServer : public Server {
3838
public:
3939
WiFiServer(uint16_t);
4040
WiFiClient available(uint8_t* status = NULL);
41+
WiFiClient accept();
4142
void begin();
4243
virtual size_t write(uint8_t);
4344
virtual size_t write(const uint8_t *buf, size_t size);

src/utility/server_drv.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,16 +256,17 @@ uint16_t ServerDrv::availData(uint8_t sock)
256256
return len;
257257
}
258258

259-
uint8_t ServerDrv::availServer(uint8_t sock)
259+
uint8_t ServerDrv::availServer(uint8_t sock, uint8_t accept)
260260
{
261261
if (!SpiDrv::available()) {
262262
return 255;
263263
}
264264

265265
WAIT_FOR_SLAVE_SELECT();
266266
// Send Command
267-
SpiDrv::sendCmd(AVAIL_DATA_TCP_CMD, PARAM_NUMS_1);
268-
SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM);
267+
SpiDrv::sendCmd(AVAIL_DATA_TCP_CMD, PARAM_NUMS_2);
268+
SpiDrv::sendParam(&sock, sizeof(sock));
269+
SpiDrv::sendParam(&accept, sizeof(accept), LAST_PARAM);
269270

270271
// pad to multiple of 4
271272
SpiDrv::readChar();

src/utility/server_drv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class ServerDrv
5757

5858
static uint16_t availData(uint8_t sock);
5959

60-
static uint8_t availServer(uint8_t sock);
60+
static uint8_t availServer(uint8_t sock, uint8_t accept = false);
6161

6262
static uint8_t checkDataSent(uint8_t sock);
6363

0 commit comments

Comments
 (0)