Fragmentation with Adafruit's CC3000 library

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
klauskinski
 
Posts: 6
Joined: Tue Jul 01, 2014 5:36 pm

Fragmentation with Adafruit's CC3000 library

Post by klauskinski »

Hi,

I'm playing with the CC3000 card and the Adafruit's CC3000 library. The data actually gets across to the server, however because it's a proprietary protocol I can't use one of the "prepackaged" protocols like http. I'm simply sending a string using the client.println(). I expected the string to be in one packet as it is relatively short, app 100-150 chars. The result was surprising - the library creates one packet for each character I send.

Here's a sample string:
$-1|0.00|0.00|0|0.00|-1|0.00|0|48.28|75.88|0.00|0.00|98657.25|4.15|0.99@

And here's the code:

Code: Select all

 Serial.print(F("Sending measurement: ")); Serial.println(reading);
  Adafruit_CC3000_Client client = cc3000.connectTCP(ip, SERVER_PORT);
  if (client.connected()) {
    client.println(reading);
  }
  else {
    Serial.println(F("Error sending measurement!"));
  }
   delay(100);
  client.close();
Any ideas what I should do differently?

Thanks,
Klaus

User avatar
tdicola
 
Posts: 1074
Joined: Thu Oct 17, 2013 9:11 pm

Re: Fragmentation with Adafruit's CC3000 library

Post by tdicola »

Yep what you're seeing is a side effect of how the Print class (which implements the print and println functions) is implemented in Arduino. It will go through character by character and call a lower level write which ends up sending each character as a different packet with the CC3000 client. Definitely not the best option for speed. :) Luckily there are a couple functions on the CC3000 client you can use instead for better performance.

Try switching to use client.fastrprintln instead of client.println. Likewise you can use client.fastrprint instead of client.print. This will take the entire string and send it in one go instead of breaking it down character by character.

One thing to be aware of though is that there's a limit to how much you can send in one packet. It's based on the memory available to the CC3000 buffer and on an Arduino it's 90 characters, so make sure a single fastrprint or fastrprintln doesn't send more than 90 characters at a time. If you hit this limit unfortunately the Arduino will lock up because the driver code goes into an infinite loop to prevent overwriting random parts of memory.

User avatar
klauskinski
 
Posts: 6
Joined: Tue Jul 01, 2014 5:36 pm

Re: Fragmentation with Adafruit's CC3000 library

Post by klauskinski »

tdicola wrote: Try switching to use client.fastrprintln instead of client.println. Likewise you can use client.fastrprint instead of client.print. This will take the entire string and send it in one go instead of breaking it down character by character.
Thanks, I'll try this and report back. Is there a documentation for the library somewhere? I'm ok in C, but not really good in even reading the libraries and/or C#.
tdicola wrote: One thing to be aware of though is that there's a limit to how much you can send in one packet. It's based on the memory available to the CC3000 buffer and on an Arduino it's 90 characters, so make sure a single fastrprint or fastrprintln doesn't send more than 90 characters at a time. If you hit this limit unfortunately the Arduino will lock up because the driver code goes into an infinite loop to prevent overwriting random parts of memory.
Yup, I heard about this one. However, I assume the 90 byte limit applies to payload only, right?

Klaus.

User avatar
klauskinski
 
Posts: 6
Joined: Tue Jul 01, 2014 5:36 pm

Re: Fragmentation with Adafruit's CC3000 library

Post by klauskinski »

I tried as suggested... but it doesn't compile... Gee, documentation would be so helpful...
Code:

Code: Select all

  Adafruit_CC3000_Client client = cc3000.connectTCP(ip, SERVER_PORT);
  if (client.connected()) {
    client.fastrprintln(reading);
  }
  else {
    Serial.println(F("Error sending measurement!"));
  }
Error:
error: no matching function for call to 'Adafruit_CC3000_Client::fastrprintln(String&)'

User avatar
tdicola
 
Posts: 1074
Joined: Thu Oct 17, 2013 9:11 pm

Re: Fragmentation with Adafruit's CC3000 library

Post by tdicola »

The examples included in the library are the best documentation of the usage. The library is ultimately a wrapper around the lower level CC3000 host driver functions from TI which are documented here: http://software-dl.ti.com/ecs/simplelin ... index.html

Yep the 90 byte limit is for the payload of a single fastrprint/write call. In reality the buffer is a bit larger (~130 characters) but some of that space is used up with headers talking to the CC3000 so you're left with 90 bytes of payload data.

The error you see is from no fastrprint overload being available that takes in the Arduino String class as a parameter. The String class can be problematic because it dynamically allocates space under the covers, so if you do a lot of string manipulation (like concatenating strings together) in a loop you can quickly fragment the heap and run out of memory (there's only about ~1k of memory free on a normal Arduino after the buffers, etc. are taken). Most folks end up pulling out String class usage and switching to C strings/character arrays with the typical string.h functions. However with Arduino 1.05+ it looks like the String class lets you get at the string as a c string using the c_str() function, so try switching to call fastrprintlin like: client.fastrprint(reading.c_str()).

User avatar
klauskinski
 
Posts: 6
Joined: Tue Jul 01, 2014 5:36 pm

Re: Fragmentation with Adafruit's CC3000 library

Post by klauskinski »

Super thanks! Very helpful. I'll try this. Man learns every day something new..........

Klaus

User avatar
klauskinski
 
Posts: 6
Joined: Tue Jul 01, 2014 5:36 pm

Re: Fragmentation with Adafruit's CC3000 library

Post by klauskinski »

BRAVO!!! The sketch compiled. Thank you very much to get me through this hurdle. I still have to test the functionality, but at least I have a workable code now.

User avatar
klauskinski
 
Posts: 6
Joined: Tue Jul 01, 2014 5:36 pm

Re: Fragmentation with Adafruit's CC3000 library

Post by klauskinski »

And this works. Super. Thanks for helping me out.

Klaus.

User avatar
tdicola
 
Posts: 1074
Joined: Thu Oct 17, 2013 9:11 pm

Re: Fragmentation with Adafruit's CC3000 library

Post by tdicola »

Awesome, glad it's working for you now.

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”