Stefano Tommesani

  • Increase font size
  • Default font size
  • Decrease font size
Home Web Improving web speed with Arduino

Improving web speed with Arduino


The Flash library by Mikal Hart is a wonderful piece of software for improving simple web server apps built on the Arduino: the main problem when designing such apps is that the limited number of RAM available forces the designer either to use an SD card to store the files that are part of the web site, and/or store some HTML fragments in the PROGMEM area, and copy them back to normal RAM to modify and include them in the server HTML packets. The Flash library greatly simplifies the second technique, as the following code fragment highlights:

FLASH_STRING(WebPageHeader, "HTML source code");
Client& client = web_server.get_client();

The FLASH_STRING macro declares a string contained in PROGMEM, so that it does not decrease the available RAM. Streaming that string is as easy as calling the print method and passing the Client connection of the web server as the parameter. Using this technique, dynamic generation of web pages includes both static parts stored in PROGMEM as FLASH_STRINGs (for example, page headers and footers, top menus, etc.) and dynamic parts that are generated by code (for example, tables containing data values coming from attached devices).

The only downside of this technique is that it is slow! Every HTML page generates a lot of network traffic, and a look at how FLASH_STRINGs are printed shows why (the following is an excerpt from libraries\Flash\Flash.cpp):

 void _FLASH_STRING::print(Print &stream) const
for (size_t i=0; ; ++i)
char c = (*this)[i];
if (!c) break;
stream.print(c); // print in char form

Each string contained in a FLASH_STRING object is written one character at a time to the network connection!

The result is a lot of HTML overhead, as we can see with WireShark:


Given the overhead of all these transactions just to write a simple HTML page through a connection, a better solution is using a small amount of RAM to group characters and send larger packets of data, as the following print method does:

void _FLASH_STRING::print(Print &stream) const
char CharBuffer[CHAR_BUFFER_SIZE + 1];
char CharBufferIndex = 0;

for (size_t i = 0; ; ++i)
CharBuffer[CharBufferIndex] = (*this)[i];
if (!CharBuffer[CharBufferIndex])
if (CharBufferIndex == CHAR_BUFFER_SIZE)
CharBuffer[CharBufferIndex] = 0;  //< terminate char buffer
CharBufferIndex = 0;
if (CharBufferIndex > 0)
CharBuffer[CharBufferIndex] = 0;

This improved method groups chars from the FLASH_STRING into a CHAR_BUFFER_SIZE-long temporary buffer, and then properly terminates the buffer and sends it through the network connection. The difference in performance, even with a small buffer size, is clear, and a new run of WireShark shows that a lot less packets are needed to send the generated HTML page. Defining a proper value for CHAR_BUFFER_SIZE depends on the RAM requirements of the sketch, the default value of 16 looks like a minimum value that should be a good compromise between network speed and reduced RAM usage, but if the RAM is not so heavily used, larger buffer sizes such as 32 or 64 bytes bring even more improvement to the observed speed.


Last Updated on Thursday, 21 March 2013 23:18  

Latest Articles

Easily upload videos of security cameras to YouTube
In this example, we will import video from a Yi security camera into YouTube. The same process, with eventual adjustment to the naming of directories in the SD card used by the camera to record videos, will also apply to other
A software to stand out 27 January 2018, 14.35 Web
A software to stand out
Standing out of the pack starts by being visible, and being noticed by the right group of professionals. No matter how good your profile is, it is lost in a sea of similar profiles, so you need to show up and start attracting
Web page scraping, the easy way 07 January 2018, 00.46 Web
Web page scraping, the easy way
There are many ways to extract data elements from web pages, almost all of them prettier and cooler than the method proposed here, but as we are in an hurry, let's get that data quickly, ok? Suppose we have to extract the
Scraping dynamic page content 06 January 2018, 23.57 Web
Scraping dynamic page content
One of the most common roadblocks when scraping the content of web sites is getting the full contents of the page, including JS-generated data elements (probably, the ones you are looking for). So, when using CEFSharp to scrape
Unit-testing file I/O 26 November 2017, 12.09 Testing
Unit-testing file I/O
Two good news: file I/O is unit-testable, and it is surprisingly easy to do. Let's see how it works! A software no-one asked for First, we need a piece of software that deals with files and that has to be unit-tested. The