ESP32-PoE Arduino Core Webserver

Started by avsteele, August 02, 2023, 05:05:21 PM

Previous topic - Next topic

avsteele

Are there any examples of how to get a server running on the physical Ethernet connection?

I see this question asked in the past without any response, e.g.:
https://www.olimex.com/forum/index.php?topic=7241.0
https://www.olimex.com/forum/index.php?topic=8915.0

The closest example provided by Olimex does *not* work. It is clear this example is design to use the WiFi connection
https://github.com/OLIMEX/ESP32-POE/blob/master/SOFTWARE/ARDUINO/ESP32_PoE_WebServer_Demo/ESP32_PoE_WebServer_Demo.ino

The ESP32 example in 'Arduino15\packages\esp32\hardware\esp32\2.0.7\libraries\Ethernet\examples\ETH_LAN8720') does not include server functionality.


I can get the example running which allows it to pull an IP address. I can ping it fine.  However, the recommended library <ESP32Webserver> does not appear to respond to any GET requests. I believe that library only works for the WiFi connection.

Quote#include <Arduino.h>
#include <ETH.h>
#include <ESP32WebServer.h>

static bool eth_connected = false;

ESP32WebServer server(80);

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");
      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;
    default:
      Serial.println("ETH Unknown event");
  }
}

void handleRoot() {
  Serial.println("Handle root");
  //digitalWrite ( led, 1 );
  char temp[400];
  int sec = millis() / 1000;
  int min = sec / 60;
  int hr = min / 60;

  snprintf ( temp, 400,

"<html>\
  <head>\
    <meta http-equiv='refresh' content='5'/>\
    <title>ESP32 Demo</title>\
    <style>\
      body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
    </style>\
  </head>\
  <body>\
    <h1>Hello from ESP32!</h1>\
    <p>Uptime: %02d:%02d:%02d</p>\
    <img src=\"/test.svg\" />\
  </body>\
</html>",

    hr, min % 60, sec % 60
  );
  server.send ( 200, "text/html", temp );
}


void setup() {
  Serial.begin(115200);
  WiFi.onEvent(WiFiEvent);
  ETH.begin();
  server.on ( "/", handleRoot );
  delay(1000);
}

void loop() {
  server.handleClient();
}


Stanimir5F

Hello, avsteele!

It is true that we don't have a webserver with ethernet example. However in the latest versions in the espressif package (2.0.11 at the moment of writing) they have plenty of webserver examples (true, with WiFi) and also an example with ethernet.
You can combine the two and you will get a webserver example with ethernet. Here I have combined the simplest webserver example (HelloServer) with the ethernet example (ETH_LAN8720) and I can see the initial page printing "hello from esp32!" and if you append "/inline" to the end of the address: "this works as well":

/*
    This sketch shows the Ethernet event usage

*/

#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");
      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;
    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.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
}


Obviously if you need something more advanced you can use as a base any of the other examples provided by Espressif. I hope this helps you with the issue you have.

Stan, Olimex
May the Source be with You!

avsteele

Thank you very much Stan. This does appear to work.