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