Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Sudeshna19 opened this issue Jun 21, 2017 · 1 comment

Comments

@Sudeshna19
Copy link

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

@Sudeshna19
Copy link
Author

I also tried out the same code in MKR1000 and IDE 1.8.1. I used size_rcvd = client1.read(data2, 2) instead of two read(). I saw the prints as data_s 65 0 even though the size_rcvd = 2. It was always neglecting the previously sent byte.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant