Pages: [1]   Go Down
Author Topic: WiFi Shield - Buffering outsending data  (Read 1621 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I am porting my old ethernet shield code to support the new WiFi shield (pretty cool).

The porting is quite easy but I noticed a different behavior in sending data which definitely impacts my project.

I send some messages from Arduino :

Code:
...
    sprintf(buffer,"%s=%d#", variable, (int)value);
    client.write((const uint8_t *)buffer, strlen(buffer)*sizeof(char));
...
   
using the ethernet shield each message is received almost instantly from the server which can follow a changing quantity on Arduino.

Using the WiFi, the first message is not received instantly but after a delay together with other messages.
After a new delay (without any message), a new bunch of messages is received. With this behaviour the server is not able doesn't follow the quantity on Arduino.

It seems that messages are queued somewhere.

Any suggestion how to change this behavior or how to flush data ?

Thank you
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
support the new WiFi shield (pretty cool).
Oh, that shield. Umm, which one is that?

What library are you using?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Take a look here:

http://arduino.cc/en/Main/ArduinoWiFiShield
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 116
Posts: 5393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It will buffer somewhere to build a packet. The w5100 ethernet chip does that internally.

How long is the delay? WiFi is not exactly the most immediate response device, especially if the WiFi shield is not the only device connected.
Is that a "couple blinks of the eye" delay?
A "go to lunch" delay?
A "come back tomorrow" delay?

Does it depend on the amount of data you are sending?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK. That describes the hardware. Nothing about your software...
Logged

The Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 5
There are 10 types of people in the world: Those who understand binary, and those who don't.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Since the WiFiShield has a low power consuming chip, I suspect it needs to 'power up' after each period of inactivity. When powered up it will send the data without delay...

- Michel
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@SurferTim

1) Delay is about "couple blinks of the eye"
2) Delay persists even after some time of operation so I don't think that the delay is related to any sort of wake up procedure
3) All messages I am sending are about 10 characters (see code below). I have built a test code with more data, same behavior

@PaulS

1) Official WiFi Library (http://arduino.cc/en/Reference/WiFi)
2) I have almost removed all the code. Now there is only something like this:

Code:
void loop() {


     while(true) {

        ...

        sprintf(buffer,"%s=%d#", variable, (int)value);
        client.write((const uint8_t *)buffer, strlen(buffer)*sizeof(char));
     }

}

@MSpijk

The delay persists even after that WiFi shield has been powered for some time


Thank you all guys for your cooperation
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Now there is only something like this:
Why are you running an infinite loop inside an infinite loop?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Why are you running an infinite loop inside an infinite loop?

This was a stupid mistake I made in the code I posted.

Any more useful suggestion ?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

I have done some more tests.

Ethernet Shield code

Code:
...
IPAddress ip(192,168,1,220);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);

EthernetServer server(80);

...

void loop() {
 
  // listen for incoming clients
  EthernetClient client = server.available();
 
  if (client) {
   
    Serial.println("new client");
           
    while (client.connected()) {

      // Receiving data

      if (client.available()) {
       
        char c = client.read();
        Serial.write(c);
      }
     
      // Sending data
     
      for(int i=0; i<500; i++) {
           
        char buffer[30];
     
        sprintf(buffer,"%s=%d#", "Pot", i);

        client.write((const uint8_t *)buffer, strlen(buffer)*sizeof(char)); 
      }

    }

    delay(1);
   
    client.stop();
    Serial.println("client disconnected");
  }
 
}

The client receives messages continuously

Code:

2012-09-01 10:40:06.540 ConnectTest[3870:403] Pot=0#
2012-09-01 10:40:06.544 ConnectTest[3870:403] Pot=1#
2012-09-01 10:40:06.545 ConnectTest[3870:403] Pot=2#Pot=3#Pot=4#Pot=5#Pot=6#Pot=7#Pot=8#Pot=9#Pot=10#Pot=11#Pot=12#Pot=13#
2012-09-01 10:40:06.546 ConnectTest[3870:403] Pot=14#
2012-09-01 10:40:06.548 ConnectTest[3870:403] Pot=15#Pot=16#Pot=17#
2012-09-01 10:40:06.549 ConnectTest[3870:403] Pot=18#Pot=19#Pot=20#Pot=21#
2012-09-01 10:40:06.551 ConnectTest[3870:403] Pot=22#Pot=23#Pot=24#Pot=25#Pot=26#
2012-09-01 10:40:06.553 ConnectTest[3870:403] Pot=27#Pot=28#Pot=29#Pot=30#Pot=31#
2012-09-01 10:40:06.556 ConnectTest[3870:403] Pot=32#Pot=33#Pot=34#Pot=35#Pot=36#Pot=37#
2012-09-01 10:40:06.557 ConnectTest[3870:403] Pot=38#Pot=39#Pot=40#Pot=41#Pot=42#Pot=43#Pot=44#Pot=45#
2012-09-01 10:40:06.559 ConnectTest[3870:403] Pot=46#Pot=47#Pot=48#Pot=49#Pot=50#
2012-09-01 10:40:06.560 ConnectTest[3870:403] Pot=51#Pot=52#Pot=53#Pot=54#Pot=55#
2012-09-01 10:40:06.562 ConnectTest[3870:403] Pot=56#Pot=57#Pot=58#Pot=59#
2012-09-01 10:40:06.563 ConnectTest[3870:403] Pot=60#Pot=61#Pot=62#Pot=63#
2012-09-01 10:40:06.567 ConnectTest[3870:403] Pot=64#Pot=65#Pot=66#Pot=67#
2012-09-01 10:40:06.570 ConnectTest[3870:403] Pot=68#Pot=69#Pot=70#Pot=71#Pot=72#Pot=73#Pot=74#Pot=75#Pot=76#


WiFi Shield code

Code:

...
WiFiServer server(80);
...

void loop() {
 
  // listen for incoming clients
  WiFiClient client = server.available();
 
  if (client) {
   
    Serial.println("new client");
           
    while (client.connected()) {
     
      // Receiving data

      if (client.available()) {
       
        char c = client.read();
        Serial.write(c);
      }
     
      // Sending data
     
      for(int i=0; i<500; i++) {
           
        char buffer[30];
     
        sprintf(buffer,"%s=%d#", "Pot", i);

        client.write((const uint8_t *)buffer, strlen(buffer)*sizeof(char));         
      }

    }

    delay(1);
   
    client.stop();
    Serial.println("client disonnected");
  }
}

The client receives messages in bunch and a delay of about 2 secs between different bunches.

Code:

2012-09-01 10:32:40.359 ConnectTest[3475:403] Pot=0#Pot=1#Pot=2#Pot=3#Pot=4#Pot=5#Pot=6#Pot=7#Pot=8#Pot=9#Pot=10#Pot=11#Pot=12#Pot=13#Pot=14#Pot=15#Pot=16#Pot=17#Pot=18#Pot=19#Pot=20#Pot=21#Pot=22#Pot=23#Pot=24#Pot=25#Pot=26#Pot=27#Pot=28#Pot=29#Pot=30#Pot=31#
2012-09-01 10:32:40.360 ConnectTest[3475:403] Pot=32#
2012-09-01 10:32:42.096 ConnectTest[3475:403] Pot=33#Pot=34#Pot=35#Pot=36#Pot=37#Pot=38#Pot=39#Pot=40#Pot=41#Pot=42#Pot=43#Pot=44#Pot=45#Pot=46#Pot=47#Pot=48#Pot=49#Pot=50#Pot=51#Pot=52#Pot=53#Pot=54#Pot=55#Pot=56#Pot=57#Pot=58#Pot=59#Pot=60#Pot=61#Pot=62#Pot=63#Pot=64#
2012-09-01 10:32:42.098 ConnectTest[3475:403] Pot=65#
2012-09-01 10:32:44.120 ConnectTest[3475:403] Pot=66#Pot=67#Pot=68#Pot=69#Pot=70#Pot=71#Pot=72#Pot=73#Pot=74#Pot=75#Pot=76#Pot=77#Pot=78#Pot=79#Pot=80#Pot=81#Pot=82#Pot=83#Pot=84#Pot=85#Pot=86#Pot=87#Pot=88#Pot=89#Pot=90#Pot=91#Pot=92#Pot=93#Pot=94#Pot=95#Pot=96#Pot=97#
2012-09-01 10:32:44.126 ConnectTest[3475:403] Pot=98#
2012-09-01 10:32:46.144 ConnectTest[3475:403] Pot=99#Pot=100#Pot=101#Pot=102#Pot=103#Pot=104#Pot=105#Pot=106#Pot=107#Pot=108#Pot=109#Pot=110#Pot=111#Pot=112#Pot=113#Pot=114#Pot=115#Pot=116#Pot=117#Pot=118#Pot=119#Pot=120#Pot=121#Pot=122#Pot=123#Pot=124#Pot=125#Pot=126#Pot=127#Pot=128#Pot=129#Pot=130#
2012-09-01 10:32:46.147 ConnectTest[3475:403] Pot=131#
2012-09-01 10:32:48.169 ConnectTest[3475:403] Pot=132#Pot=133#Pot=134#Pot=135#Pot=136#Pot=137#Pot=138#Pot=139#Pot=140#Pot=141#Pot=142#Pot=143#Pot=144#Pot=145#Pot=146#Pot=147#Pot=148#Pot=149#Pot=150#Pot=151#Pot=152#Pot=153#Pot=154#Pot=155#Pot=156#Pot=157#Pot=158#Pot=159#Pot=160#Pot=161#Pot=162#Pot=163#
2012-09-01 10:32:48.171 ConnectTest[3475:403] Pot=164#


This indicates that somewhere there is a buffer which has to be filled before data are sent to the client.

Does anyone know how to change this behavior ?

Logged

Toronto, Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Any reason why you use write instead of print? Have you tried flush after the write or print?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what's the matter, it doesn't work in my router!Cartiers
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Any reason why you use write instead of print?

No, I haven't., I have tried print with the same result.

Quote
Have you tried flush after the write or print?

Yes I have. No change.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Have you tried flush after the write or print?

Quote
Yes I have. No change.
I wouldn't expect any change. The Serial.flush() method simply blocks until the output buffer is empty. It doesn't force anything to happen.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, I am experiencing the same issue. When I try to send a message every second, I will get a few messages concatenated together on the server side after 3 seconds (about 180 characters) And when I only send one message, I never receive it, The connection just opens and then eventually times out with no data being transferred. [To be clear, the same code with the EthernetClient works as expected, sending individual messages as client.print(char*) is called]

I was hoping the OP had figured out a solution, or if anyone has new insight into a possible fix (aside from modifying the server to parse apart multiple messages received in the same packet). If necessary I can point you to my code.

Thank you.
Logged

Pages: [1]   Go Up
Jump to: