Chat freely about anything...

User avatar
By stavbodik
#57604 Hello , In my project I am using 2 ESP8266 for communicate with each other.

The client has to send sensor data to server, Unfortunately I don't know how to receive/print data correctly on the server side to serial monitor , The data received/prints as gibberish.


Client side :

Code: Select allvoid SendTCP(unsigned long pulseLength)
{
 
  if (!client.connect(ipBroadCast, tcpRemotePort)) {
    Serial.println("TCP Connection failed");
    Serial.println(client.connect(ipBroadCast, tcpRemotePort));
    return;
  }else
  {
    Serial.println("TCP Connected");
  }
  
 client.write((uint8_t*) &pulseLength,4);

}




Server side :
Code: Select allvoid ReceiveTCP()
{
   WiFiClient client = server.available();
   if (client) {

    if (client.connected()) {
      Serial.println("Client Connected ");
    }


    // Wait for data from client to become available
     while (client.connected() && !client.available()) {
        delay(1);
     }
  
     // Read incoming message
     if (client.available() > 0) {
      Serial.print("incoming :");
      int size;
      while ((size = client.available()) > 0) {
        uint8_t* msg = (uint8_t*)malloc(size);
        size = client.read(msg, size);
        Serial.write(msg, size);
        free(msg);
      }
    
    }
  
    // close the connection:
    client.stop();
  }
}




*Note : with same sending method I am able to read the data correctly on PC using server
written in java :

Code: Select all
long pulseLength = 0;

byte[] recivedULBuffer = new byte[4];

inputStreamFromClient.read(recivedULBuffer);

for (int i = 0; i < recivedULBuffer.length; i++){

pulseLength += ((long) recivedULBuffer[i] & 0xffL) << (8 * i);

}

System.out.println(pulseLength);






Thanks !
User avatar
By martinayotte
#57607 According to your code, your are sending/transmit binary instead of plain text, that is Ok, but it need to be handle into such way.

On your PC server, your translate the binary into pulseLength variable, but on the ESP Server your don't do such thing and try to print the binary over serial without any text translation, therefore serial will display as binary too, looking like garbage ...

Instead of having this :
Code: Select all        uint8_t* msg = (uint8_t*)malloc(size);
        size = client.read(msg, size);
        Serial.write(msg, size);
        free(msg);

Try that instead :
Code: Select all        long * pulseLength = (long *)malloc(size);
        size = client.read((uint8_t *)pulseLength, size);
        String str = String("Value = ") + (*pulseLength);
        Serial.println(str);
        free(pulseLength);

This code is untested, therefore if it doesn't work, add debug print for size, etc. for each steps.