Skip to content

Commit 9337b5c

Browse files
committed
Add WiFiTelnetToSerialIPv6 example
To demonstrate new abilities of dual stack WiFiServer and remoteIP6 we add this example. Signed-off-by: Denys Fedoryshchenko <[email protected]>
1 parent 4f63dac commit 9337b5c

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
//Even this example state IPv6, it is dual stack and compatible with IPv4 too
27+
28+
//how many clients should be able to telnet to this ESP32
29+
#define MAX_SRV_CLIENTS 1
30+
const char* ssid = "**********";
31+
const char* password = "**********";
32+
33+
WiFiServer server(23);
34+
WiFiClient serverClients[MAX_SRV_CLIENTS];
35+
36+
void setup() {
37+
Serial.begin(115200);
38+
Serial.println("\nConnecting");
39+
40+
wifiMulti.IPv6(true);
41+
wifiMulti.addAP(ssid, password);
42+
wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
43+
wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");
44+
45+
Serial.println("Connecting Wifi ");
46+
for (int loops = 10; loops > 0; loops--) {
47+
if (wifiMulti.run() == WL_CONNECTED) {
48+
Serial.println("");
49+
Serial.print("WiFi connected ");
50+
Serial.print("IP address: ");
51+
Serial.println(WiFi.localIP());
52+
break;
53+
}
54+
else {
55+
Serial.println(loops);
56+
delay(1000);
57+
}
58+
}
59+
if (wifiMulti.run() != WL_CONNECTED) {
60+
Serial.println("WiFi connect failed");
61+
delay(1000);
62+
ESP.restart();
63+
}
64+
65+
//start UART and the server
66+
Serial1.begin(9600);
67+
server.begin();
68+
server.setNoDelay(true);
69+
70+
Serial.print("Ready! Use 'telnet ");
71+
Serial.print(WiFi.localIP());
72+
Serial.println(" 23' to connect");
73+
}
74+
75+
void loop() {
76+
uint8_t i;
77+
if (wifiMulti.run() == WL_CONNECTED) {
78+
//check if there are any new clients
79+
if (server.hasClient()){
80+
for(i = 0; i < MAX_SRV_CLIENTS; i++){
81+
//find free/disconnected spot
82+
if (!serverClients[i] || !serverClients[i].connected()){
83+
if(serverClients[i]) serverClients[i].stop();
84+
serverClients[i] = server.available();
85+
if (!serverClients[i]) Serial.println("available broken");
86+
Serial.print("New client: ");
87+
Serial.print(i); Serial.print(' ');
88+
Serial.println(serverClients[i].remoteIP6());
89+
break;
90+
}
91+
}
92+
if (i >= MAX_SRV_CLIENTS) {
93+
//no free/disconnected spot so reject
94+
server.available().stop();
95+
}
96+
}
97+
//check clients for data
98+
for(i = 0; i < MAX_SRV_CLIENTS; i++){
99+
if (serverClients[i] && serverClients[i].connected()){
100+
if(serverClients[i].available()){
101+
//get data from the telnet client and push it to the UART
102+
while(serverClients[i].available()) Serial1.write(serverClients[i].read());
103+
}
104+
}
105+
else {
106+
if (serverClients[i]) {
107+
serverClients[i].stop();
108+
}
109+
}
110+
}
111+
//check UART for data
112+
if(Serial1.available()){
113+
size_t len = Serial1.available();
114+
uint8_t sbuf[len];
115+
Serial1.readBytes(sbuf, len);
116+
//push UART data to all connected telnet clients
117+
for(i = 0; i < MAX_SRV_CLIENTS; i++){
118+
if (serverClients[i] && serverClients[i].connected()){
119+
serverClients[i].write(sbuf, len);
120+
delay(1);
121+
}
122+
}
123+
}
124+
}
125+
else {
126+
Serial.println("WiFi not connected!");
127+
for(i = 0; i < MAX_SRV_CLIENTS; i++) {
128+
if (serverClients[i]) serverClients[i].stop();
129+
}
130+
delay(1000);
131+
}
132+
}

0 commit comments

Comments
 (0)