Hardware Reference
In-Depth Information
complicated software; although you can create a real web server, you can also
get great results from Python scripts. Your Python script could then inform
Arduinos of the temperature that you want for your living room or when to
turn on the automatic sprinkler system.
Example Program
Now that you have fetched a web page from a web server, it is time to tell the
Arduino to do the same thing. The sketch will look like Listing 9-1.
Listing 9-1: Fetching (fi lename: Chapter9client.ino )
1 #include <SPI.h>
2 #include <Ethernet.h>
3
4 // If your Arduino has a MAC address, use that instead
5 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
6 char server[] = " www.packetfury.net"; // name of server
7
8 // Set a static IP address to use if the DHCP fails to assign
9 IPAddress ip(192,168,0,42);
10
11 // Initialize the Ethernet client library
12 EthernetClient client;
13
14 void setup()
15 {
16 // Open serial communications and wait for port to open:
17 Serial.begin(9600);
18
19 // Start the Ethernet connection:
20 if (Ethernet.begin(mac) == 0)
21 {
22 Serial.println("Failed to configure Ethernet using DHCP");
23 // Can't get an IP, so use another one
24 Ethernet.begin(mac, ip);
25 }
26 // Give the Ethernet shield some time to initialize:
27 delay(2000);
28 Serial.println("Connecting...");
29
30 // Are we connected?
31 if (client.connect(server, 80))
32 {
33 Serial.println("Connected");
34 // Make a HTTP request:
35 client.println("GET helloarduino.html HTTP/1.1");
36 client.println("Host: www.packetfury.net");
37 client.println();
38 }
Continues
 
Search WWH ::




Custom Search