Moving Forward with Arduino – Chapter 16 – Ethernet

This is part of a series originally titled “Getting Started with Arduino!” by John Boxall – A tutorial on the Arduino universe. The first chapter is here, the complete series is detailed here.

Welcome back fellow arduidans!

In this instalment we will introduce and examine the use of Ethernet networking with Arduino systems.

Please allow me to make an assumption that you have a basic understanding of computer networking, such as the knowledge of how to connect computers to a hub/router with RJ45 cables, what an IP and MAC address is, and so on. However if you have questions, as always – don’t hesitate to ask. Furthermore, here is a good quick rundown about Ethernet – on Wikipedia of all places.

First of all, you will need an ethernet shield. There are a few on the market, such as the original version by the Arduino team, others from the usual retailers such as adafruitLittle Bird ElectronicsSparkfun and so on. Readers of my articles will know my preference is for the Australian-designed Freetronics line of hardware, so I will be using their shield. Plus it also has some interesting power-over-ethernet features which we will also consider later on. Now, let’s get started!This is an ethernet shield on top of a TwentyTen board:

Notice that the digital pins ten to thirteen (the SPI) are highlighted on the shield – they are used exclusively by the ethernet shield, so you cannot (for now) use them for other i/o operations. Plus all of that lovely space for circuitry…

First of all, let’s do something quick and easy to check that all is functional. If you are using Arduino software version 19 (released this month) you might have some issues with the sketches, so for now we will be using version 18. You can have both versions installed at the same time, and they are downloadable fromhere. Open the Arduino software, and select File > Examples > Ethernet > Webserver. This loads a simple sketch which will display data gathered from the analogue inputs on a web browser. However don’t upload it yet, it needs a slight modification.

You need to specify the IP address of the ethernet shield – which is done inside the sketch. This is simple, go to the line:

byte ip[] = { 10, 0, 0, 177 };

The 10.0.0.177 is the IP address, so you can alter it to match your own setup. For example, in my home the router’s IP address is 192.168.0.1, the printer is 192.168.0.50 and all PCs are below …50. So I will set my shield IP to 192.168.0.77 by altering the byteip[] array to:

byte ip[] = { 192, 168, 0, 77};

You also have the opportunity to change your MAC address. Each piece of networking equipment has a unique serial number to identify itself over a network, and this is normall hard-programmed into the equipments’ firmware. However with Arduino we can define the MAC address ourselves. If you are running more than one ethernet shield on your network, ensure they have different MAC addresses by altering the hexadecimal values in the line:

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

However if you only have one shield just leave it be. There may be the very, very, statistically rare chance of having a MAC address the same as your existing hardware, so that would be another time to change it. Once you have made your alterations, upload the sketch to your Arduino or compatible board. If you haven’t already, disconnect the power and add your ethernet shield.

Now, connect the shield to your router or hub with an RJ45 cable, and the Arduino board to the power via USB or external power supply. Then return to your computer, and using your web browser, enter your ethernet shield’s IP address into the URL bar. The web browser will query the ethernet shield, which will return the values from the analogue ports on the Arduino board, as such:

As there isn’t anything plugged into the analog inputs, their value will change constantly. Press F5 or “refresh” to see the new values. Neat – your Arduino is now serving data over a network. It is quite motivating to see it actually work. But now that we can see the ethernet shield is working we can move on to something more useful. Let’s dissect the previous example in a simple way, and see how we can distribute and display more interesting data over the network. For reference, all of the ethernet-related functions are handled by the ethernet Arduino library. If you examine the previous sketch we just used, the section that will be of interest is:

// output the value of each analog input pin for (int i = 0; i < 6; i++) { client.print("analog input "); client.print(i); client.print(" is "); client.print(analogRead(i)); client.println("<br />"); }

Hopefully this section of the sketch should be familiar – remember how we have used serial.print(); in the past when sending data to the serial monitor box? Well now we can do the same thing, but sending data from our ethernet shield back to a web browser – on other words, a very basic type of web page. However there is something you may or may not want to  learn in order to format the output in a readable format – HTML code. I am not a website developer (which explains why tronixstuff is hosted by WordPress!) so will not delve into HTML too much. However if you wish to serve up nicely formatted web pages with your Arduino and so on, here would be a good start. However in the interests of simplicity, the following two lines will be the most useful:

client.print(" is ");

Client.print (); allows us to send text or data back to the web page. It works in the same way as serial.print(), so nothing new there. You can also specify thedata type in the same way as with serial.print(). Naturally you can also use it to send data back as well. The other useful line is:

client.println("<br />");

this sends the HTML code back to the web browser telling it to start a new line. The part that actually causes the carriage return/new line is the <br /> which is an HTML code (or “tag”) for a new line. So if you are creating more elaborate web page displays, you can just insert other HTML tags in the client.print();statement.

Note that the sketch will only send the data when it has been requested, i.e. received a request from the web browser.

Example 16.1

So let’s put our new knowledge into action with some simple sensor hardware – measuring temperature and pseudo-light levels. In chapter fourteen we did this and sent the results over the air using XBee wireless modules. Now we shall make that data available to a web browser instead.

We will need:

Here is the schematic for the circuit: (click to enlarge)

and in real life. If you were to construct a permanent application, the Freetronics shield is great as you have all that prototyping space:

and our sketch – Example 16.1.pdf.

Finally, the sketch in action, on the desktop PC:

and on a phone via my internal wireless access point (the screen is a little fuzzy due to the adhesive screen protector):

Now you can see how easy it is to send data from your Arduino via an ethernet network to a web browser. But that is only to a local web browser. What if I wanted to read that data using my phone whilst trundling along on the Westlander, or from an internet cafe in downtown Vientiane? It can be done, but is a little bit tricky for the uninitiated – so let’s get initiated!

You will need a static IP address – that is, the IP address your internet service provider assigns to your connection needs to stay the same. If you don’t have a static IP, as long as you leave your modem/router permanently swiched on your IP shouldn’t change.

However, if your internet service provider cannot offer you a static IP at all, you can still move forward with the project by using an organisation that offers a Dynamic DNS. These organisations offer you your own static IP hostname (e.g. mojo.monkeynuts.com) instead of a number, keep track of your changing IP address and linking it to the new hostname. From what I can gather, your modem needs to support (have an in-built client for…) these DDNS services. As an example, two companies are No-IP and DynDNS.com. Please note that I haven’t used those two***, they are just offered as examples.

Now, to find your IP address, usually this can be found by logging into your router’s administration page.

If you happen to be in Australia and have an iinet BoB, we have the same hardware! Anyhow, for example, if I enter 192.168.0.1 in a web browser, and after entering my modem administration password, the following screen is presented:

What you are looking for is your WAN IP address, as artistically circled above. To keep the pranksters away, I have blacked out some of my address. The next thing to do is turn on port-forwarding. This tells the router where to redirect incoming requests from the outside world. When the modem receives such a request, we want to send that request to the port number of our ethernet shield. Using the Server server(80); function in our sketch has set the port number to 80. Each modem’s configuration screen will look different, but as an example here is one:

So you can see from the line number one, the inbound port numbers have been set to 80, and the IP address of the ethernet shield has been set to 192.168.0.77 – the same as in the sketch. After saving the settings, we’re all set. The external address of my ethernet shield will be the WAN:80, e.g.  213.123.456.128:80 into the browser of a web device will contact the lonely Ethernet hardware back home. Furthermore, you may need to alter your modem’sfirewall settings, to allow the port 80 to be “open” to incoming requests. Please check your modem documentation for more information on how to do this.

Now from basically any internet-connected device in the free world, I can enter my WAN and port number into the URL field and receive the results. For example, from a phone when it is connected to the Internet via 3.5G mobile data:

How neat is that? The web page, not the phone. Well, the phone is pretty awesome too.

OK, it’s just the temperature – but with your other Arduino knowledge from our tutorials and elsewhere – you can wire up all sorts of sensors, poll them from your Arduino and use the ethernet shield and an internet connection to access that data from anywhere. Here are some applications that spring to mind, all can be made possible with details from our previous tutorials:

  • Sensitive temperature monitoring (e.g. a smoke house, tropical fish tank, chemical storage room, and so on);
  • “Have the children come home from school?” – children must swipe their RFID tag when they arrive home. Arduino stores time and tag number, which can be converted into display data for web output;
  • For single room-mates – perhaps a remote, high-tech version of a necktie on a doorknob… when the “busy” flatmate arrives home, they turn a switch which is read by the Arduino, and is then polled by the sketch – the other flatmates can poll from their phone before coming home;
  • Using reed switch/magnet pairs, you could monitor whether important doors or windows (etc.) were open or closed.
  • A small RFID could be placed on the collar of your pet – two RFID readers on each side of a cat/dog flap door. Using simple logic the Arduino could calculate if the pet was inside or outside, and the last time the pet went through the door.

Competition! Email your suggestion(s) for Ethernet+Arduino use to: competition at tronixstuff dot com before the 1st October 2010. Three judges will review and choose a winner to receive a mystery prize. Please note that your suggestion will be made public, so if it’s a secret, don’t enter!

The possibilities are only limited by your imagination or requirements.

Now speaking of requirements, one of those is power to your Arduino or compatible board. Naturally the Ethernet shield receives its power from the host board, but you still need to power the host board. This can be a royal pain, as you need to run two cables, one for power and the other for Ethernet. And as fate would usually have it, there isn’t a power point near your hardware to make using a plug pack/wall wart easy. In the past, you may have spliced open your network cable, and soldered up your power supply to the required pairs, then use some heat shrink to hold the whole mess back together again.

However, our friends at Freetronics have come up with a simple yet practical solution to this – their four channel power-over-Ethernet midspan injector:

Using it is very easy. First you will need to set the jumpers on your PoE-compatible Ethernet shield so it feeds the power from the Ethernet line to the host board:

Then, run an Ethernet cable from your router/hub/switch port to the injector, and another Ethernet lead out to your Ethernet shield that supports power-over-Ethernet. Then, plug in your power supply (plug pack) to the injector’s black power socket:

You can have an AC or DC supply, as the injector has a built-in bridge rectifier and smoothing circuitry. For Arduino use, your power supply must output between seven and twelve volts! Any more and your Arduino host board will get very cranky, as you will be outside the limits of the on-board voltage regulator. Please note that the injector does not reduce voltage, so if you insert a 20 volt AC power supply, the injector will happily send 20V DC down the Ethernet cable.

Note that if you use a midspan injector, you don’t run every network device through it – only the devices that require power. So you wouldn’t run standard Ethernet devices such as your printers or PCs through it. E.g.:

However, if you do need to send a voltage over 12 volts DC and below 24 volts DC, there is a tiny regulator board available to solder onto your PoE Ethernet shield, as such:

It converts the input 14 to 24 volts DC that comes in from the midspan power injector to a nice, smoothed 12 volts DC which your host board will be very happy with. The regulator is a 7812, so you can draw up to one amp through it. This should be enough for your host board, Ethernet shield, and even another shield or two depending on your requirements.

Thus concludes our introduction to Arduino and Ethernet. I hope you enjoyed reading and learning as much as I did learning about it before writing the article. If you have any comments at all please leave a comment (below). In the coming chapters we will look at two-way interaction over the internet.

We also have a  dedicated Google Group to answer your questions, discuss the projects and related items on the website – please sign up, it’s free and we can all learn something. If you would like to showcase your work from this article, email a picture or a link to john at tronixstuff dot com. You might even win a prize!

So have fun, stay safe and see you soon for our next instalment, hopefully by the 25th of September 2010.

High resolution photos from this article are available in flickr.

Please subscribe (see the top-right of this page) to receive notifications of new articles.

Thank you to Freetronics for technical and other assistance for this article.

Don’t forget to check out the range of gear at Little Bird Electronics

*** For the curious, I really do make and experiment with everything discussed on my blog – however if not, I will say so.

Advertisements
By John Boxall

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s