Communicating Over Networks Using an Ethernet Shield
In the previous section, you learned how to build network applications with
an Arduino by using your PC’s network connection. This approach works
nicely, but it also has a few disadvantages. The biggest problem is that you
need a complete PC, while for many applications the Arduino’s hardware
capabilities would be sufficient. In this section, you’ll learn how to solve this
problem with an Ethernet shield.
Usually, you can’t connect a naked Arduino to a network. Not only are its
hardware capabilities too limited, but also most Arduino boards don’t have
an Ethernet port. That means you can’t plug an Ethernet cable into them,
and to overcome this limitation, you have to use an Ethernet shield. Such
shields come with an Ethernet chip and Ethernet connectors and turn your
Arduino into a networking device immediately. You only have to plug it in.
You can choose from several products (the following figure shows some of
them); they all are good and serve their purpose well. For prototyping, I prefer
the “official” shield,
7
because it comes with sockets for all pins and has a
microSD card slot. Alternatively, you can use the Arduino Ethernet,
8
an
Arduino board that comes with an Ethernet port and doesn’t need a separate
shield.
Hardware is only one aspect of turning an Arduino into a network device. We
also need some software for network communication. The Arduino IDE comes
with a convenient Ethernet library that contains a few classes related to net-
working. We’ll use it now to access a Daytime service on the Internet.
A Daytime service
9
returns the current date and time as an ASCII string.
Daytime servers listen on either TCP or UDP port 13. You can find many
Daytime services on the Internet; one of them runs at time.nist.gov. Before
7.
http://www.arduino.cc/en/Main/ArduinoEthernetShield
8.
http://www.arduino.cc/en/Main/ArduinoBoardEthernet
9.
http://en.wikipedia.org/wiki/DAYTIME
report erratum • discuss
Communicating Over Networks Using an Ethernet Shield • 173
www.it-ebooks.info