Stefano Tommesani

  • Increase font size
  • Default font size
  • Decrease font size
Home Programming 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.


Quote this article on your site

To create link towards this article on your website,
copy and paste the text below in your page.

Preview :

Improving web speed with Arduino
Tuesday, 02 October 2012

Powered by QuoteThis © 2008
Last Updated on Thursday, 21 March 2013 23:18  
View Stefano Tommesani's profile on LinkedIn

Latest Articles

Fixing Git pull errors in SourceTree 10 April 2017, 01.44 Software
Fixing Git pull errors in SourceTree
If you encounter the following error when pulling a repository in SourceTree: VirtualAlloc pointer is null, Win32 error 487 it is due to to the Cygwin system failing to allocate a 5 MB large chunk of memory for its heap at
Castle on the hill of crappy audio quality 19 March 2017, 01.53 Audio
Castle on the hill of crappy audio quality
As the yearly dynamic range day is close (March 31st), let's have a look at one of the biggest audio massacres of the year, Ed Sheeran's "Castle on the hill". First time I heard the song, I thought my headphones just got
Necessary evil: testing private methods 29 January 2017, 21.41 Testing
Necessary evil: testing private methods
Some might say that testing private methods should be avoided because it means not testing the contract, that is the interface implemented by the class, but the internal implementation of the class itself. Still, not all
I am right and you are wrong 28 December 2016, 14.23 Web
I am right and you are wrong
Have you ever convinced anyone that disagreed with you about a deeply held belief? Better yet, have you changed your mind lately on an important topic after discussing with someone else that did not share your point of
How Commercial Insight changes R&D 06 November 2016, 01.21 Web
How Commercial Insight changes R&D
The CEB's Commercial Insight is based on three pillars: Be credible/relevant – Demonstrate an understanding of the customer’s world, substantiating claims with real-world evidence. Be frame-breaking – Disrupt the