103 lines
3.1 KiB
C++
103 lines
3.1 KiB
C++
/*
|
|
WiFiTelnetToSerial - Example Transparent UART to Telnet Server for ESP32
|
|
Copyright (c) 2017 Hristo Gochkov. All rights reserved.
|
|
This file is part of the ESP32 WiFi library for Arduino environment.
|
|
This library is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU Lesser General Public
|
|
License as published by the Free Software Foundation; either
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
This library is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Lesser General Public License for more details.
|
|
You should have received a copy of the GNU Lesser General Public
|
|
License along with this library; if not, write to the Free Software
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
*/
|
|
#include <WiFi.h>
|
|
#include <WiFiMulti.h>
|
|
|
|
WiFiMulti wifiMulti;
|
|
|
|
//how many clients should be able to telnet to this ESP32
|
|
#define MAX_SRV_CLIENTS 1
|
|
const char* ssid = "Derpina";
|
|
const char* password = "randomsharedkey1";
|
|
|
|
WiFiServer server(23);
|
|
WiFiClient serverClients[MAX_SRV_CLIENTS];
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
Serial.println("\nConnecting");
|
|
|
|
wifiMulti.addAP(ssid, password);
|
|
wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
|
|
wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");
|
|
|
|
Serial.println("Connecting Wifi ");
|
|
for (int loops = 10; loops > 0; loops--) {
|
|
if (wifiMulti.run() == WL_CONNECTED) {
|
|
Serial.println("");
|
|
Serial.print("WiFi connected ");
|
|
Serial.print("IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
break;
|
|
}
|
|
else {
|
|
Serial.println(loops);
|
|
delay(1000);
|
|
}
|
|
}
|
|
if (wifiMulti.run() != WL_CONNECTED) {
|
|
Serial.println("WiFi connect failed");
|
|
delay(1000);
|
|
ESP.restart();
|
|
}
|
|
|
|
//start UART and the server
|
|
server.begin();
|
|
server.setNoDelay(true);
|
|
|
|
Serial.print("Ready! Use 'telnet ");
|
|
Serial.print(WiFi.localIP());
|
|
Serial.println(" 23' to connect");
|
|
}
|
|
|
|
int testval=0;
|
|
|
|
void loop() {
|
|
testval=testval+1;
|
|
uint8_t i;
|
|
Serial.println(testval);
|
|
if (wifiMulti.run() == WL_CONNECTED) {
|
|
//check if there are any new clients
|
|
if (server.hasClient()){
|
|
for(i = 0; i < MAX_SRV_CLIENTS; i++){
|
|
//find free/disconnected spot
|
|
if (!serverClients[i] || !serverClients[i].connected()){
|
|
if(serverClients[i]) serverClients[i].stop();
|
|
serverClients[i] = server.available();
|
|
if (!serverClients[i]) Serial.println("available broken");
|
|
Serial.print("New client: ");
|
|
Serial.print(i); Serial.print(' ');
|
|
Serial.println(serverClients[i].remoteIP());
|
|
break;
|
|
server.print(Serial.read());
|
|
}
|
|
}
|
|
if (i >= MAX_SRV_CLIENTS) {
|
|
//no free/disconnected spot so reject
|
|
server.available().stop();
|
|
}
|
|
}
|
|
|
|
}
|
|
else {
|
|
Serial.println("WiFi not connected!");
|
|
for(i = 0; i < MAX_SRV_CLIENTS; i++) {
|
|
if (serverClients[i]) serverClients[i].stop();
|
|
}
|
|
delay(1000);
|
|
}
|
|
}
|