1

I want to send basic information from a server to a client. 4 numbers representing a random sequence for example 0100. I have put this in byte array and use the "server.write" to send it to the client. At the client side I always get 4 times "255" when i print it on the monitor (when i print in on serial monitor on the server side it shows the right sequence numbers).

This is (only relevant part of ) my code on the server:

int sequence;
byte myseq[4]={0,0,0,0};
int i;
byte a;


void loop() {
  // wait for a new client:
  WiFiClient client = server.available();

  sequence=random(0,3);

for (i = 0; i < 4; i = i + 1) {
  if (i == sequence){
  a=1;
  }
  else{
  a=0;
  }
   myseq[i] = a;
     }



  // when the client sends the first byte, say hello:
  if (client) {
    if (!alreadyConnected) {
      // clead out the input buffer:
      client.flush();
      Serial.println("We have a new client");
      client.println("Hello, client!");
      alreadyConnected = true;
    }

    if (client.available() > 0) {
      // read the bytes incoming from the client:
      char request = client.read();
      Serial.print(request);
      for (i = 0; i < 4; i = i + 1) {
      Serial.print(myseq[i]);
      server.write(myseq[i]);

      }

    }
  }
}

the code on the client side

byte response[4]={0,0,0,0};

void loop() {


if (client.connect(ServerIP, 23)) {
  client.print("741G");
  //String response = client.readStringUntil('\n');
  for (i = 0; i < 4; i = i + 1) {
  response[i] = client.read();
  Serial.println(response[i]);

  }

  //client.stop();
} 

Can somebody help me in explaining what i'm doing wrong ? what happens with the numbers once they get sent ? Apparently they arrive in a different format ?

thanks in advance !

ps : i see now that my sequence numbers are always 4 booleans (half a byte) maybe there is an easier way?

1
  • you don't test with client.available() if the bytes arrived so read returns -1 – Juraj Apr 11 '20 at 5:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.