Help wifi telnet to serial. :)

I want to send continuous serial data.
but unfortunately after a while the data stop stops and esp starts again.
wifitelnet to serial.

I want to ask for help in this
a usable code.

just send a few kbytes.
about 1-2 Mbytes
would be sent

you need to identify why.

without the code it's hard to tell. ➜ please read How to get the best out of this forum

couple of ideas:

May be you are in a loop and don't give a chance to the rest of the system to run and the watchdog bites you? try adding a yield(); after printing

May be you have a buffer overflow?

who knows... code will help us help you.

1 Like

#include <ESP8266WiFi.h>


//how many clients should be able to telnet to this ESP8266
#define MAX_SRV_CLIENTS 1

const char* ssid = "Sdata";
const char* password = "Kacsa1982";
String nameh="Fanuc.4444";
WiFiServer server(4444);
WiFiClient serverClients[MAX_SRV_CLIENTS];

void setup() {
 
  WiFi.hostname(nameh.c_str());
  Serial1.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial1.print("\nConnecting to "); Serial1.println(ssid);
  uint8_t i = 0;
  while (WiFi.status() != WL_CONNECTED && i++ < 20) delay(500);
  if(i == 21){
    Serial1.print("Could not connect to"); Serial1.println(ssid);
    while(1) delay(500);
  }
  //start UART and the server
  Serial.begin(4800,SERIAL_7E1);
  server.begin();
  server.setNoDelay(false);
  
  Serial1.print("Ready! Use 'telnet ");
  Serial1.print(WiFi.localIP());
  Serial1.println(" 23' to connect");
}

void loop() {
  uint8_t i;
  //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();
        Serial1.print("New client: "); Serial1.print(i);
        
        break;
      }
    }
    //no free/disconnected spot so reject
    if ( i == MAX_SRV_CLIENTS) {
       WiFiClient serverClient = server.available();
       serverClient.stop();
        Serial1.println("Connection rejected ");
    }
  }
  //check clients for data
  for(i = 0; i < MAX_SRV_CLIENTS; i++){
    if (serverClients[i] && serverClients[i].connected()){
      if(serverClients[i].available()){
        //get data from the telnet client and push it to the UART
        while(serverClients[i].available()) Serial.write(serverClients[i].read());
      }
    }
  }
  //check UART for data
  if(Serial.available()){
    size_t len = Serial.available();
    uint8_t sbuf[len];
    Serial.readBytes(sbuf, len);
    //push UART data to all connected telnet clients
    for(i = 0; i < MAX_SRV_CLIENTS; i++){
      if (serverClients[i] && serverClients[i].connected()){
        delay(5);
        serverClients[i].write(sbuf, len);
        
         
        delay(1);
       
      }
    }
  }
}

That would be the code

I use simple code
I'm not that good at it
in programming

please read How to get the best out of this forum and modify your post accordingly to add the code tags (and take that opportunity to remove your password)

try to change that line

        while (serverClients[i].available()) Serial.write(serverClients[i].read());

into

        while (serverClients[i].available()) {
          Serial.write(serverClients[i].read());
          yield();
        }

Who would want me to fix my code?
Where exactly you need to go
to the code

Seems you don't care about fixing your the look of your post and add code tags.

It's important to me as it shows you are not just self centred on your issue but respect the community here.

so.. bye.

1 Like

but I only care
I'm just new here.
And I don't know my format
the code tag

I'm sorry I managed to fix it.
Please help me

I gave you something to try in #5

#include <ESP8266WiFi.h>


//how many clients should be able to telnet to this ESP8266
#define MAX_SRV_CLIENTS 1

const char* ssid = "Sdata";
const char* password = "Kacsa1982";
String nameh="Fanuc.4444";
WiFiServer server(4444);
WiFiClient serverClients[MAX_SRV_CLIENTS];

void setup() {

  WiFi.hostname(nameh.c_str());
  Serial1.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial1.print("\nConnecting to "); Serial1.println(ssid);
  uint8_t i = 0;
  while (WiFi.status() != WL_CONNECTED && i++ < 20) delay(500);
  if(i == 21){
    Serial1.print("Could not connect to"); Serial1.println(ssid);
    while(1) delay(500);
  }
  //start UART and the server
  Serial.begin(4800,SERIAL_7E1);
  server.begin();
  server.setNoDelay(false);
  
  Serial1.print("Ready! Use 'telnet ");
  Serial1.print(WiFi.localIP());
  Serial1.println(" 23' to connect");
}

void loop() {
  uint8_t i;
  //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();
        Serial1.print("New client: "); Serial1.print(i);
        
        break;
      }
    }
    //no free/disconnected spot so reject
    if ( i == MAX_SRV_CLIENTS) {
       WiFiClient serverClient = server.available();
       serverClient.stop();
        Serial1.println("Connection rejected ");
    }
  }
  //check clients for data
  for(i = 0; i < MAX_SRV_CLIENTS; i++){
    if (serverClients[i] && serverClients[i].connected()){
      if(serverClients[i].available()){
        //get data from the telnet client and push it to the UART
        while (serverClients[i].available()) {
          Serial.write(serverClients[i].read());
          
          yield();
          
          
        }
      }
    }
  }
  //check UART for data
  if(Serial.available()){
    size_t len = Serial.available();
    uint8_t sbuf[len];
    Serial.readBytes(sbuf, len);
    //push UART data to all connected telnet clients
    for(i = 0; i < MAX_SRV_CLIENTS; i++){
      if (serverClients[i] && serverClients[i].connected()){
        delay(5);
        serverClients[i].write(sbuf, len);
        
         
        delay(1);
       
      }
    }
  }
}

I put the code in it
but it still disconnects from the wifi network and for a while goes for data sending

4192 send a byte
it disconnects in a few seconds

Max 250 block send

Well many things can go wrong so you code should check for error in sending the payload, lost wifi connection, … and react appropriately

To send 1 - 2 MB, you should use TCP or better yet RUDP (reliable UDP).

Serial unpacketized communication of that size is bound to fail over almost any network.

Also you may want to get rid of the multi-client code and make it single client to make it easier to pinpoint the issue and debug. For example you are reading from multiple clients and sending the date to one destination - surely something you don't intend to do. Also, if any of your clients go away, you need to stop that client. If you look at the original TelnetSerial code, there is a conditional to stop the client.

Also you need to put the code from the original at the start of the loop that tests if you are still attached to the WiFi access point, and if not, to reconnect, and issue a warning.

Finally note that at the last paragraph, if the client disconnects, no warning is issued, and I don't think it can recover from this condition.

I hope this helps.