Wifi and Ethernet combination

Hello Everybody, I just need someone with more expertise to let me know if this is even possible. I am currently using an esp32-eth01 board and I want to connect a sensor via an ethernet cable to the board and then have that same board connected to my Wifi router. The end goal is to send some messages over MQTT with information from the sensor. Is this at all possible and how would I go about this?

Is the Ethernet cable from the sensor going to be used as a network connection or merely as a method of connecting wires between the sensor and the board ?

What is the sensor ?

A picture of a hand-drawn wiring diagram would be helpful.

The ethernet cable would not be used as a network connection. The board would connect to the router over wireless. The ethernet cable is for the sensor and only as a method of connecting wires.

Please provide details of the sensor and how far from the ESP it will be located

Ethernet cables are are really good choice if your distances are pretty far because you can use all the twisted pairs to isolate power/ground/signal. Get that part working at whatever distance you need first, then move off to connect to wifi and your broker.

take a couple of simple test program (which represent the requirements of your project) one for Ethernet second from WiFi which you know work and combine them to test on the ESP32-ETH01
e.g. running a UDPchat server on Ethernet and a Webserver on WiFi on the ESP32-ETH01

code UDPchat.ino for Ethernet

// UDP chat program using  ESP32-ETH01  tested on UNO and Nano
// **** change IP addresses and ports to suit requirements *****

#include <EthernetESP32.h>
#include <EthernetUdp.h>  // for UDP

//  EMACDriver(EthPhyType phyType, int mdcPin = 23, int mdioPin = 18, int powerPin = -1, emac_rmii_clock_gpio_t clockPin = EMAC_APPL_CLK_OUT_GPIO, emac_rmii_clock_mode_t clockMode = EMAC_CLK_EXT_IN);
EMACDriver driver(ETH_PHY_LAN8720, 23, 18, 16, EMAC_CLK_IN_GPIO);  // note powerPin = 16 required


// *****   IP of this machine and remote machine *********
IPAddress localIP;  // these will be read from the keyboard
IPAddress remoteIP;

// Enter a MAC address
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x32 };

unsigned int localPort = 10000;   // local port to listen on
unsigned int remotePort = 10000;  // remote port to transmiit too

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  //while (!Serial)
  //  ;
  delay(2000);
  Serial.println("\nEthernet UDP chat program");
  // Check for Ethernet hardware present
  // You can use Ethernet.init(pin) to configure the CS pin
  //Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH Shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit FeatherWing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit FeatherWing Ethernet
  Ethernet.init(driver);  // for ESP32-ETH01
 Serial.println("Initialize Ethernet with DHCP:");
  if (Ethernet.begin()) {
    Serial.print("  DHCP assigned IP ");
    Serial.println(Ethernet.localIP());
  } else {
    Serial.println("Failed to configure Ethernet using DHCP");
    while (true) {
      delay(1);
    }
  }
  Udp.begin(localPort);  // start UDP
  Serial.print("Ethernet UDP started ");
  displayIPaddress(Ethernet.localIP(), localPort);
  // get remote IP address
  remoteIP = getIPaddress("\nenter remote IP address (e.g. 192.168.1.176)? ");
  Serial.print("\nRemote ");
  displayIPaddress(remoteIP, remotePort);
  setupWiFi();
}

void loop() {
  loopWiFi();
  // if Serial text entered transmit as a datagram
  if (Serial.available()) {
    Udp.begin(localPort);
    char text[100] = { 0 };
    Serial.readBytesUntil('\n', text, 100);
    Serial.print("Transmitting to ");
    displayIPaddress(remoteIP, remotePort);
    Serial.println(text);
    Udp.beginPacket(remoteIP, remotePort);  // transmit datagram
    Udp.print(text);
    Udp.endPacket();
  }
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    displayIPaddress(Udp.remoteIP(), Udp.remotePort());
    // read the packet into packetBuffer
    char packetBuffer[100] = { 0 };      // buffer to hold incoming packet,
    Udp.read(packetBuffer, packetSize);  //UDP_TX_PACKET_MAX_SIZE);     // receive datagram
    Serial.print("Contents: ");
    Serial.println(packetBuffer);
    // if (strcmp(packetBuffer, "OK") != 0) {

    // send a reply to the IP address and port that sent us the packet we received
    //   Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    // Udp.write("OK");
    //  Udp.endPacket();
    //}
  }
  delay(10);
}

// read IP address from keyboard and check it
IPAddress getIPaddress(const char* prompt) {
  IPAddress ip;
  while (1) {  // read  IP (end with new line)
    Serial.print(prompt);
    while (Serial.available() == 0) delay(10);
    char text[40] = { 0 };
    Serial.readBytesUntil('\n', (char*)text, 40);
    for (int i = 0; i < 40; i++)
      if (text[i] < ' ') text[i] = 0;  // remove CR or LF
    Serial.print(text);
    if (ip.fromString(text)) break;  // if IP OK break while
    Serial.println(" invalid IP try again");
  }
  return ip;
}


// print IPAdress and port
void displayIPaddress(const IPAddress address, unsigned int port) {
  Serial.print(" IP ");
  for (int i = 0; i < 4; i++) {
    Serial.print(address[i], DEC);
    if (i < 3) Serial.print(".");
  }
  Serial.print(" port ");
  Serial.println(port);
}

void displayMACaddress(byte address[]) {
  Serial.print("MAC address ");
  for (int i = 0; i < 6; i++) {
    Serial.print("0x");
    Serial.print(address[i], HEX);
    if (i < 5) Serial.print(".");
  }
  Serial.println();
}

code Webserver.ino (place in same directory as above UDPchat.ino file) for WiFi

// from https://electropeak.com/learn/create-a-web-server-w-esp32/

/*
  ESP32 Web Server - STA Mode
  modified on 25 MAy 2019
  by Mohammadreza Akbari @ Electropeak
  Home
*/

#include <WiFi.h>
#include <WebServer.h>

// SSID & Password
const char* ssid = "xxxxxxxxxxx";
const char* password = "zzzzzzzzzzzzzzzzz";

WebServer server(80);  // Object of WebServer(HTTP port, 80 is defult)

void setupWiFi() {
  Serial.begin(115200);
  Serial.println("Try Connecting to ");
  Serial.println(ssid);

  // Connect to your wi-fi modem
  WiFi.begin(ssid, password);

  // Check wi-fi is connected to wi-fi network
  while (WiFi.status() != WL_CONNECTED) {
  delay(1000);
  Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected successfully");
  Serial.print("Got IP: ");
  Serial.println(WiFi.localIP());  //Show ESP32 IP on serial

  server.on("/", handle_root);

  server.begin();
  Serial.println("HTTP server started");
  delay(100); 
}

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

// HTML & CSS contents which display on web server
String HTML = "<!DOCTYPE html>\
<html>\
<body>\
<h1>My First Web Server with ESP32 - Station Mode &#128522;</h1>\
</body>\
</html>";

// Handle root url (/)
void handle_root() {
  server.send(200, "text/html", HTML);
}

the only modifications made to the test programs were

  1. the setup() and loop() function identifiers in file Webserver.ino were changed to setupWiFi() and loopWiFi()
  2. in file UDPchat.ino setup() called setupWiFi() and loop() called loopWiFi()

serial monitor output from ESP32-ETH01

Ethernet UDP chat program
Initialize Ethernet with DHCP:
  DHCP assigned IP 192.168.1.67
Ethernet UDP started  IP 192.168.1.67 port 10000

enter remote IP address (e.g. 192.168.1.176)? 192.168.1.65
Remote  IP 192.168.1.65 port 10000
Try Connecting to 
XXXXXXXX
..........
WiFi connected successfully
Got IP: 192.168.1.106
HTTP server started
Transmitting to  IP 192.168.1.65 port 10000
hello Java from ESP32-ETH-01

Transmitting to  IP 192.168.1.65 port 10000
test2 from ETH-01

Received packet of size 15
From  IP 192.168.1.65 port 51300
Contents: hello from java
Received packet of size 15
From  IP 192.168.1.65 port 49261
Contents: test2 from Java

output from Java UDPchat program running CMD window on PC

F:\Ardunio\Networking\Ethernet\ESP32-ETH01\UDPchat_WiFi_webserver>java UDPchat
chat program: IP address BB-DELL2/192.168.1.65 port 10000
UDP datagram length 29  from IP /192.168.1.106 received: hello Java from ESP32-ETH-01
UDP datagram length 18  from IP /192.168.1.106 received: test2 from ETH-01
hello from java
Sending to 192.168.1.67 socket 10000 data: hello from java
test2 from Java
Sending to 192.168.1.67 socket 10000 data: test2 from Java

web client on PC displays

once you know the combined programs work you can start implementing your project