Skip to content

Arduino WiFi Shield with IDE 1.8.1 gives wrong values for multiple byte TCP read #46

@Sudeshna19

Description

@Sudeshna19

I have Mega 2560 connected to WiFi Shield (configured as server). I am using IDE 1.8.1. In my code I want to receive say 2 bytes. The client sends 1 byte at a time. Say client sends b first and then A ideally server should read bA after the second byte sent from client. However, I notice in IDE 1.8.1 the values get corrupted. I have attached the code, also pasting it below:
`#include <SPI.h>
#include<WiFi.h>

int configureSuccess = WL_IDLE_STATUS;
char ssid[] = "xyz";
char pass[] = "*******";
WiFiServer server(20000);

static uint8_t alreadyConnected = 0;

void setup() {
Serial.begin(9600);
Serial1.begin(28800);
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
while ( configureSuccess != WL_CONNECTED) {
configureSuccess = WiFi.begin(ssid, pass);
// delay 10000
unsigned long startMillis = millis();
while(millis()- startMillis < 10000)
{
};
}
server.begin();

  for(int trialcount=0;trialcount<5;trialcount++)
  {
      if(Serial)
      {
          break;
      }else
      {
          unsigned long startMillis  = millis();
    while(millis()- startMillis < 1000)
                    {
                    };
      }
  }
  
  if (configureSuccess==WL_CONNECTED)
   { 
     IPAddress ip = WiFi.localIP();
     Serial.print("<<<IP address: ");
     Serial.print(ip);  
     Serial.println(">>>");            
  }
 else
 {
     Serial.println("<<< IP address :Failed to configure. >>>");
 }

}

void loop() {
// wait for a new client:
int avlBytes = 0;
int sizeReceived = 0;
int data2[2];

Serial1.println("loop");
WiFiClient client1 = server.available();
if (client1 == true){
if(!alreadyConnected){
client1.flush();
alreadyConnected++;
Serial1.println("Flushing");
}
avlBytes = client1.available();
if(avlBytes >= 2){
data2[0] = client1.read();
data2[1] = client1.read();
Serial1.println("data_s");
Serial1.println(data2[0]);
Serial1.println(data2[1]);
}
else{
Serial1.println("rcvd");
Serial1.println(avlBytes);
}
}
else {
alreadyConnected = 0;
Serial1.println("No client");
}
unsigned long startMillis = millis();
while(millis()- startMillis < 1000)
{
};
}`

If client send b first then rcvd 1 is printed. If client then sends A then data_s -1 -1 is printed. If I try the same code in IDE 1.6.13 data_s 98 65 is printed which is correct.

However, in both IDE 1.8.1 and IDE 1.6.13 if I use client1.read(data2, 2) instead of two back-to-back read() the Serial1 print would be data_s 98 0. The second byte can be anything random, not always 0.

wifishield1_6_13.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions