Honestly I feel myself so dumb, because I cannot figure it out, what I've made wrong.
I try to implement a simple Telnet - UART communication, but when I send a character with telnet, the UART sends it back.
Code: Select all
WiFiServer server(23);
WiFiClient serverClient;
void setup() {
Serial.begin(115200);
delay(5000);
Serial.println("\nConnecting");
Serial.println("Connecting Wifi ");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi...");
}
Serial.println("");
Serial.print("WiFi connected ");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
//start UART and the server
Serial2.begin(57600);
server.begin();
server.setNoDelay(true);
Serial.print("Ready! Use 'telnet ");
Serial.print(WiFi.localIP());
Serial.println(" 23' to connect");
}
void loop() {
//check if there are any new clients
if (server.hasClient()){
//find free/disconnected spot
if (!serverClient || !serverClient.connected()){
if(serverClient)
serverClient.stop();
serverClient = server.available();
if (!serverClient)
Serial.println("available broken");
else {
server.available().stop();
}
Serial.print("New client: ");
Serial.println(serverClient.remoteIP());
}
}
//check telnet clients for data
if (serverClient && serverClient.connected()){
if(serverClient.available()){
//get data from the telnet client and push it to the UART
while(serverClient.available())
Serial2.write(serverClient.read()); // This triggers the Serial2.available() method below
serverClient.flush();
}
}
else {
if (serverClient) {
serverClient.stop();
}
}
//check UART for data
if(Serial2.available()){
size_t len = Serial2.available();
uint8_t sbuf[len];
Serial2.readBytes(sbuf, len);
//push UART data to all connected telnet clients
if (serverClient && serverClient.connected()){
serverClient.write(sbuf, len);
delay(1);
Serial2.flush();
}
}
}
Thanks in advance!