142 lines
3.4 KiB
C++
142 lines
3.4 KiB
C++
/*
|
|
* This example combines the Espressif examples:
|
|
* WebServer --> HelloServer
|
|
* Ethernet --> ETH_LAN8720
|
|
* It implements a webserver using ethernet interface (instead of WiFi)
|
|
* After compiled and uploaded on the terminal
|
|
* you should see the IP address of the board (server)
|
|
* Open the IP in your browser and you should see
|
|
* a message on the homepage: "hello from esp32!"
|
|
* If you append "/inline" to the end of the address
|
|
* you should see: "this works as well"
|
|
*
|
|
* Tested with Espressif package (2.0.11)
|
|
* and Olimex board ESP32-PoE and ESP32-EVB
|
|
*/
|
|
|
|
#include <ETH.h>
|
|
#include <WebServer.h>
|
|
#include <ESPmDNS.h>
|
|
|
|
static bool eth_connected = false;
|
|
WebServer server(80);
|
|
|
|
const int led = 13;
|
|
|
|
void handleRoot() {
|
|
digitalWrite(led, 1);
|
|
server.send(200, "text/plain", "hello from esp32!");
|
|
digitalWrite(led, 0);
|
|
}
|
|
|
|
void handleNotFound() {
|
|
digitalWrite(led, 1);
|
|
String message = "File Not Found\n\n";
|
|
message += "URI: ";
|
|
message += server.uri();
|
|
message += "\nMethod: ";
|
|
message += (server.method() == HTTP_GET) ? "GET" : "POST";
|
|
message += "\nArguments: ";
|
|
message += server.args();
|
|
message += "\n";
|
|
for (uint8_t i = 0; i < server.args(); i++) {
|
|
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
|
|
}
|
|
server.send(404, "text/plain", message);
|
|
digitalWrite(led, 0);
|
|
}
|
|
|
|
void WiFiEvent(WiFiEvent_t event)
|
|
{
|
|
switch (event) {
|
|
case ARDUINO_EVENT_ETH_START:
|
|
Serial.println("ETH Started");
|
|
//set eth hostname here
|
|
ETH.setHostname("esp32-ethernet");
|
|
break;
|
|
case ARDUINO_EVENT_ETH_CONNECTED:
|
|
Serial.println("ETH Connected");
|
|
break;
|
|
case ARDUINO_EVENT_ETH_GOT_IP:
|
|
Serial.print("ETH MAC: ");
|
|
Serial.print(ETH.macAddress());
|
|
Serial.print(", IPv4: ");
|
|
Serial.print(ETH.localIP());
|
|
if (ETH.fullDuplex()) {
|
|
Serial.print(", FULL_DUPLEX");
|
|
}
|
|
Serial.print(", ");
|
|
Serial.print(ETH.linkSpeed());
|
|
Serial.println("Mbps");
|
|
Serial.println(ETH.localIPv6());
|
|
eth_connected = true;
|
|
break;
|
|
case ARDUINO_EVENT_ETH_DISCONNECTED:
|
|
Serial.println("ETH Disconnected");
|
|
eth_connected = false;
|
|
break;
|
|
case ARDUINO_EVENT_ETH_STOP:
|
|
Serial.println("ETH Stopped");
|
|
eth_connected = false;
|
|
break;
|
|
case ARDUINO_EVENT_ETH_GOT_IP6:
|
|
Serial.print("STA IPv6: ");
|
|
Serial.println(ETH.localIPv6());
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void testClient(const char * host, uint16_t port)
|
|
{
|
|
Serial.print("\nconnecting to ");
|
|
Serial.println(host);
|
|
|
|
WiFiClient client;
|
|
if (!client.connect(host, port)) {
|
|
Serial.println("connection failed");
|
|
return;
|
|
}
|
|
client.printf("GET / HTTP/1.1\r\nHost: %s\r\n\r\n", host);
|
|
while (client.connected() && !client.available());
|
|
while (client.available()) {
|
|
Serial.write(client.read());
|
|
}
|
|
|
|
Serial.println("closing connection\n");
|
|
client.stop();
|
|
}
|
|
|
|
void setup()
|
|
{
|
|
delay (500);
|
|
Serial.begin(115200);
|
|
WiFi.onEvent(WiFiEvent);
|
|
|
|
ETH.enableIpV6();
|
|
|
|
ETH.begin();
|
|
|
|
if (MDNS.begin("esp32")) {
|
|
Serial.println("MDNS responder started");
|
|
}
|
|
|
|
server.on("/", handleRoot);
|
|
|
|
server.on("/inline", []() {
|
|
server.send(200, "text/plain", "this works as well");
|
|
});
|
|
|
|
server.onNotFound(handleNotFound);
|
|
|
|
server.begin();
|
|
Serial.println("HTTP server started");
|
|
}
|
|
|
|
void loop ()
|
|
{
|
|
server.handleClient();
|
|
delay(2);//allow the cpu to switch to other tasks
|
|
}
|