Skip to content

Commit 5cc8a1f

Browse files
committed
more examples
1 parent 5157c4b commit 5cc8a1f

File tree

4 files changed

+361
-0
lines changed

4 files changed

+361
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* BasivHttpClient example from the ESP32 HTTPClient library modified for Ethernet.
3+
*/
4+
5+
#include <EthernetESP32.h>
6+
#include <HTTPClient.h>
7+
8+
#define USE_SERIAL Serial
9+
/*
10+
const char* ca = \
11+
"-----BEGIN CERTIFICATE-----\n" \
12+
"MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n" \
13+
"MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" \
14+
"DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n" \
15+
"SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n" \
16+
"GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC\n" \
17+
"AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF\n" \
18+
"q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8\n" \
19+
"SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0\n" \
20+
"Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA\n" \
21+
"a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj\n" \
22+
"/PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T\n" \
23+
"AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG\n" \
24+
"CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv\n" \
25+
"bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k\n" \
26+
"c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw\n" \
27+
"VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC\n" \
28+
"ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz\n" \
29+
"MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu\n" \
30+
"Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF\n" \
31+
"AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo\n" \
32+
"uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/\n" \
33+
"wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu\n" \
34+
"X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG\n" \
35+
"PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6\n" \
36+
"KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n" \
37+
"-----END CERTIFICATE-----\n";
38+
*/
39+
40+
W5500Driver driver;
41+
//ENC28J60Driver driver;
42+
//EMACDriver driver(ETH_PHY_LAN8720);
43+
44+
void setup() {
45+
46+
USE_SERIAL.begin(115200);
47+
delay(500);
48+
while (!USE_SERIAL);
49+
50+
USE_SERIAL.println();
51+
Ethernet.init(driver);
52+
53+
USE_SERIAL.println("Initialize Ethernet with DHCP:");
54+
if (Ethernet.begin()) {
55+
USE_SERIAL.print(" DHCP assigned IP ");
56+
USE_SERIAL.println(Ethernet.localIP());
57+
} else {
58+
USE_SERIAL.println("Failed to configure Ethernet using DHCP");
59+
while (true) {
60+
delay(1);
61+
}
62+
}
63+
64+
}
65+
66+
void loop() {
67+
68+
HTTPClient http;
69+
70+
USE_SERIAL.print("[HTTP] begin...\n");
71+
// configure traged server and url
72+
//http.begin("https://www.howsmyssl.com/a/check", ca); //HTTPS
73+
http.begin("http://example.com/index.html"); //HTTP
74+
75+
USE_SERIAL.print("[HTTP] GET...\n");
76+
// start connection and send HTTP header
77+
int httpCode = http.GET();
78+
79+
// httpCode will be negative on error
80+
if (httpCode > 0) {
81+
// HTTP header has been send and Server response header has been handled
82+
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
83+
84+
// file found at server
85+
if (httpCode == HTTP_CODE_OK) {
86+
String payload = http.getString();
87+
USE_SERIAL.println(payload);
88+
}
89+
} else {
90+
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
91+
}
92+
93+
http.end();
94+
95+
delay(5000);
96+
}

examples/BasicOTA/BasicOTA.ino

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* BasicOTA example from the ESP32 ArduinoOTA library modified for Ethernet.
3+
*/
4+
5+
#include <EthernetESP32.h>
6+
#include <ESPmDNS.h>
7+
#include <ArduinoOTA.h>
8+
9+
W5500Driver driver;
10+
//ENC28J60Driver driver;
11+
//EMACDriver driver(ETH_PHY_LAN8720);
12+
13+
void setup() {
14+
15+
Serial.begin(115200);
16+
delay(500);
17+
while (!Serial);
18+
19+
Ethernet.init(driver);
20+
21+
Serial.println("Initialize Ethernet with DHCP:");
22+
if (Ethernet.begin()) {
23+
Serial.print(" DHCP assigned IP ");
24+
Serial.println(Ethernet.localIP());
25+
} else {
26+
Serial.println("Failed to configure Ethernet using DHCP");
27+
while (true) {
28+
delay(1);
29+
}
30+
}
31+
32+
// Port defaults to 3232
33+
// ArduinoOTA.setPort(3232);
34+
35+
// Hostname defaults to esp3232-[MAC]
36+
// ArduinoOTA.setHostname("myesp32");
37+
38+
// No authentication by default
39+
// ArduinoOTA.setPassword("admin");
40+
41+
// Password can be set with it's md5 value as well
42+
// MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
43+
// ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
44+
45+
ArduinoOTA
46+
.onStart([]() {
47+
String type;
48+
if (ArduinoOTA.getCommand() == U_FLASH) {
49+
type = "sketch";
50+
} else { // U_SPIFFS
51+
type = "filesystem";
52+
}
53+
54+
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
55+
Serial.println("Start updating " + type);
56+
})
57+
.onEnd([]() {
58+
Serial.println("\nEnd");
59+
})
60+
.onProgress([](unsigned int progress, unsigned int total) {
61+
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
62+
})
63+
.onError([](ota_error_t error) {
64+
Serial.printf("Error[%u]: ", error);
65+
if (error == OTA_AUTH_ERROR) {
66+
Serial.println("Auth Failed");
67+
} else if (error == OTA_BEGIN_ERROR) {
68+
Serial.println("Begin Failed");
69+
} else if (error == OTA_CONNECT_ERROR) {
70+
Serial.println("Connect Failed");
71+
} else if (error == OTA_RECEIVE_ERROR) {
72+
Serial.println("Receive Failed");
73+
} else if (error == OTA_END_ERROR) {
74+
Serial.println("End Failed");
75+
}
76+
});
77+
78+
ArduinoOTA.begin();
79+
80+
Serial.println("Ready");
81+
Serial.print("IP address: ");
82+
Serial.println(Ethernet.localIP());
83+
}
84+
85+
void loop() {
86+
ArduinoOTA.handle();
87+
}

examples/HelloServer/HelloServer.ino

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* HelloServer example from the ESP32 WebServer library modified for Ethernet.
3+
*/
4+
5+
#include <EthernetESP32.h>
6+
#include <WebServer.h>
7+
#include <ESPmDNS.h>
8+
9+
W5500Driver driver;
10+
//ENC28J60Driver driver;
11+
//EMACDriver driver(ETH_PHY_LAN8720);
12+
13+
WebServer server(80);
14+
15+
const int led = 13;
16+
17+
void handleRoot() {
18+
digitalWrite(led, 1);
19+
server.send(200, "text/plain", "hello from esp32!");
20+
digitalWrite(led, 0);
21+
}
22+
23+
void handleNotFound() {
24+
digitalWrite(led, 1);
25+
String message = "File Not Found\n\n";
26+
message += "URI: ";
27+
message += server.uri();
28+
message += "\nMethod: ";
29+
message += (server.method() == HTTP_GET) ? "GET" : "POST";
30+
message += "\nArguments: ";
31+
message += server.args();
32+
message += "\n";
33+
for (uint8_t i = 0; i < server.args(); i++) {
34+
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
35+
}
36+
server.send(404, "text/plain", message);
37+
digitalWrite(led, 0);
38+
}
39+
40+
void setup(void) {
41+
pinMode(led, OUTPUT);
42+
digitalWrite(led, 0);
43+
Serial.begin(115200);
44+
45+
Serial.begin(115200);
46+
delay(500);
47+
while (!Serial);
48+
49+
Ethernet.init(driver);
50+
51+
Serial.println("Initialize Ethernet with DHCP:");
52+
if (Ethernet.begin()) {
53+
Serial.print(" DHCP assigned IP ");
54+
Serial.println(Ethernet.localIP());
55+
} else {
56+
Serial.println("Failed to configure Ethernet using DHCP");
57+
while (true) {
58+
delay(1);
59+
}
60+
}
61+
62+
if (MDNS.begin("esp32")) {
63+
Serial.println("MDNS responder started");
64+
}
65+
66+
server.on("/", handleRoot);
67+
68+
server.on("/inline", []() {
69+
server.send(200, "text/plain", "this works as well");
70+
});
71+
72+
server.onNotFound(handleNotFound);
73+
74+
server.begin();
75+
Serial.println("HTTP server started");
76+
}
77+
78+
void loop(void) {
79+
server.handleClient();
80+
delay(2); //allow the cpu to switch to other tasks
81+
}

examples/WebClient/WebClient.ino

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
15+
W5500Driver driver;
16+
//ENC28J60Driver driver;
17+
//EMACDriver driver(ETH_PHY_LAN8720);
18+
19+
char server[] = "arduino.tips";
20+
21+
// Set the static IP address to use if the DHCP fails to assign
22+
IPAddress ip(192, 168, 0, 177);
23+
24+
// Initialize the Ethernet client library
25+
// with the IP address and port of the server
26+
// that you want to connect to (port 80 is default for HTTP):
27+
EthernetClient client;
28+
29+
void setup() {
30+
31+
Serial.begin(115200);
32+
delay(500);
33+
while (!Serial);
34+
35+
Ethernet.init(driver);
36+
37+
// start the Ethernet connection:
38+
Serial.println("Initialize Ethernet with DHCP:");
39+
if (Ethernet.begin() == 0) {
40+
Serial.println("Failed to configure Ethernet using DHCP");
41+
// Check for Ethernet hardware present
42+
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
43+
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
44+
while (true) {
45+
delay(1); // do nothing, no point running without Ethernet hardware
46+
}
47+
}
48+
if (Ethernet.linkStatus() == LinkOFF) {
49+
Serial.println("Ethernet cable is not connected.");
50+
}
51+
// try to configure using IP address instead of DHCP:
52+
Ethernet.begin(ip);
53+
} else {
54+
Serial.print(" DHCP assigned IP ");
55+
Serial.println(Ethernet.localIP());
56+
}
57+
58+
Serial.print("connecting to ");
59+
Serial.print(server);
60+
Serial.println("...");
61+
62+
// if you get a connection, report back via serial:
63+
if (client.connect(server, 80)) {
64+
Serial.print("connected to ");
65+
Serial.println(client.remoteIP());
66+
// Make a HTTP request:
67+
client.println("GET /asciilogo.txt HTTP/1.1");
68+
client.print("Host: ");
69+
client.println(server);
70+
client.println("Connection: close");
71+
client.println();
72+
} else {
73+
// if you didn't get a connection to the server:
74+
Serial.println("connection failed");
75+
}
76+
}
77+
78+
void loop() {
79+
// if there are incoming bytes available
80+
// from the server, read them and print them:
81+
while (client.available()) {
82+
char c = client.read();
83+
Serial.write(c);
84+
}
85+
86+
// if the server's disconnected, stop the client:
87+
if (!client.connected()) {
88+
Serial.println();
89+
Serial.println("disconnecting.");
90+
client.stop();
91+
92+
// do nothing forevermore:
93+
while (true) {
94+
delay(1);
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)