How to Use Arduino as a Web Server

Arduino Web Server Featured

When it comes to IoT projects, Arduino isn’t quite a household name, but maybe it should be. While Arduino boards are comprised of fairly modest hardware, they can get a whole lot done. These boards can power everything from weather stations to smart mirrors.

Arduino boards are also surprisingly good candidates for projects you might normally use a Raspberry Pi for. In this case, we’ll be looking at using one as a basic web server. This is a simple example, but it can scale up as well.

Before You Start

You’ll need a few things to get started. Obviously, you’ll need an Arduino board like the Arduino Uno. Then you’ll also need the Ethernet Shield for Arduino. Aside from this, you’ll need an Ethernet cable to connect to your network and a USB cable to power and program the board.

Arduino Web Server Arduino Uno

When it comes to writing and running the code, you’ll be best served by running the Arduino IDE. If you’re running Ubuntu, we have a guide to getting the Arduino IDE up and running.

Preparation

Before you do anything, you’ll want to connect the Ethernet Shield to your Arduino. To do this, simply mount the shield on top of your Arduino board. Now you can connect to your computer via the USB cable.

If you have an SD card you’re using with the Arduino, it can cause potential problems with this sketch. Either remove the SD card or add the following code in the setup() function in the next section:

The Code

There is already plenty of code available to get a basic web server running on the Arduino Ethernet Shield. We’re not going to reinvent the wheel here. Instead, the following code is taken from the Arduino website. Copy it into the Arduino IDE, compile it, and upload it to get your server running.

What the Code Is Doing

In the above code the MAC address is being set manually. The IP address is as well. While the MAC address should work fine on most networks, you may need to change the IP address to fit with the address conventions used on your network.

Arduino Web Server Arduino Ethernet Shield

The setup() function is, well, setting up the Ethernet connection and preparing to listen for incoming connections. It’s also handling any errors that may pop up at runtime.

The real meat of this sketch takes place in the loop() function. This listens for clients initiating connections, responds with header information, then serves HTML code which is rendered by the browser. The code then closes the connection once it has finished serving the data.

Conclusion

We’ve already mentioned what can happen when you have an SD card inserted but aren’t using it. What if you decided to load your web site off of the SD card instead? You can modify the above code to work this way, or you can use it as the basis for a larger project.

This web server can also serve as a small part of a larger IoT project. For an idea as to where you could potentially go from here, take a look at our roundup of some of the best Arduino IoT projects.

Leave a Comment

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.