Go Down

Topic: Upper limit for client.print() string length (Read 7505 times) previous topic - next topic

liuzengqiang

Is there an upper limit on how many characters you can send at a time with client.print()?

I am seeing some problems when I sent a string 112 bytes long, problem when I send a string 100 bytes long, but no problem with 89 byte long string.

Then I cut the long string in half and did two client.print() and no more problem. What is the maximal length then? Thanks.
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

SurferTim

#1
Apr 20, 2013, 12:19 pm Last Edit: Apr 20, 2013, 12:24 pm by SurferTim Reason: 1
Yes, there is an upper limit, but you are not even close. The max you can send at a time is about 1400 characters, but most Arduinos do not have enough SRAM for a buffer that big. If you try that with an Uno, you would run out of SRAM.

edit: This applies to the ethernet shield. I don't know about the Wifi shield. I have sent 500 characters at a time with my Mega/ethernet shield, but the code would not run on an Uno due to SRAM limitations.

liuzengqiang

Thanks SurferTim. I am using a MEGA so memory should not be the problem. I will also check with the web master for more clues. I suppose the lower layer SPI functions might have limit on max length?! But I client.write somehow fragments the packet as needed?
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

SurferTim

Maybe if you post your code, it will help me or someone else see the problem.

liuzengqiang

SurferTim,

Thanks for your help. Here is the code that sends the packet to the server:

Original code that stops working (returning 0) when the string becomes too long (100 bytes is too long, 90 is not. I wasn't patient enough to find the exact limit)
Code: [Select]
int SendPacket(char* buffer)
{
  unsigned long connect_time;
  if (client.connect(server, 1035))
  {
    connect_time=millis();
    Debug->println("Connected to server");
    client.println(buffer);
  }
 
  else return 0;
 
  while (!client.available())
  {
    if (millis()-connect_time>server_response_time)
    {
      client.stop();
      return 0;
    }
  }
 
  return client.read();
}


Modified code that does multiple client.println(), which works. In this code, the caller needs to intentionally insert separators (I chose '~') within the long string so this function will replace them with '\0' (fragment the long message) and send the long message one fragment at a time.
Code: [Select]
int SendPacket(char* buffer)
{
  int nptr=1,i=0;
  int ptrs[10];
  ptrs[0]=0;
  unsigned long connect_time;
  if (client.connect(server, 1035))
  {
    connect_time=millis();
    Debug->println(F("Connected to server"));
    while ((buffer[i]!='\0'))
    {
      if (buffer[i]==string_segment_marker) // found one marker
      {
        buffer[i]='\0'; // replace with \0
        ptrs[nptr]=i+1; // store the pointer to the next string segments
        nptr++; // increment the number of pointers or segments
      }
      i++;
    }
    for (byte j=0;j<nptr;j++) // print strings segment by segment
    {
      client.println(buffer+ptrs[j]);
    }
  }
 
  else return 0;
 
  while (!client.available())
  {
    if (millis()-connect_time>server_response_time)
    {
      client.stop();
      return 0;
    }
  }
 
  return client.read();
}


The server was working fine with a cell modem and library but I recently added more digits to each value in the long message and have not tested it since. I could reassemble the other prototype that has cell modem and test the long message via cell modem just to make sure the server is not to be blamed.
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

liuzengqiang

#5
Apr 21, 2013, 03:14 am Last Edit: Apr 21, 2013, 04:27 am by liudr Reason: 1
http://arduino.cc/en/Main/ArduinoWiFiShield

Search shankar108's comment. I checked with the web master and he said there is no limit on his end. This may be a real limit.

The same person also reported a possible reason here:

https://github.com/arduino/wifishield/issues/29

I don't know enough of SPI bus to know if there can be any timing issue.
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

woodwax

Hello, I using ESP8266 there is a lot of memory. I discovereded that the upper limit for client.println() function is 2922bytes. Is it possible to pull up this limit? I using ESP8266 for webserver and html(where I have lot of code, javascripth graphs, etc..) page going through this function. Thank You.

SurferTim

Hello, I using ESP8266 there is a lot of memory. I discovereded that the upper limit for client.println() function is 2922bytes. Is it possible to pull up this limit? I using ESP8266 for webserver and html(where I have lot of code, javascripth graphs, etc..) page going through this function. Thank You.
How did you determine the ESP8266 can send a packet larger than 1492 bytes? What code did you use to send the 2922 bytes?


woodwax

#8
Aug 22, 2015, 08:26 pm Last Edit: Aug 22, 2015, 08:56 pm by woodwax
On page about println() is:

"return the number of bytes written"

And here is my code:

Code: [Select]
String  s = "var temperature=[";
  for (i=0; i < temperature.count; i++)
          {
            s += temperature.saved[i]; 
            s += ",";           
          }
        s += "];\n var epoch=[";
       
        for (i=0; i < temperature.count; i++)
          {
         s += temperature.epoch_saved[i]-7200;
         s += ",";
         
          }
         s += "];\n";
   
    Serial.print("Bytes sent:");
    Serial.println(client->println(s));


The String s is enough large. The data sent by client->println(s) function is in file sent_data.txt. You can clearly see the epoch array is not comleted (lack of some characters and ]; ) and it contains 2921 characters + maybe terminating.

On picture bytes_sent.png is the serial output an there is the 2922 bytes sent.

SurferTim

Obviously not an Uno. String s would be grow to be too large for an Uno's SRAM.


woodwax

#10
Aug 22, 2015, 08:52 pm Last Edit: Aug 22, 2015, 09:02 pm by woodwax
Sorry I forget the atachments.
The question is still available, can I extend the number of characters sent by function client.print()?

SurferTim

I send 26.5K in one upload, but not all as one client.print. Why is it necessary to send that all in one client.print?


woodwax

Because, I think send data in one client.print() is a bit faster.

SurferTim

Because, I think send data in one client.print() is a bit faster.
Then what is stopping you?

PaulS

Quote
Because, I think send data in one client.print() is a bit faster.
If it isn't possible, then your assertion is clearly false.
The art of getting good answers lies in asking good questions.

Go Up