Pages: [1]   Go Down
Author Topic: Is it possible to reduce a sketch size be modifying libraries?  (Read 1277 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

Does anyone have experience modifying libraries (specifically HttpClient) in order to reduce the size of a sketch?

I'm writing a sketch and I want my arduino uno (+plus ethernet/SD card shield) to be able to:
-log data to Cosm
-log data to SD card
-read information from a DHT11 temperature/humidity sensor
-get the current UTC time from an internet server

I've found example codes that do all these things individually, and I verified that in individual sketches each component works. However, when I combine everything together in my master code I get an error saying that my sketch is too big (37.4 kb out of 32.3 kb allowed). I have a few ideas on how to reduce the size of my sketch, but with all the libraries I need to include for the above tasks,
#include <dht11.h>
#include <SD.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SPI.h>
#include <HttpClient.h>
#include <Cosm.h>,
I think it might be better for me to tackle it from that end. The biggest seems to be the HttpClient that is required for the Cosm feature to work properly, with HttpClient.h at 20kb and HttpClient.ccp at 17kb (according to windows explorer).

Could I remove parts of those libraries that Cosm doesn't need to make it smaller? Has anyone tried this before or know what parts I could remove? Or is there another way to upload data to Cosm that doesn't require the HttpClient.h library? Or is it possible to store part of the program or library files on the SD card on the board?

SD.ccp is also large, at 15kb. Do both the .h and .ccp file become part of the final sketch size? I would assume not, since otherwise that would be much more than the 37kb my sketch is currently at, unless it gets compressed during compiling..., so which is more important to focus on?

thank you for any advice or tips!

Eli
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 642
Posts: 50362
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The size of a source file has little to do with the size of an object module. The 20kb and 17kb don't add up to create a 37kb object module.

Quote
Could I remove parts of those libraries that Cosm doesn't need to make it smaller?
That's the linker's job, and it is really good at it. You don't call a function, the linker doesn't include it. You do, and it does. The only way to stop the linker from including something is to stop calling it.

Quote
Or is it possible to store part of the program or library files on the SD card on the board?
No.

Quote
Do both the .h and .ccp file become part of the final sketch size?
See above.

Quote
so which is more important to focus on?
The code you control.
Logged

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 48
Posts: 2302
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't bother.

As I understand it, what you are trying to do is done for you when the programme is compiled. 

It is a tempting avenue, particularly as you are probably around 35k at the moment and looking to snatch victory from the jaws of defeat with a little judicious shaving.  Besides, your SD card suggests you may want to add an on-board clock later, which will only make the situation worse.

$25 will see you with a Mega and all your problems are solved.

Logged

Cleveland, TN
Offline Offline
Full Member
***
Karma: 2
Posts: 207
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You might also want to check your data types. If you are using integers where bytes will suffice, you can save a lot of room depending on how many you are using.

Strings are also very expensive. If you are using a lot of fixed strings, it is better to store them on your SD card or stick them in the flash memory instead using the progrmem option.

A quick google on Arduino Strings had a link to someone dealing specifically with an http client app that was causing him grief because of the size of the strings.

Logged

Luck,

Wade

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

Thanks everyone, then I won't bother digging through the libraries.
For now I've left off the SD card parts of the program and that fits, but I may have to go to a MEGA soon anyway.

thanks,
Eli
Logged

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 19
Posts: 364
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

To regain program space on a recent project I dropped the SD card logging as well. Since I had another Arduino around I just turned it, with the SD card shield, into a logger. My project outputs data samples via serial prints and the other Arduino reads them and saves them, to the SD card. It's fairly simple to do.

I also made it so that when you're done logging you can just jumper a pin to ground and it will allow displaying or deleting the file and a few other useful things. Pull the jumper and it's a stand alone data logger.

Logged

Offline Offline
Edison Member
*
Karma: 49
Posts: 1670
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The SD library code is large but its RAM requirement is also large and I presume that that is what you are really running into. As Jimmy60 has pointed out, one way to get around it is to put just the SD part in another Arduino.
Another way would be to replace the SD in the original sketch with an I2C EEPROM and store the data in a raw form. Then write a separate sketch which extracts that data in its verbose form as and when required. You'd just have to figure out what size of EEPROM you would need to hold sufficient data so that the data extraction doesn't have to be performed too often.

Pete
Logged

United Kingdom
Offline Offline
Tesla Member
***
Karma: 227
Posts: 6637
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, you can shrink libraries, by removing from the .cpp file all those functions that are never called and are never called elsewhere in the library either. The linker won't remove those functions, all it will remove is any entire .cpp files in the library that don't contain any code or data that is needed. So if the library has only a single .cpp file, then you get all of it or none of it.

Once you've removed any functions you don't need, you can remove any data declarations that were used only by those functions.

There are a couple of exceptions:

1. Templates functions and classes. For these, the compiler only instantiates what is needed; so there is nothing to be gained by removing them.

2. Functions in derived classes that override virtual functions with the same name and parameters in an ancestor class. In this case, if objects of the derived class are created, then the overriding function can get called even if you think you are calling the version in the base class. The classic case if the write() function in classes such as HardwareSerial that derive from class Print.

Having said that, if you are 5K over the size limit, then it's probably simpler to upgrade to a Mega.
Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Pages: [1]   Go Up
Jump to: