Skip to content

Commit 432bcf5

Browse files
bbx10me-no-dev
authored andcommitted
Add WiFiServer hasClient and WiFiTelnetToSerial example (espressif#394)
WiFiTelnetToSerial is also a test for hasClient().
1 parent db09ca8 commit 432bcf5

File tree

3 files changed

+157
-2
lines changed

3 files changed

+157
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
WiFiTelnetToSerial - Example Transparent UART to Telnet Server for ESP32
3+
4+
Copyright (c) 2017 Hristo Gochkov. All rights reserved.
5+
This file is part of the ESP32 WiFi library for Arduino environment.
6+
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20+
*/
21+
#include <WiFi.h>
22+
#include <WiFiMulti.h>
23+
24+
WiFiMulti wifiMulti;
25+
26+
//how many clients should be able to telnet to this ESP32
27+
#define MAX_SRV_CLIENTS 1
28+
const char* ssid = "**********";
29+
const char* password = "**********";
30+
31+
WiFiServer server(23);
32+
WiFiClient serverClients[MAX_SRV_CLIENTS];
33+
34+
HardwareSerial Serial1(2); // UART1/Serial1 pins 16,17
35+
36+
void setup() {
37+
Serial.begin(115200);
38+
Serial.println("\nConnecting");
39+
40+
wifiMulti.addAP(ssid, password);
41+
wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
42+
wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");
43+
44+
Serial.println("Connecting Wifi ");
45+
for (int loops = 10; loops > 0; loops--) {
46+
if (wifiMulti.run() == WL_CONNECTED) {
47+
Serial.println("");
48+
Serial.print("WiFi connected ");
49+
Serial.print("IP address: ");
50+
Serial.println(WiFi.localIP());
51+
break;
52+
}
53+
else {
54+
Serial.println(loops);
55+
delay(1000);
56+
}
57+
}
58+
if (wifiMulti.run() != WL_CONNECTED) {
59+
Serial.println("WiFi connect failed");
60+
delay(1000);
61+
ESP.restart();
62+
}
63+
64+
//start UART and the server
65+
Serial1.begin(9600);
66+
server.begin();
67+
server.setNoDelay(true);
68+
69+
Serial.print("Ready! Use 'telnet ");
70+
Serial.print(WiFi.localIP());
71+
Serial.println(" 23' to connect");
72+
}
73+
74+
void loop() {
75+
uint8_t i;
76+
if (wifiMulti.run() == WL_CONNECTED) {
77+
//check if there are any new clients
78+
if (server.hasClient()){
79+
for(i = 0; i < MAX_SRV_CLIENTS; i++){
80+
//find free/disconnected spot
81+
if (!serverClients[i] || !serverClients[i].connected()){
82+
if(serverClients[i]) serverClients[i].stop();
83+
serverClients[i] = server.available();
84+
if (!serverClients[i]) Serial.println("available broken");
85+
Serial.print("New client: ");
86+
Serial.print(i); Serial.print(' ');
87+
Serial.println(serverClients[i].remoteIP());
88+
break;
89+
}
90+
}
91+
if (i >= MAX_SRV_CLIENTS) {
92+
//no free/disconnected spot so reject
93+
server.available().stop();
94+
}
95+
}
96+
//check clients for data
97+
for(i = 0; i < MAX_SRV_CLIENTS; i++){
98+
if (serverClients[i] && serverClients[i].connected()){
99+
if(serverClients[i].available()){
100+
//get data from the telnet client and push it to the UART
101+
while(serverClients[i].available()) Serial1.write(serverClients[i].read());
102+
}
103+
}
104+
else {
105+
if (serverClients[i]) {
106+
serverClients[i].stop();
107+
}
108+
}
109+
}
110+
//check UART for data
111+
if(Serial1.available()){
112+
size_t len = Serial1.available();
113+
uint8_t sbuf[len];
114+
Serial1.readBytes(sbuf, len);
115+
//push UART data to all connected telnet clients
116+
for(i = 0; i < MAX_SRV_CLIENTS; i++){
117+
if (serverClients[i] && serverClients[i].connected()){
118+
serverClients[i].write(sbuf, len);
119+
delay(1);
120+
}
121+
}
122+
}
123+
}
124+
else {
125+
Serial.println("WiFi not connected!");
126+
for(i = 0; i < MAX_SRV_CLIENTS; i++) {
127+
if (serverClients[i]) serverClients[i].stop();
128+
}
129+
delay(1000);
130+
}
131+
}

libraries/WiFi/src/WiFiServer.cpp

+23-1
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,16 @@ void WiFiServer::stopAll(){}
4040
WiFiClient WiFiServer::available(){
4141
if(!_listening)
4242
return WiFiClient();
43+
int client_sock;
44+
if (_accepted_sockfd >= 0) {
45+
client_sock = _accepted_sockfd;
46+
_accepted_sockfd = -1;
47+
}
48+
else {
4349
struct sockaddr_in _client;
4450
int cs = sizeof(struct sockaddr_in);
45-
int client_sock = accept(sockfd, (struct sockaddr *)&_client, (socklen_t*)&cs);
51+
client_sock = accept(sockfd, (struct sockaddr *)&_client, (socklen_t*)&cs);
52+
}
4653
if(client_sock >= 0){
4754
int val = 1;
4855
if(setsockopt(client_sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&val, sizeof(int)) == ESP_OK) {
@@ -70,6 +77,8 @@ void WiFiServer::begin(){
7077
return;
7178
fcntl(sockfd, F_SETFL, O_NONBLOCK);
7279
_listening = true;
80+
_noDelay = false;
81+
_accepted_sockfd = -1;
7382
}
7483

7584
void WiFiServer::setNoDelay(bool nodelay) {
@@ -80,6 +89,19 @@ bool WiFiServer::getNoDelay() {
8089
return _noDelay;
8190
}
8291

92+
bool WiFiServer::hasClient() {
93+
if (_accepted_sockfd >= 0) {
94+
return true;
95+
}
96+
struct sockaddr_in _client;
97+
int cs = sizeof(struct sockaddr_in);
98+
_accepted_sockfd = accept(sockfd, (struct sockaddr *)&_client, (socklen_t*)&cs);
99+
if (_accepted_sockfd >= 0) {
100+
return true;
101+
}
102+
return false;
103+
}
104+
83105
void WiFiServer::end(){
84106
close(sockfd);
85107
sockfd = -1;

libraries/WiFi/src/WiFiServer.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
class WiFiServer : public Server {
2727
private:
2828
int sockfd;
29+
int _accepted_sockfd = -1;
2930
uint16_t _port;
3031
uint8_t _max_clients;
3132
bool _listening;
@@ -34,13 +35,14 @@ class WiFiServer : public Server {
3435
public:
3536
void listenOnLocalhost(){}
3637

37-
WiFiServer(uint16_t port=80, uint8_t max_clients=4):sockfd(-1),_port(port),_max_clients(max_clients),_listening(false){}
38+
WiFiServer(uint16_t port=80, uint8_t max_clients=4):sockfd(-1),_accepted_sockfd(-1),_port(port),_max_clients(max_clients),_listening(false),_noDelay(false){}
3839
~WiFiServer(){ end();}
3940
WiFiClient available();
4041
WiFiClient accept(){return available();}
4142
void begin();
4243
void setNoDelay(bool nodelay);
4344
bool getNoDelay();
45+
bool hasClient();
4446
size_t write(const uint8_t *data, size_t len);
4547
size_t write(uint8_t data){
4648
return write(&data, 1);

0 commit comments

Comments
 (0)