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

asked Mar 21, 2017 at 0:51

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.

James Waldby - jwpat7
8,9203 gold badges21 silver badges33 bronze badges
answered Mar 21, 2017 at 4:41
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

answered Mar 22, 2017 at 1:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.