Network your Arduino

uno_16x9

The basic Arduino Uno board (or its Arduino Duemilanove predecessor) is incredibly versatile, thanks to the double row of pin headers on the long edges commonly known as the ‘shield header’.

We’ve already look at how to plug in an LCD display and a motor drive shield, but if you’re interested in making ‘Internet of Things’ (IoT) projects, you can even plug in an Ethernet shield.

Because the Arduino Uno’s microcontroller chip, the Atmel ATMEGA328P, clocks along at a sedate 16MHz and only delivers 8-bit processing, you are limited to relatively low transmit speeds, typically less than 500KB per second.

But for reading sensors and delivering remote information, it’s a great way to hook up your Arduino board and learn about networking at the same time.

W5100 Ethernet Shield

w5100The W5100 Ethernet Shield uses a low-cost Wiznet W5100 controller chip that delivers a standard Ethernet port, plus the bonus of MicroSD card storage on the other end. This enables you to not only transmit data but also store it.

The card reader handles up to 32GB MicroSD cards and most relevant Arduino code libraries available will handle FAT32 filesystems comfortably, giving you file sizes up to 4GB.

You’ll find the shield online via eBay sellers starting from around $8.50.

Serial Peripheral Interface

unoThe Arduino Uno board has 20 digital I/O pins you can ‘bit bang’ (send high or low) to transmit data, but arguably it’s most useful connection port is the Serial Peripheral Interface or SPI.

Many electronic devices support SPI connection and here, the interface has a general speed of 4MHz, which isn’t bad given the ATMEGA328P only clocks at 16MHz. It’s also the interface used by the W5100 Ethernet Shield, which is why it has the six-pin header on the underside of the board.

Those six pins form the Arduino’s main SPI connection (although there’s a secondary option on pins D11, D12 and D13).

Ethernet library

libAs with most external shields, the Ethernet shield needs two code libraries in order to provide the programming instructions to make your shield do something – in this case, the Ethernet library and the SPI library.

Our project in this article is a very simple web server that lets you switch on and off an LED connected to the Arduino. You’ll find the source code, plus the SPI code library on our website here.

Download the zip file, unzip it and copy the contents of the /libraries folder to the /libraries folder of your Arduino IDE. The Ethernet library comes with the Arduino IDE – grab the latest v1.6.4 release from arduino.cc/download.

How the source code works

projThe LED in our project connects in series with a 330-ohm resistor between I/O pins D5 and D4 (acting as ground) – not terribly interesting, but enough to have our web server do something that shows how control via a web server can work.

Load the WebLED source code into the Arduino IDE and after the comments and include statements that load up the code libraries, we get to the initialization code.

An Ethernet port needs two pieces of identifying info – a Media Access Control (MAC) address and an IP address. The MAC address identifies your Ethernet shield, while the IP address identifies your location on the network – think of it as your name and address on your snail mail.

It means the first thing the source code requires is to set these MAC and IP addresses – you can make them up, but they must be valid addresses. We’ve chosen the default MAC address used for Ethernet shield and since its 48-bits long, it’s unlikely to run into anything else with that address on your network – this sits in the mac[] byte array.

The IP address, however, has to fit your local network and uses the standard 32-bit IPv4 addressing standard, where home networks usually start on ‘192.168’.

We’ve selected ‘192.168.0.200’ here – again, it’s unlikely you’ll have anything else on that particular address, but just check – if you’re router is set to 192.168.1.xxx, you’ll need to change your IPAddress code appropriately (e.g. 192.168.1.200).

In the setup() method, we declare I/O pins D4 and D5 as outputs and start the Ethernet connection with the ‘begin’ statement and the MAC and IP addresses as parameters.

Next, we launch the server with another ‘begin’ statement.

Main loop

ledonOnce you have the project built, powered up and connected to your router, head to your PC, smartphone or tablet, fire up a web browser and enter the address you chose for the IPAddress variable (‘192.168.0.200’ in our example).

What happens now is the web browser sends out an HTTP request for data. Your router recognises that address as on the local network (your home network) and hits the Arduino board.

The main loop of our source code at this point, is waiting for a client. When a client connects, the code then sends back a very basic HTTP webpage using the ‘client.println’ statements. Now here’s the clever bit – remember that we’re using the Arduino to control the LED over the web.

First thing it does is check the digital pin D5 to see if the LED is on (high) or not (low) using the ‘if (digitalRead(5))’ command. If D5 is high (LED is on), it sends data to the web browser that the LED is on and vice versa.

ledoffNext, it then creates a simple HTTP form with two radio buttons and essentially waits for the user to make a choice.

Once the choice is made and the Submit button pressed, HTTP code comes back from the user’s web browser into the Ethernet buffer. We check that buffer to see if the return code includes a ‘/?LED=on’ or ‘/?LED=off’ entry and write the D5 pin output appropriately.

Why is this clever? Because the Arduino is able to make the leap between web pages and discrete electronics on its own – that’s seriously cool stuff.

Finally, the ‘client.stop()’ code ensures that as soon as any interaction between the server and web client is complete, we let go of that connection to ensure we play good ‘network neighbours’ and don’t hog the network.

Internet of Things

Sure, we’re making baby steps here, but this can be the start of building your own Internet of Things projects.

It’s also why the whole IoT concept is so popular – the ability to control devices over the web (especially when that control comes from a smartphone or tablet) opens up a world of opportunities only limited by your imagination.

  • The basic Arduino Uno board (or its Arduino Duemilanove
    predecessor) is extremely versatile,As with most external shields, the
    Ethernet shield requirements two code libraries so as to deliver the programming directions to create your shield do something . the ability to control devices over the net (specifically when that manage comes from a smartphone or tablet) opens up a
    globe of possibilities only limited by your imagination.