//parts from WiFiTelnetToSerial in ESP32 WiFi library from Hristo Gochkov #define telnetPort xxxxx #define WEBport yyyyy #ifdef ESP8266 #include #include #include #include ESP8266WebServer WEBserver ( WEBport ); #define MAX_SRV_CLIENTS 3 // max 3 clients connect to telnet server #define ledLight LOW #define ledDark HIGH #else #include #include #include #include ESP32WebServer WEBserver ( WEBport ); #define MAX_SRV_CLIENTS 8 // tested on ESP32 #define ledLight HIGH #define ledDark LOW #endif uint8_t ServerIPlocal[4] {192,168,1,140} ; //Node static IP uint8_t gatewayIPlocal[4] {192,168,1,1} ; uint8_t subnetIPlocal[4] {255,255,255,0} ; String routerGlobalIP = "x.y.z.w" ; // or ServerIPlocal // use on WEB page refresh only const char* ssid = "mySSID"; const char* password = "1234567890"; WiFiServer telnetServer(telnetPort); WiFiClient serverClients[MAX_SRV_CLIENTS]; bool clientHandled = true ; void setup(void) { delay(1000); Serial.begin(115200) ; delay(1000); #ifdef BUILTIN_LED Serial.print("BUILTIN_LED defined = "); Serial.println(BUILTIN_LED); pinMode (BUILTIN_LED, OUTPUT) ; digitalWrite(BUILTIN_LED, ledDark); #else Serial.println("No BUILTIN_LED "); #endif delay(1000); Serial.begin(115200) ; WiFi.begin(ssid, password ); delay(1000); // min 1000 WiFi.config(ServerIPlocal, gatewayIPlocal, subnetIPlocal) ; // WiFi.setAutoReconnect(true); delay(1000); Serial.print("\nConnecting to "); Serial.println(ssid); uint8_t i = 0; while (WiFi.status() != WL_CONNECTED && i++ < 20){ Serial.print("."); delay(500); } if(i == 21){ Serial.print("Could not connect to "); Serial.println(ssid); while(1) delay(500); } ; #ifdef BUILTIN_LED digitalWrite(BUILTIN_LED, ledLight); #endif Serial.print("WiFi.localIP(): "); Serial.println(WiFi.localIP()); //start the server telnetServer.begin(); telnetServer.setNoDelay(true); Serial.print("Ready! Use 'telnet "); Serial.print(WiFi.localIP()); Serial.print(":"); Serial.print(telnetPort); Serial.print(" or "); Serial.print(routerGlobalIP); Serial.print(":"); Serial.print(telnetPort); Serial.println(" to connect"); WEBserver.on("/", handle_index); WEBserver.onNotFound(handleNotFound); WEBserver.begin(); Serial.println("HTTP server started"); #ifdef ESP32 Serial.print("WIFI_IF_STA = "); Serial.println(WIFI_IF_STA); Serial.print("WIFI_IF_AP = "); Serial.println(WIFI_IF_AP); #endif Serial.print("WiFi.SSID() = "); Serial.print(WiFi.SSID()); Serial.print(" IP Address = : "); Serial.println(WiFi.localIP()); Serial.print("Received signal strength (RSSI):"); Serial.print(WiFi.RSSI()); Serial.println(" dBm"); Serial.print("macAddress() = "); Serial.println(WiFi.macAddress()); String WiFiMode [4] {"WIFI_OFF", "WIFI_STA", "WIFI_AP", "WIFI_AP_STA"} ; Serial.print("WiFi.getMode() = "); Serial.print(WiFi.getMode()); Serial.print(" = "); Serial.println(WiFiMode[WiFi.getMode()]); /* ESP8266WiFiType.h : * typedef enum WiFiMode { * WIFI_OFF = 0, WIFI_STA = 1, WIFI_AP = 2, WIFI_AP_STA = 3 *} WiFiMode_t;*/ } void loop() { while (WiFi.status() != WL_CONNECTED ){ Serial.println("try reconnect"); #ifdef BUILTIN_LED digitalWrite(BUILTIN_LED, ledLight); delay(200); digitalWrite(BUILTIN_LED, ledDark); delay(200); #endif WiFi.begin(ssid, password ); delay(1000); // min 1000 WiFi.config(ServerIPlocal, gatewayIPlocal, subnetIPlocal) ; delay(1000); } ; #ifdef BUILTIN_LED digitalWrite(BUILTIN_LED, (digitalRead(BUILTIN_LED)^ 1 ) ); #endif WEBserver.handleClient(); uint8_t i; int16_t len = 5000; char sbuf[len]; // char only len = 0; //check if there are any new clients if (telnetServer.hasClient()){ clientHandled = false ; for(i = 0; i < MAX_SRV_CLIENTS; i++){ //find free/disconnected spot if (!serverClients[i] || !serverClients[i].connected()){ if(serverClients[i]){ // serverClients[i] not connected = !serverClients[i].connected()) serverClients[i].stop(); Serial.print("serverClients["); Serial.print(i); Serial.println("].stop "); } ; // end if(serverClients[i]) serverClients[i] = telnetServer.available(); serverClients[i].flush(); // clear input buffer, else you get strange characters Serial.print("New client: "); Serial.println(i); clientHandled = true ; break ; } ; // end if (!serverClients[i] || !serverClients[i].connected()) } ; // end for i if (!clientHandled) { Serial.println("no free/disconnected spot so reject"); WiFiClient serverClient = telnetServer.available(); serverClient.stop(); } ; // end if (!clientHandled) } ; // end if (telnetServer.hasClient()) //check clients for data for(i = 0; i < MAX_SRV_CLIENTS; i++){ if (serverClients[i] && serverClients[i].connected()){ //Serial.print("serverClients["); Serial.print(i); Serial.println("].connected "); if(serverClients[i].available()){ //get data from the telnet client and push it to the UART //Serial.print("serverClients["); Serial.print(i); Serial.println("].available "); #ifdef BUILTIN_LED digitalWrite(BUILTIN_LED, (digitalRead(BUILTIN_LED)^ 1 ) ); #endif len = 0; while(serverClients[i].available()) { len++; sbuf[len-1] = serverClients[i].read(); Serial.write(sbuf[len-1]); } ; // while(serverClients[i].available()) #ifdef BUILTIN_LED digitalWrite(BUILTIN_LED, (digitalRead(BUILTIN_LED)^ 1 ) ); #endif //push data to all connected telnet clients for(uint8_t j = 0; j < MAX_SRV_CLIENTS; j++){ if (serverClients[j] && serverClients[j].connected() && ( i != j ) ){ serverClients[j].write(sbuf, len); delay(1); } ; // if (serverClients[j] && serverClients[j].connected() && ( i != j )) } ; //for j #ifdef BUILTIN_LED digitalWrite(BUILTIN_LED, (digitalRead(BUILTIN_LED)^ 1 ) ); #endif len = 0; } ; // if(serverClients[i].available()) } ; // if (serverClients[i] && serverClients[i].connected()) } ; //for i /* //push data to all connected telnet clients for(i = 0; i < MAX_SRV_CLIENTS; i++){ if (serverClients[i] && serverClients[i].connected()){ serverClients[i].write(sbuf, len); delay(1); } ; // if (serverClients[i] && serverClients[i].connected()) } ; //for i */ // delay (1000) ; } ; // loop void handle_index() { String s = "\r\n"; s += "\r\n"; s += "\r\n"; s += "\r\n"; s += "\r\n"; s += "telnet multi server\r\n" ; s += "\r\n"; s += "\r\n"; s += " \r\n" ; s += "

\r\n" ; s += "