Skip to content

Commit eb13557

Browse files
authored
Merge pull request #144 from JAndrassy/wifis3_server_accept
WiFiS3 WiFiServer::accept()
2 parents 8c271d0 + aaab68b commit eb13557

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""

Diff for: libraries/WiFiS3/src/WiFiServer.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ WiFiClient WiFiServer::available() {
3232
return WiFiClient();
3333
}
3434

35+
/* -------------------------------------------------------------------------- */
36+
WiFiClient WiFiServer::accept() {
37+
/* -------------------------------------------------------------------------- */
38+
if(_sock != -1) {
39+
string res = "";
40+
modem.begin();
41+
/* call the server accept on esp so that the accept is performed */
42+
if(modem.write(string(PROMPT(_SERVERACCEPT)),res, "%s%d\r\n" , CMD_WRITE(_SERVERACCEPT), _sock)) {
43+
int client_sock = atoi(res.c_str());
44+
return WiFiClient(client_sock);
45+
}
46+
}
47+
return WiFiClient();
48+
}
49+
3550
/* -------------------------------------------------------------------------- */
3651
void WiFiServer::begin(int port) {
3752
/* -------------------------------------------------------------------------- */

Diff for: libraries/WiFiS3/src/WiFiServer.h

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class WiFiServer : public Server {
3737
WiFiServer();
3838
WiFiServer(int p);
3939
WiFiClient available();
40+
WiFiClient accept();
4041
void begin(int port);
4142
void begin();
4243
virtual size_t write(uint8_t);

0 commit comments

Comments
 (0)