|
-
-
- #include <SPI.h>
- #include <NativeEthernet.h>
-
-
-
- byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
-
-
-
-
- char server[] = "www.google.com";
-
-
- IPAddress ip(192, 168, 0, 177);
- IPAddress myDns(192, 168, 0, 1);
-
-
-
-
- EthernetClient client;
-
-
- unsigned long beginMicros, endMicros;
- unsigned long byteCount = 0;
- bool printWebData = true;
-
- void setup() {
-
-
-
-
-
-
-
-
-
- Serial.begin(9600);
- while (!Serial) {
- ;
- }
-
-
- Serial.println("Initialize Ethernet with DHCP:");
- if (Ethernet.begin(mac) == 0) {
- Serial.println("Failed to configure Ethernet using DHCP");
-
- if (Ethernet.hardwareStatus() == EthernetNoHardware) {
- Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
- while (true) {
- delay(1);
- }
- }
- if (Ethernet.linkStatus() == LinkOFF) {
- Serial.println("Ethernet cable is not connected.");
- }
-
- Ethernet.begin(mac, ip, myDns);
- } else {
- Serial.print(" DHCP assigned IP ");
- Serial.println(Ethernet.localIP());
- }
-
- delay(1000);
- Serial.print("connecting to ");
- Serial.print(server);
- Serial.println("...");
-
-
- if (client.connect(server, 80)) {
- Serial.print("connected to ");
- Serial.println(client.remoteIP());
-
- client.println("GET /search?q=arduino HTTP/1.1");
- client.println("Host: www.google.com");
- client.println("Connection: close");
- client.println();
- } else {
-
- Serial.println("connection failed");
- }
- beginMicros = micros();
- }
-
- void loop() {
-
-
- int len = client.available();
- if (len > 0) {
- byte buffer[80];
- if (len > 80) len = 80;
- client.read(buffer, len);
- if (printWebData) {
- Serial.write(buffer, len);
- }
- byteCount = byteCount + len;
- }
-
-
- if (!client.connected()) {
- endMicros = micros();
- Serial.println();
- Serial.println("disconnecting.");
- client.stop();
- Serial.print("Received ");
- Serial.print(byteCount);
- Serial.print(" bytes in ");
- float seconds = (float)(endMicros - beginMicros) / 1000000.0;
- Serial.print(seconds, 4);
- float rate = (float)byteCount / seconds / 1000.0;
- Serial.print(", rate = ");
- Serial.print(rate);
- Serial.print(" kbytes/second");
- Serial.println();
-
-
- while (true) {
- delay(1);
- }
- }
- }
|