News: Solving the Arduino Ethernet Web Server Page Linking Problem

What’s happening at Starting Electronics? (14 June 2013)

I have been working on the sketch for part 10 of the Arduino Ethernet shield web server tutorial that many people could not get to work. The sketch allows the Arduino with Ethernet shield to host two web pages on the SD card. Each web page has a link to the other page and when clicked, should load the other page in the web browser.

The Problem

Firstly, there was no problem when I developed the code using Arduino software version 1.0.3, an Arduino Uno and Arduino Ethernet shield – everything worked. However, a number of people could not get the code to work as reported in the blog entry for the tutorial. I did not have any other Arduino hardware to test the code further, so could not get the problem to come up and then debug it.

New Hardware

Arduino Ethernet Board

After getting a new Arduino Ethernet board, and loading the sketch from part 10, the problems started appearing. The first web page loaded from the Arduino web server, but a blank page appeared in the web browser when the link to the second page was clicked. Compiling the sketch with Arduino software version 1.0.5 and trying to run the web server on the old hardware now resulted in the first web page not loading at all. Changing back to version 1.0.3 software solved the problem on the old hardware, but not on the new Arduino Ethernet board.

Arduino Mega 2560 Board

When testing the sketch on a new Arduino Mega 2560 with the original Ethernet shield the sketch worked with no problems.

Memory Sizes

It had been suggested by someone in the comments on the blog that the problem was with too little SRAM on the Uno board – changing to the Arduino Mega proved this. The SRAM sizes for the three boards that I have are as follows:

  • Arduino Uno – 2k Bytes SRAM
  • Arduino Ethernet – 2k Bytes SRAM
  • Arduino Mega 2560 – 8k Bytes SRAM

The Solution to the Problem

I had already suspected that using the String class in the sketch was the source of the problem and had mentioned a way of reducing the number of bytes that the String class object in the sketch was using in part 16 of the tutorial series. Restricting the number of bytes in the web page link example, however, made no difference.

The String Class Object

The String object was used to store the HTTP request from the web browser. The stored request could then be analysed later in the sketch to see which page the web browser was requesting. Restricting the String object to storing only the first 30 bytes of the HTTP request did not solve the problem. The only solution was to eliminate the use of the String class altogether.

My thinking in using the String class was that the sketch was small, so there should be no memory problems and it worked (at least on my hardware) – no need to write extra functions and make the sketch more complicated. The String class also had a method that made it easy to find out which web page was requested by searching in the stored HTTP request String for the web page name.

New String Functions

In the new sketch, the String class object was replaced by a char buffer with limited size. Two new functions were written to operate on the new buffer. One to clear the buffer and the other to search for a particular string in the buffer. This eliminated the need to use the String class at all. The function that searches for the string in the HTTP request buffer is used to find which web page the browser is requesting.

To see the new sketch, go to part 10 of the Arduino Ethernet web server tutorial: Arduino SD Card Web Server – Linking Pages.

IP Addresses

A few weeks ago, my ADSL router failed and I had to replace it. The old router used the address range 10.0.0.x and the new router now uses the address range 192.168.0.x. The new sketch now uses the new address range, so be sure to change the IP address in the sketch to suit your network. If the sketches were all working on your system at 10.0.0.20, but the updated sketches stop working, it is probably because the updated sketches are using the new IP address 192.168.0.20.

I will continue testing the remainder of the Arduino Ethernet web server tutorial using the Arduino Ethernet board and will eliminate the use of the String object from all sketches, which will hopefully make the code a lot more reliable and able to work across all the Arduino hardware.