Ethernet to WiFi bridge using ESP32

Thread Starter

JohanEricson

Joined Aug 30, 2017
38
Hi everyone!

I'm trying to make an ethernet to WiFi bridge, to connect at device that only supports ethernet via WiFi. I am vary new to networking and have not done a ton of programming. But some...

I think I want to accomplish this:

DEVICE ---eth--->ESP32 wifi station---WiFi--->Home network WiFi AP

I did eventually find this example, and i "added wifi station mode". So that i am able to connect to the home network via the ESP32 WiFi. But not via ethernet.
After some (a lot) of testing and fiddling about i think the problem is that ethernet is only an DHCP-client, and my device is therefore not getting an IPaddress.

I have been looking through the code and the library but haven't been able to figure out how to set DHCP-mode. I can't even figure out how it's set for the WiFi AP mode. The library seems to be nicely written, but it's way above my programming level.

Now, i know there are things to buy to accomplish this. But that just isn't an option in this case. The point of the test is the ESP32.
And, i know i should just learn to code and learn how networks work. But hey, who has time for that?

Board: ESP32-C6-DEVKIT
IDE: Arduino IDE
Library: arduino-esp32 Ethernet

So, the question is; Is it even possible?

Thank you in advance!

Below is my code with the addition to the library example.

C++:
#include <WiFi.h>
#include <ETH.h>
#include <SPI.h>

#define ETH_TYPE     ETH_PHY_W5500
#define ETH_ADDR     1 // defualt
#define ETH_CS       21
#define ETH_IRQ      4 // defualt not connected
#define ETH_RST      5 // defualt
#define ETH_SPI_SCK  19
#define ETH_SPI_MISO 20
#define ETH_SPI_MOSI 18

#define AP_SSID "ESP32-ETH-WIFI-BRIDGE"
#define AP_PASS "password"

#define STA_SSID "EricsonBenchWiFi"
#define STA_PASS  "123456789"

IPAddress ap_ip(192, 168, 4, 1);
IPAddress ap_mask(255, 255, 255, 0);
IPAddress ap_leaseStart(192, 168, 4, 2);
IPAddress ap_dns(8, 8, 4, 4);

void setup() {
  Serial.begin(115200);
  Serial.setDebugOutput(true);
  Network.onEvent(onEvent);

  WiFi.AP.begin();
  WiFi.AP.config(ap_ip, ap_ip, ap_mask, ap_leaseStart, ap_dns);
  WiFi.AP.create(AP_SSID, AP_PASS);
  if (!WiFi.AP.waitStatusBits(ESP_NETIF_STARTED_BIT, 1000)) {
    Serial.println("Failed to start AP!");
    return;
  }
  delay(100);

  SPI.begin(ETH_SPI_SCK, ETH_SPI_MISO, ETH_SPI_MOSI);
  ETH.begin(ETH_TYPE, ETH_ADDR, ETH_CS, ETH_IRQ, ETH_RST, SPI);
  WiFi.begin(STA_SSID, STA_PASS);
}

void loop() {
  delay(20000);
}

void onEvent(arduino_event_id_t event, arduino_event_info_t info) {
  switch (event) {
/*----------------Ethernet events-----------------------------------------*/
    case ARDUINO_EVENT_ETH_START:
      Serial.println("ETH Started");
      break;

    case ARDUINO_EVENT_ETH_CONNECTED:
      Serial.println("ETH Connected");
      break;

    case ARDUINO_EVENT_ETH_GOT_IP:
      Serial.println("ETH Got IP");
      Serial.println(ETH);
      WiFi.AP.enableNAPT(true);
      break;

    case ARDUINO_EVENT_ETH_LOST_IP:
      Serial.println("ETH Lost IP");
      WiFi.AP.enableNAPT(false);
      break;
    case ARDUINO_EVENT_ETH_DISCONNECTED:
      Serial.println("ETH Disconnected");
      WiFi.AP.enableNAPT(false);
      break;

    case ARDUINO_EVENT_ETH_STOP:
      Serial.println("ETH Stopped");
      break;

/*-------------------WiFi Access point events-----------------------------------------*/
    case ARDUINO_EVENT_WIFI_AP_START:
      Serial.println("AP Started");
      Serial.println(WiFi.AP);
      break;

    case ARDUINO_EVENT_WIFI_AP_STACONNECTED:   
      Serial.println("AP STA Connected");
      break;

    case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED:
      Serial.println("AP STA Disconnected");
      break;

    case ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED:
      Serial.print("AP STA IP Assigned: ");
      Serial.println(IPAddress(info.wifi_ap_staipassigned.ip.addr));
      break;

    case ARDUINO_EVENT_WIFI_AP_PROBEREQRECVED:
      Serial.println("AP Probe Request Received");
      break;

    case ARDUINO_EVENT_WIFI_AP_STOP:
      Serial.println("AP Stopped");
      break;

/*-------------------WiFi Station events-----------------------------------------*/
    case ARDUINO_EVENT_WIFI_STA_START:     
      Serial.println("STA Started");
      break;

    case ARDUINO_EVENT_WIFI_STA_CONNECTED:
      Serial.println("STA Connected");
      break;

    case ARDUINO_EVENT_WIFI_STA_GOT_IP:
      Serial.println("STA Got IP");
      Serial.println(WiFi.STA);
      WiFi.AP.enableNAPT(true);
      break;

    case ARDUINO_EVENT_WIFI_STA_LOST_IP:
      Serial.println("STA Lost IP");
      WiFi.AP.enableNAPT(false);
      break;

    case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
      Serial.println("STA Disconnected");
      WiFi.AP.enableNAPT(false);
      break;

    case ARDUINO_EVENT_WIFI_STA_STOP:
      Serial.println("STA Stopped");
      break;

    default: break;
  }
}
 
Top