It’s that time of the week! We’ve been playing around with the Ethernet shield again and this time we’ve come up with an article about creating a webserver from scratch; using the Arduino SD/Ethernet Shield.
This would be ideal for people looking to add a basic web system to their projects.
So, what do we need?
- 1x Arduino
- 1x SD/Ethernet Shield (eBay, Sparkfun, Adafruit etc.)
- 1x Micro-SD Card (or Micro SDHC, same as above, anywhere online)
- Our code!
Before running any code below; run one of the SD Card examples such as CardInfo so that you can verify that the SD Card is fully working. If you are using the Ethernet Shield version you MUST set pin 10 to HIGH (using digitalWrite(10, HIGH); ) in the setup function; if you don’t then the SD will not initialise properly.
What will this project do?
- Serve files from the SD Card via a Web Browser
- Give you a base to easily add web functionality to your projects.
First things first, insert the SD Card into a Card Reader and the card should be formatted to a FAT file system – You can do this easily on any Windows based PC by right clicking the drive in Computer and selecting Format. We have tested this on a Sandisk 2GB SDHC Micro Card – These are extremely cheap from any online retailer.
Let me start by saying that this was based on the example code from Adafruit and modified to add features/functionality.
The first thing you’ll need to do is make sure that the network settings match your network. The following code should be altered to suit your setup:
Also note that the speed of the web server will also depend of the speed and type of the SD Card. The faster the card the faster you can serve files. This is because the code is looking to see if a file exists before serving it to the client. If you are using this in a project where you know the files always exist; you could remove this check to make it faster.
If you are wanting to serve images from the SD Card; you’ll need to output the correct content types or it will just display as raw data. We have left it out of this example to try and get the code as small as possible.
Ideally this code would be suited to run on the MEGA and not the Uno/Leonardo as there isn’t much free space to work with; but it’s certainly doable and seems to work really well.
This should pretty much be it. There isn’t too much for you to modify in the code to get it working, if you have any questions or need any help please feel free to leave a comment or send us a message using our contact page.