t r o n i x s t u f f

fun and learning with electronics

Moving Forward with Arduino – Chapter 16 – Ethernet

Please note that the tutorials are not currently compatible with Arduino IDE v1.0. Please continue to use v22 or v23 until further notice.

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, and others from the usual retailer. 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 need to also have the line:

#include <SPI.h>

Thanks to Scouris for that tip.

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.

Update: Have received some feedback from those people with different network configurations than the normal hub/router setup. If you are going to plug your Ethernet shield directly into the network socket on a computer which is using wi-fi to connect to the router – make sure you are using a crossover cable between the shield and the PC. Some Apple computers don’t need one, but check first yourself. Furthermore, make sure you have either turned off MAC address filtering on your router, or added your Ethernet shield’s MAC to the accepted list.

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.com) 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 the data 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:

  • Arduino Uno or compatible board
  • Ethernet shield
  • Analog Devices TMP36 temperature sensor
  • 10 k ohm resistor
  • light-dependent resistor/photocell

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.

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’s firewall 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.

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.

For a detailed tutorial about power over Ethernet, review this tutorial.

Have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column, or join our Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

September 12, 2010 - Posted by | arduino, education, ethernet, learning electronics, microcontrollers | , , , , , , , , , , , , , , , , , , , , ,

27 Comments »

  1. Great article, John!
    When I tried to verify a sketch using the Ethernet Shield in version 0019, I found I needed to include the Spi.h file as well as the Ethernet.h for the sketch to get accepted. Function names, etc, all seemed to have no problem.

    Comment by scouris | September 12, 2010 | Reply

    • Thank you – and you got it working under v19. Nice one! I will try your suggestion now.
      cheers
      john

      Comment by John Boxall | September 12, 2010 | Reply

  2. Great article as alway.

    After I read through your series. I must buy Arduino and starting mod it now, looks fun!

    Comment by Olanor | September 15, 2010 | Reply

    • Thanks Olanor.
      Yes, you must get an Arduino board and start making things!
      cheers
      john

      Comment by John Boxall | September 15, 2010 | Reply

  3. I’ve been messing with a new Freetronics EtherTen (my first Arduino) and found that the network does not connect until the board is powered via the DC jack; when powered by USB only the NIC does not connect (no link lights and no data flow). Now that I figured that out all the example sketches work great (using version 022). Cheers for the guides; great for noobs like me.

    Comment by BlueAvatar | May 12, 2011 | Reply

    • Hello
      Thanks for your feedback. Wow – you’re quick getting an EtherTen. Nice board! I haven’t got mine yet.
      That’s odd, if an Arduino and ethernet shield can run from USB I don’t know why the EtherTen can’t run from USB. You should ask support@freetronics.com and see what they say.
      Cheers
      John

      Comment by John Boxall | May 12, 2011 | Reply

      • I can also confirm the power issue. Took me a while to work it out, I couldn’t get anything happening on the NIC until it was externally powered. I didn’t even know there were link and activity lights on the EtherTen! Freetronics has confirmed this and claim it’s a weak USB source. You can read more about it here – http://www.freetronics.com/pages/usb-power-and-reset

        Comment by Ian Triggs | June 13, 2011

  4. Is it possible to control an Arduino via this? For example, can i send parameters to the Arduino via this web interface using a textbox and button? Or maybe even just have it listen on a specific tcp port and accept commands that way?

    Comment by Peter Gooch | June 12, 2011 | Reply

    • hi Peter
      Yes it is possible, but I haven’t researched doing so yet. Most likely in July I will have an article about that if all goes well.
      cheers
      john

      Comment by John Boxall | June 15, 2011 | Reply

  5. Wow, I know this is done and dusted a fair while ago but I am trying to get the server working remotely from my galaxy s.
    I can access it locally from a browser but not outside of my home network.
    I turned on port forwarding for the shield. I manually added it to attached devices on my router page but it doesn’t show up on the network in win7. I made a rule in windows firewall each to allow incoming and outgoing for the shield.
    It is plugged into my netgear Rp614v3 router. The light for the port is amber whereas my 2 pcs on other ports lightup green. I don’t know what the significance of that is. I am at a loss to understand why it refuses to work.

    Comment by Mike | December 5, 2011 | Reply

  6. I managed to disconnect my brain from my hand and deleted My Arduino 0023 IDE and now after reinstalling I cannot manage to get the twitter library to run. I get this error when it fails to compile:

    In file included from SimplePost.cpp:3:
    /home/arduino/Desktop/arduino-0023/libraries/Twitter/Twitter.h:23:25: error: EthernetDNS.h: No such file or directory

    for what it means…(the twitter.h does turn orange)
    I would like to think that i am smarter than the ONU but I am beginning to wonder….

    Comment by shaddoracer | January 4, 2012 | Reply

    • that’s odd. You can always download the EthernetDNS library again, http://gkaindl.com/software/arduino-ethernet
      john

      Comment by John Boxall | January 4, 2012 | Reply

      • still no luck… this all started when i went from ubuntu 10.10 to 11.04 besides losing several gigs of pics and programs this really has me stumped. since the data was lost forever I tried wiping the hard drive and starting over with a fresh install all to no avail..

        Comment by shaddoracer | January 5, 2012

      • Are you reinstalling v23 or Arduino 1.0? (My sketch doesn’t work for v1.0)

        Comment by John Boxall | January 6, 2012

  7. I am running v23. now, on ubuntu 11.04. I spent some time on it last night and found that I had made some progress. But unwittingly got a token for the wrong twitter account. Once I figured that out, I knew I had at least hit upon the correct setting once. So I went back got the right token and then looked at what I had previously done and managed to get the twitter part working. I am no programmer ( i got this to help me learn if only for my own satisfaction). With the help of this site and people like you… I might actually accomplish that goal. In time I might even be able to contribute, Many thanks!! I am sure that you will be answering more questions as I delve further into this mystery machine. that is,… if you dont mind ;)

    Comment by shaddoracer | January 6, 2012 | Reply

    • Glad you are making process. Have fun and enjoy yourself.
      cheers
      john

      Comment by John Boxall | January 6, 2012 | Reply

  8. hi
    i am having my internet connected through a “pendrive”and i can also connect internet through my universities wireless connection but i am not having a normal telephone line ‘net’ or any modem.. or anything else.so, can i use the ethernet shield with such kind.. as in the beginning you have mentioned that shields are connected to the modem via RJ45 cable so what could be a solution to that.are there any ports available on the laptops through which we can connect the RJ45 cable while working with such kind of net connections.
    i would here also mention that i am having a dell laptop-[INSPIRON N4010] that’s having wired network connection port AS MENTIONED on page 7 of the inspiron setup guide.
    i am eagerly waiting for your reply. hope you understand the problem.

    Comment by anas_ali | February 5, 2012 | Reply

  9. wow ,
    very useful guide.thank you for doing this.

    do you know if there is a way to make the page live? I mean is it possible to make is refresh (by it self) every 1 sec?
    this way you can know what is going on at real-time ?
    and if not – can you tell me how\what to change in the code in order to make it like a log?
    every change will be written?

    Thank you again!

    Comment by Dan | April 29, 2012 | Reply

    • You can use code at the web-browser side of things to auto-refresh. However I don’t have a clue about HTML or writing Java apps. Perhaps ask about in a web-programming forum or somesuch.

      Comment by John Boxall | April 29, 2012 | Reply

  10. Thank you!

    Comment by Dan | April 29, 2012 | Reply

    • Sorry about the mess. OK – this is what my message should have said !!

      Place tag below in <head> tag – this gives a 5 second refresh of same page

      <meta http-equiv=”refresh” content=”5″>

      Comment by tdaus | May 5, 2012 | Reply

  11. Hi John,
    Read your great guide above.

    I have 2 sets of Arduino UNO + Ethernet Shield. One is uploaded with WebServer Example and the other with WebClient Example from the standard sketch examples. Both are tested OK separately. When I try the webclient connect to my webserver with appropriate ip, I get connection failed. Is there anything that has to be introduced in order to make the connection successful?

    Many thanks.

    Comment by Hoyt | May 5, 2012 | Reply

    • Solution for this question can be find in [tronixstuff: 1876]

      Comment by Hoyt | May 26, 2012 | Reply

  12. hi
    ( my comment didn’t showed up in the comment area, therefore writing all over again )
    i having problem with poE module .
    what is the purpose of adding poE module with arduino ethernet shield and what will be the consequences in the absence of poE module.

    Comment by anas_ali | May 16, 2012 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 2,588 other followers