onds to give the service some preparation time, and then we read and print
its output character by character.
The client’s interface is similar to that of the
Serial
class. The
available
function
checks whether some bytes are still available, and
read
returns the next byte.
At the end, we call
stop
to disconnect from the service, and then we start again.
Note that our program isn’t completely robust. If the server needs longer than
300 milliseconds to deliver its data, our program will not read it. In our case
it’s not a big deal, but for more critical applications, you’d better wait until
data is available and add a timeout mechanism.
Compile and upload the program to the Arduino. Then open the serial monitor,
and you should see something like this:
Connecting...connected.
56807 14-11-04 16:34:18 50 0 0 259.2 UTC(NIST) *
Disconnecting.
Connecting...connected.
56807 14-11-04 16:34:20 50 0 0 515.5 UTC(NIST) *
Disconnecting.
We’re finished! Our Arduino is directly connected to the Internet, and it even
does something useful: we’ve turned it into a very accurate clock.
All in all, networking with an Arduino doesn’t differ much from networking
with a PC, if you use the Ethernet shield. In the next section, you’ll learn how
to use services such as DHCP and DNS with an Arduino.
Using DHCP and DNS
In the preceding section, you learned how to access IP services the “hard”
way. That is, you had to know your own IP address and the services’s IP
address, too. For your home projects, this is convenient and efficient.
As soon as you create projects that have to run in unknown environments,
you have to use a more flexible approach. If you’re going to build an actual
product based on a networking Arduino, you certainly don’t want your cus-
tomers to enter an unused IP address and upload a new sketch before they
can use it.
In such cases you need a more flexible solution. In this section you’ll determine
service addresses using the Domain Name System (DNS), and you’ll obtain
the Arduino’s IP address using the Dynamic Host Configuration Protocol
(DHCP).
Here’s a version of our time server example that uses DHCP and DNS:
report erratum • discuss
Using DHCP and DNS • 177
www.it-ebooks.info