Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
179
Tweeting Arduinos
One of the most popular hardware kits available is the Botanicall. It checks whether your plants need water, and if they do,
it sends a reminder message via http://twitter.com/. As soon as
you water it, it dutifully sends a Thank You message. Although
the ofcial version of the Botanicall is a specialized piece of
hardware, you can build it using an Arduino.
Botanicalls certainly make your life a bit easier. Whether the
Twitwee Clock improves your life is a matter of taste. This modied cuckoo clock looks for Twitter updates using a wireless
Internet connection. Whenever it nds a programmable search
term, it displays the corresponding tweets on a display and also
pops out a cuckoo making some noise. Youd better ask your
family up front before you build this project and install it in your
living room.
.
.
.
http://www.botanicalls.com/
http://www.botanicalls.com/archived_kits/twitter/
http://www.haroonbaig.com/projects/TwitweeClock/
that you need a complete PC, while for many applications the Arduinos
hardware capabilities would be sufcient. In this section, youll learn
how to solve this problem with an Ethernet shield.
You cant connect a naked Arduino to a network. Not only are its hardware capabilities too limited, it also doesnt have an Ethernet port. That
means you cant plug an Ethernet cable into it, 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; they all are good and serve their
purpose well.7 For prototyping I prefer the ofcial shield,8 because it
comes with sockets for all pins (its on the left side in Figure 8.6, on the
next page). Also at the time of this writing the Arduino team announced
the Arduino Ethernet, an Arduino board that comes with an Ethernet
port and does not need a separate shield.
7.
8.
180
9.
http://en.wikipedia.org/wiki/DAYTIME
181
#include <SPI.h>
#include <Ethernet.h>
byte mac[]
= { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte my_ip[]
= { 192, 168, 2, 120 };
byte time_server[] = { 192, 43, 244, 18 }; // time.nist.gov
10
-
15
-
void setup() {
Ethernet.begin(mac, my_ip);
Serial.begin(BAUD_RATE);
}
20
void loop() {
delay(1000);
Serial.print("Connecting...");
if (!client.connect()) {
Serial.println("connection failed.");
} else {
Serial.println("connected.");
delay(1000);
25
-
while (client.available()) {
char c = client.read();
Serial.print(c);
}
30
-
Serial.println("Disconnecting.");
client.stop();
35
-
strings format.
182
First, we include the Ethernet library and dene a constant for the
DAYTIME service port (we also have to include the SPI library, because
the Ethernet library depends on it). Then we dene three byte arrays:
mac contains the MAC address we are going to use for the Ethernet shield. A MAC address is a 48-bit number that uniquely
identies a network device.11 Usually, the manufacturer sets this
identier, but for the Ethernet shield, we have to set it ourselves;
we use an arbitrary number.
Important note: the MAC address has to be unique on your network. If you connect more than one Arduino, make sure they all
have different MAC addresses!
Whenever you connect your PC to the Internet, it probably gets
a new IP address via the Dynamic Host Conguration Protocol
(DHCP).12 For most Arduino applications, a DHCP implementation
is comparatively costly, so you usually assign an IP address manually. In most cases, this will be a local address in the 192.168.x.y
range; we store this address in the my_ip array.
To turn domain names such as time.nist.gov into an IP address,
you need access to the Domain Name System (DNS). The Arduinos
standard library doesnt support DNS, so we have to nd out the
IP address ourselves. We assign it to time_server. The telnet command already turned the DAYTIME service domain name into an
IP address for us. Alternatively, you can use one of the following
commands to determine a domain names IP address:
maik> host time.nist.gov
time.nist.gov has address 192.43.244.18
maik> dig +short time.nist.gov
192.43.244.18
maik> resolveip time.nist.gov
IP address of time.nist.gov is 192.43.244.18
maik> ping -c 1 time.nist.gov
PING time.nist.gov (192.43.244.18): 56 data bytes
64 bytes from 192.43.244.18: icmp_seq=0 ttl=48 time=173.598 ms
--- time.nist.gov ping statistics --1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 173.598/173.598/173.598/0.000 ms
11. http://en.wikipedia.org/wiki/Mac_address
12. http://en.wikipedia.org/wiki/Dynamic_Host_Conguration_Protocol
183
In line 11, we create a new Client object. This class is part of the Ethernet library and allows us to create network clients that connect to a
certain IP address and port.
Now we have to initialize the Ethernet shield itself; we do this in line
14 in the setup( ) function. We have to invoke Ethernet.begin( ), passing it
our MAC and IP addresses. Then we initialize the serial port so that we
can output some debug messages. At this point, weve initialized all the
components we need, so we can nally connect to the DAYTIME server
and read its output.
Please note that you can also pass the IP address of your network gateway and your subnet mask to Ethernet.begin( ). This is necessary if you
do not connect the Arduino directly to the Internet but use a router or
a cable modem instead. In this case, you can pass the gateway address
as follows:
// ...
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte my_ip[] = { 192, 168, 2, 120 };
byte time_server[] = { 192, 43, 244, 18 }; // time.nist.gov
// Insert IP address of your cable or DSL router below:
byte gateway[] = { 192, 168, 13, 254 };
Client client(time_server, DAYTIME_PORT);
void setup() {
Ethernet.begin(mac, my_ip, gateway);
Serial.begin(BAUD_RATE);
}
// ...
The loop( ) function of our sketch starts with a short delay, allowing all
components to initialize properly. This is necessary because the Ethernet shield is an autonomous device that is capable of working in parallel
to the Arduino. In line 22, we try to connect to the DAYTIME service. If
the connection cannot be established, we print an error message. Otherwise, we wait for half a second to give the service some preparation
time, and then we read and print its output character by character.
Note that the clients interface is similar to the interface of the Serial
class. With available( ), we can check 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.
Compile and upload the program to the Arduino. Then open the serial
monitor, and you should see something like this:
184
http://arduino.cc/en/Main/ArduinoBoardLilyPad
http://blog.makezine.com/archive/2010/03/email-counting_t-shirt.html
http://blog.makezine.com/archive/2010/01/arduino_powered_mood_meter.html
http://luminet.cc
Connecting...connected.
55480 10-10-11 13:32:23 28 0 0 579.9 UTC(NIST) *
Disconnecting.
Connecting...connected.
55480 10-10-11 13:32:26 28 0 0
Disconnecting.
34.5 UTC(NIST) *
185
http://gkaindl.com/software/arduino-ethernet
http://code.google.com/p/webduino/
186