|
| 1 | +/* |
| 2 | + Web client |
| 3 | +
|
| 4 | + This sketch connects to a website (http://www.google.com) |
| 5 | + using an Arduino WIZnet Ethernet shield. |
| 6 | +
|
| 7 | + Circuit: |
| 8 | + * Ethernet shield attached to default SPI pins |
| 9 | +
|
| 10 | + created 18 Dec 2009 |
| 11 | + by David A. Mellis |
| 12 | + modified 9 Apr 2012 |
| 13 | + by Tom Igoe, based on work by Adrian McEwen |
| 14 | + */ |
| 15 | + |
| 16 | +#include <SPI.h> |
| 17 | +#include <EthernetCompat.h> |
| 18 | + |
| 19 | +// Enter a MAC address for your controller below. |
| 20 | +// Newer Ethernet shields have a MAC address printed on a sticker on the shield |
| 21 | +byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; |
| 22 | + |
| 23 | +// if you don't want to use DNS (and reduce your sketch size) |
| 24 | +// use the numeric IP instead of the name for the server: |
| 25 | +//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS) |
| 26 | +char server[] = "www.google.com"; // name address for Google (using DNS) |
| 27 | + |
| 28 | +// Set the static IP address to use if the DHCP fails to assign |
| 29 | +IPAddress ip(192, 168, 0, 177); |
| 30 | +IPAddress myDns(192, 168, 0, 1); |
| 31 | + |
| 32 | +// Initialize the Ethernet client library |
| 33 | +// with the IP address and port of the server |
| 34 | +// that you want to connect to (port 80 is default for HTTP): |
| 35 | +EthernetClient client; |
| 36 | + |
| 37 | +// Variables to measure the speed |
| 38 | +unsigned long beginMicros, endMicros; |
| 39 | +unsigned long byteCount = 0; |
| 40 | +bool printWebData = true; // set to false for better speed measurement |
| 41 | + |
| 42 | +void setup() { |
| 43 | + // You can use Ethernet.init to configure the pins |
| 44 | + // the values in the commented out init are the defaults |
| 45 | + // #define IRQ_PIN 4 //IRQ pin must be wired |
| 46 | + // #define RESET_PIN -1 // reset pin is optional |
| 47 | + // Ethernet.init(ETH_PHY_W5500, ETH_PHY_ADDR_AUTO, SS, IRQ_PIN, RESET_PIN); |
| 48 | + // Ethernet.init(ETH_PHY_W5500, ETH_PHY_ADDR_AUTO, SS, IRQ_PIN, RESET_PIN, SPI, SCK, MISO, MOSI); |
| 49 | + |
| 50 | + // Open serial communications and wait for port to open: |
| 51 | + Serial.begin(115200); |
| 52 | + while (!Serial) { |
| 53 | + ; // wait for serial port to connect. Needed for native USB port only |
| 54 | + } |
| 55 | + |
| 56 | + // start the Ethernet connection: |
| 57 | + Serial.println("Initialize Ethernet with DHCP:"); |
| 58 | + if (Ethernet.begin(mac) == 0) { |
| 59 | + Serial.println("Failed to configure Ethernet using DHCP"); |
| 60 | + // Check for Ethernet hardware present |
| 61 | + if (Ethernet.hardwareStatus() == EthernetNoHardware) { |
| 62 | + Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); |
| 63 | + while (true) { |
| 64 | + delay(1); // do nothing, no point running without Ethernet hardware |
| 65 | + } |
| 66 | + } |
| 67 | + if (Ethernet.linkStatus() == LinkOFF) { |
| 68 | + Serial.println("Ethernet cable is not connected."); |
| 69 | + } |
| 70 | + // try to configure using IP address instead of DHCP: |
| 71 | + Ethernet.begin(mac, ip, myDns); |
| 72 | + } else { |
| 73 | + Serial.print(" DHCP assigned IP "); |
| 74 | + Serial.println(Ethernet.localIP()); |
| 75 | + } |
| 76 | + // give the Ethernet shield a second to initialize: |
| 77 | + delay(1000); |
| 78 | + Serial.print("connecting to "); |
| 79 | + Serial.print(server); |
| 80 | + Serial.println("..."); |
| 81 | + |
| 82 | + // if you get a connection, report back via serial: |
| 83 | + if (client.connect(server, 80)) { |
| 84 | + Serial.print("connected to "); |
| 85 | + Serial.println(client.remoteIP()); |
| 86 | + // Make a HTTP request: |
| 87 | + client.println("GET /search?q=arduino HTTP/1.1"); |
| 88 | + client.println("Host: www.google.com"); |
| 89 | + client.println("Connection: close"); |
| 90 | + client.println(); |
| 91 | + } else { |
| 92 | + // if you didn't get a connection to the server: |
| 93 | + Serial.println("connection failed"); |
| 94 | + } |
| 95 | + beginMicros = micros(); |
| 96 | +} |
| 97 | + |
| 98 | +void loop() { |
| 99 | + // if there are incoming bytes available |
| 100 | + // from the server, read them and print them: |
| 101 | + int len = client.available(); |
| 102 | + if (len > 0) { |
| 103 | + byte buffer[80]; |
| 104 | + if (len > 80) { |
| 105 | + len = 80; |
| 106 | + } |
| 107 | + client.read(buffer, len); |
| 108 | + if (printWebData) { |
| 109 | + Serial.write(buffer, len); // show in the serial monitor (slows some boards) |
| 110 | + } |
| 111 | + byteCount = byteCount + len; |
| 112 | + } |
| 113 | + |
| 114 | + // if the server's disconnected, stop the client: |
| 115 | + if (!client.connected()) { |
| 116 | + endMicros = micros(); |
| 117 | + Serial.println(); |
| 118 | + Serial.println("disconnecting."); |
| 119 | + client.stop(); |
| 120 | + Serial.print("Received "); |
| 121 | + Serial.print(byteCount); |
| 122 | + Serial.print(" bytes in "); |
| 123 | + float seconds = (float) (endMicros - beginMicros) / 1000000.0; |
| 124 | + Serial.print(seconds, 4); |
| 125 | + float rate = (float) byteCount / seconds / 1000.0; |
| 126 | + Serial.print(", rate = "); |
| 127 | + Serial.print(rate); |
| 128 | + Serial.print(" kbytes/second"); |
| 129 | + Serial.println(); |
| 130 | + |
| 131 | + // do nothing forevermore: |
| 132 | + while (true) { |
| 133 | + delay(1); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments