How to Save a File from the Internet to an Arduino

The article on saving a web file to an Arduino shows how to get any file from the Internet using an Arduino and Ethernet shield and save the file to an SD card inserted into the Ethernet shield.

Two sketches are provided in the article — the first sketch saves the incoming HTTP header and the requested file. If a project requires further information from the HTTP header, it can be obtained from the file instead of trying to process the incoming header on-the-fly.

The second sketch in the article strips off the incoming HTTP header and saves only the requested file to the SD card.

Saving a web file to SD card first before accessing data from it allows the data to be randomly accessed in the file instead of trying to find data from the file in the incoming stream of bytes. This is useful for retrieving data from an XML file such as from a live sports or weather feed. Specific data fields can be randomly accessed from the saved file and then used by the Arduino.

Go to the Arduino Web File Article →

Disabling the Ethernet Chip and SD Card on an Arduino Ethernet Shield

Problems such as intermittent hanging of an Arduino sketch can occur when using the Arduino Ethernet shield with an uninitialized SD card plugged into the SD card socket of the shield. It is always best to disable the SD card before initializing the Ethernet chip, or disable the Ethernet chip before initializing the SD card.

The SPI Bus

On an Arduino Ethernet shield, the WIZnet W5100 Ethernet chip and micro SD card socket share the same SPI bus — that is the MISO, MOSI and SCK SPI bus pins. Data sent on the SPI bus will be received by all devices on the bus that are enabled. Only one device should be enabled at a time to prevent this problem.

In the case of the Arduino Ethernet shield, when initializing one device (Ethernet chip or SD card) and then only initializing the other device later, data may unintentionally be sent to the other device if it is not disabled. For example, if initializing the Ethernet chip, but having an SD card on the Ethernet shield, if the SD card is not disabled, data may be sent to the SD card and the SD card may try to respond which will corrupt data on the SPI bus.

Disabling SPI Bus Devices

The solution to this problem is to disable the device not being used. This is done by switching pin 4 of the Arduino high to disable the SD card and switching pin 10 of the Arduino high to disable the Ethernet chip.

Disabling the SD Card

To disable the SD card, use the following code before initializing the Ethernet chip with the Arduino Ethernet libraries:

// disable SD card
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);

Disabling the Ethernet Chip

To disable the Ethernet chip before using the SD card / SD card libraries, use this code:

// disable Ethernet chip
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);

The above code would normally be put in the setup() part of the sketch. The code should work on all Arduino boards that are compatible with the Arduino Ethernet shield, e.g. Uno, MEGA, Due, Leonardo, etc. It will also work on boards that have built-in Ethernet such as the Arduino Ethernet board.

Arduino Ethernet Shield Documentation

SPI Bus Pins on the Arduino Uno and MEGA

The Arduino Ethernet shield information page has the following to say about the SPI bus pins on the Uno and MEGA:

Arduino communicates with both the W5100 and SD card using the SPI bus (through the ICSP header). This is on digital pins 10, 11, 12, and 13 on the Uno and pins 50, 51, and 52 on the Mega. On both boards, pin 10 is used to select the W5100 and pin 4 for the SD card. These pins cannot be used for general I/O. On the Mega, the hardware SS pin, 53, is not used to select either the W5100 or the SD card, but it must be kept as an output or the SPI interface won’t work.

So on the Arduino MEGA, pin 53 is not used as the SPI slave select (SS) pin for either the Ethernet chip or SD card, but must be left as an output for the SPI bus to work.

SD Card and Ethernet Chip Disable Pins

The Ethernet shield information page has the following to say about disabling SPI bus devices on the Ethernet shield:

Note that because the W5100 and SD card share the SPI bus, only one can be active at a time. If you are using both peripherals in your program, this should be taken care of by the corresponding libraries. If you’re not using one of the peripherals in your program, however, you’ll need to explicitly deselect it. To do this with the SD card, set pin 4 as an output and write a high to it. For the W5100, set digital pin 10 as a high output.

It is always best to disable the second peripheral before initializing the first on the Ethernet shield SPI bus.


Using Arduino as a Sports Score Ticker to get Live Cricket Results

An Arduino with Ethernet shield can be configured as an Ethernet client to fetch live sports scores from the Internet. The scores can the be displayed on any display device connected to the Arduino or sent through the serial port.

In the Arduino live cricket score project, the Arduino fetches the live cricket scores from and displays them in the Arduino IDE serial monitor window. It gets the scores by accessing the XML scores feed and then extracting the desired information from the XML file.

Arduino cricket score ticker

Arduino Cricket Score Ticker

This is a very early version of cricket score code and will be improved on in the future. The code is susceptible to breaking if there are any unexpected changes to the XML file, but the code will be made more robust and will be updated to handle and recover from errors in the future.

Go to the Arduino live cricket score project →

Basic Multi-file Arduino Web Server

This basic Arduino web server hosts HTML web pages and various other files such as CSS, JavaScript, text and image files.

Web pages and other resource files are stored on the SD card inserted into the SD card socket on  Arduino Ethernet shield or Arduino Ethernet board.

The code for the web server can be copied from the project’s page which contains a form that allows parameters such as the MAC, IP and port numbers to be changed. After changes have been made to the settings, a button is clicked to update the Arduino web server sketch. The sketch can then be copied and pasted into the Arduino IDE for loading to the Arduino board with Ethernet shield. Some sample HTML files, JavaScript, CSS, text and image files are also available for download and demonstrate the web server capabilities.

This web server is a supplement to the Arduino Ethernet shield web server tutorial and provides a starting place to design your own web server.

Get the Arduino web server code →

How to Control an LED from an Arduino Web Page Button and an External Push Button

The article on how to control an LED with both a button on a web page from an Arduino web server and a physical hardware button includes the circuit diagram, HTML and JavaScript code and the Arduino code.

I wrote the article after being asked a question on how to control an LED from an HTML button on an Arduino hosted web page and a physical button connected to an Arduino pin.

The image below shows the final project.

Arduino LED Control from Web Page and Push Button Switch

Arduino LED Control from Web Page and Push Button Switch

An Arduino and Ethernet shield is used with a web page hosted on the micro SD card inserted into the Ethernet shield.

When the checkbox or button on the web page is clicked, the corresponding LED will switch on or off. When a push button is pressed, the corresponding LED will switch on or off and the state of the LED will be updated on the web page. Ajax is used to update the LED states on the web page so that there is no page refresh flicker.

Pressing the left push button causes the left LED to switch on and the checkbox on the page to get a check mark. Pushing the left push button again switches the LED off and the check is removed from the checkbox on the web page.

The button on the web page will initially display the text OFF as part of the button text. When the right push button is pressed, the LED will switch on and the HTML button text will be updated to display ON. Pressing the right push button again will switch the LED off and update the button text on the web page to display OFF again.

Go to the project article with video, circuit diagram and HTML, JavaScript and Arduino Code →