0

I am trying to send and receive data with the TCP server but I am not receiving data.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>

// Parametros de conexion a red WIFI.
const char* ssid = "bits";
const char* password = "12345";

// Start a TCP Server on port 5000
WiFiServer server(5000);

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid,password);
  Serial.println("");
  //Wait for connection
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 Serial.println(WiFi.localIP()); // IP

  // Start the TCP server
  server.begin();
}

void loop() {
  TCPServer();
}

void TCPServer () {
   WiFiClient client = server.available();
    if (client) {
      Serial.println("We have a new client");
      Serial.println("Hello, client!");
        if (client.available() > 0) {
            char thisChar = client.read();
            Serial.println("We got data");
            Serial.println(thisChar);
            delay(200);
        }
    }
}

Test Capture: In the capture it is seen that the code is executed until the following instruction.

Serial.println("We have a new client");
Serial.println("Hello, client!");

Test rec

Test sen

2 Answers 2

1

You have created a TCP server. You need to connect other clients to it to start data send and receive process between them. If you just simply run this and wait, then you probably won't get anything because nobody is attempting to connect to it.

0

I found the solution by modifying the code

void TCPServer () {
   WiFiClient client = server.available();
  if (client) 
  { 
    Serial.println("New Client");

    while(!client.available()&&client.connected())
    {
    delay(1);
    }

    String linea1 = client.readStringUntil('r');
    Serial.println(linea1);
  }
}

test2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.