|
| 1 | +/* |
| 2 | + The classic Arduino WebClient Ethernet example |
| 3 | +
|
| 4 | + This sketch connects to a website (http://arduino.cc) using Ethernet. |
| 5 | +
|
| 6 | + created 18 Dec 2009 |
| 7 | + by David A. Mellis |
| 8 | + modified 9 Apr 2012 |
| 9 | + by Tom Igoe, based on work by Adrian McEwen |
| 10 | +
|
| 11 | + */ |
| 12 | + |
| 13 | +#include <EthernetESP32.h> |
| 14 | +#include <NetworkClientSecure.h> |
| 15 | + |
| 16 | +W5500Driver driver; |
| 17 | +//ENC28J60Driver driver; |
| 18 | +//EMACDriver driver(ETH_PHY_LAN8720); |
| 19 | + |
| 20 | +char server[] = "arduino.tips"; |
| 21 | + |
| 22 | +// Set the static IP address to use if the DHCP fails to assign |
| 23 | +IPAddress ip(192, 168, 0, 177); |
| 24 | + |
| 25 | +// Initialize the Ethernet client library |
| 26 | +// with the IP address and port of the server |
| 27 | +// that you want to connect to (port 80 is default for HTTP): |
| 28 | +NetworkClientSecure client; |
| 29 | + |
| 30 | +void setup() { |
| 31 | + |
| 32 | + Serial.begin(115200); |
| 33 | + delay(500); |
| 34 | + while (!Serial); |
| 35 | + |
| 36 | + Ethernet.init(driver); |
| 37 | + |
| 38 | + // start the Ethernet connection: |
| 39 | + Serial.println("Initialize Ethernet with DHCP:"); |
| 40 | + if (Ethernet.begin() == 0) { |
| 41 | + Serial.println("Failed to configure Ethernet using DHCP"); |
| 42 | + // Check for Ethernet hardware present |
| 43 | + if (Ethernet.hardwareStatus() == EthernetNoHardware) { |
| 44 | + Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); |
| 45 | + while (true) { |
| 46 | + delay(1); // do nothing, no point running without Ethernet hardware |
| 47 | + } |
| 48 | + } |
| 49 | + if (Ethernet.linkStatus() == LinkOFF) { |
| 50 | + Serial.println("Ethernet cable is not connected."); |
| 51 | + } |
| 52 | + // try to configure using IP address instead of DHCP: |
| 53 | + Ethernet.begin(ip); |
| 54 | + } else { |
| 55 | + Serial.print(" DHCP assigned IP "); |
| 56 | + Serial.println(Ethernet.localIP()); |
| 57 | + } |
| 58 | + |
| 59 | + Serial.print("connecting to "); |
| 60 | + Serial.print(server); |
| 61 | + Serial.println("..."); |
| 62 | + |
| 63 | + client.setInsecure(); // use only for a test! |
| 64 | + |
| 65 | + // if you get a connection, report back via serial: |
| 66 | + if (client.connect(server, 443)) { |
| 67 | + Serial.print("connected to "); |
| 68 | + Serial.println(client.remoteIP()); |
| 69 | + // Make a HTTP request: |
| 70 | + client.println("GET /asciilogo.txt HTTP/1.1"); |
| 71 | + client.print("Host: "); |
| 72 | + client.println(server); |
| 73 | + client.println("Connection: close"); |
| 74 | + client.println(); |
| 75 | + } else { |
| 76 | + // if you didn't get a connection to the server: |
| 77 | + Serial.println("connection failed"); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +void loop() { |
| 82 | + // if there are incoming bytes available |
| 83 | + // from the server, read them and print them: |
| 84 | + while (client.available()) { |
| 85 | + char c = client.read(); |
| 86 | + Serial.write(c); |
| 87 | + } |
| 88 | + |
| 89 | + // if the server's disconnected, stop the client: |
| 90 | + if (!client.connected()) { |
| 91 | + Serial.println(); |
| 92 | + Serial.println("disconnecting."); |
| 93 | + client.stop(); |
| 94 | + |
| 95 | + // do nothing forevermore: |
| 96 | + while (true) { |
| 97 | + delay(1); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments