Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

W5500 spi ethernet #1235

Closed
dyrvigk opened this issue May 24, 2021 · 36 comments · Fixed by esphome/esphome#4424
Closed

W5500 spi ethernet #1235

dyrvigk opened this issue May 24, 2021 · 36 comments · Fixed by esphome/esphome#4424

Comments

@dyrvigk
Copy link

dyrvigk commented May 24, 2021

Describe the problem you have/What new integration you would like

Please describe your use case for this integration and alternatives you've tried:

I would like to get support for w5500 lan spi interface and using ethernet2 libary the reason is that the supported lan8720 only works with esp32 and it is hardware wise more dificult to get to work
Additional context

@evi-snowm
Copy link

The possability of supporting W5500 as Ethernet option would also be relevant for people interested in using the M5 Atom series of ESP32s. They have a PoE enabled ethernet accessory ( info & specs: https://docs.m5stack.com/en/atom/atom_poe ); this would eliminate power supplies and make having a collection of these a lot easier. Perfectly willing to buy a few to use for testing.

@barnabyjones
Copy link

I was just looking at that M5 Atom POE device too. What is involved in adding support for the w5500 Ethernet chipset?

@nagyrobi
Copy link
Member

Another nice ESP32 product with W5500:
https://kmpelectronics.eu/products/prodino-esp32-ethernet-v1/
Specs and module datasheets at the bottom of the page.

@philipptrenz
Copy link

Also the Norvi ENET "industrial" controllers use a W5500 interface
https://norvi.lk/product/industrial-esp32-ethernet/

@nagyrobi
Copy link
Member

USR-ES1 also W5500:
usr-es1-w5500-chip-uj-spi-lan-ethernet-atalakito-tcp-ip-mod

@dyrvigk
Copy link
Author

dyrvigk commented Jun 14, 2022

i think what vis needed is to make a custom component since it is supported in arduino and libaryies are there ethernet2 and spi is already supported but it seems that Otto Winter dont think there is enough interest for this i myself have used it before in arduino and hardware vise it is much easyier to work with than lan8720. specialy hardware vise i am now using lan 8720 but is is a pain in the butt to make pcb with because of the need for diferential pair routing due to 50mhz clock

@jbabio
Copy link

jbabio commented Jun 17, 2022

@jburget
Copy link

jburget commented Jun 28, 2022

Hey, I would also hope for this to get it supported, industrial shields are amazing products

@nagyrobi
Copy link
Member

How many GPIOs are needed minimum with W5500? Fewer than with LAN8720, right?
5 vs 7, I think? 2 pins fewer needed...

@nagyrobi
Copy link
Member

Additional info: ESPHome supports RP2040 which doesn't have a network connection. W5500 could be perfect:
https://github.com/Wiznet/RP2040-HAT-C/blob/main/getting_started.md

@philipptrenz
Copy link

philipptrenz commented Jun 30, 2022

Just took a look at the Ethernet component (here) and support for phy_lan8720 as well as phy_tlk110 is provided directly be ESP-IDF. Since v4.3 (or even earlier) there also seem to be support for W5500 in ESP-IDF (see here).
Maybe it is not that hard to add native support for W5500 to the Ethernet component?

@nagyrobi
Copy link
Member

Hmm...
One extra step is definitely needed: #1683
Because with ESPHome set to type: esp-idf won't do any Ethernet at all...

@dyrvigk
Copy link
Author

dyrvigk commented Jun 30, 2022 via email

@nagyrobi nagyrobi added this to the Top Requested milestone Jul 1, 2022
@philipptrenz
Copy link

@dyrvigk I did some research and testing. The arduino-libraries/Ethernet@^2.0.0 library works great with the W5500, but as far as I understood, it brings its own IP stack, server implementation etc. while esphome uses the stack from the WiFi library. So I think this won't work out.

But I stumbled over this and it seems that IDF supports SPI to ethernet boards like the W5500 using the "standard" software IP stack. But as @nagyrobi mentioned, the Ethernet component of esphome is not available when using IDF.

@philipptrenz
Copy link

Managed to get a minimal working example using the Arduino framework and the lwip stack of the WiFi library.

#include <Arduino.h>
#include <WiFi.h>
#include <ETH.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include "driver/spi_master.h"
#include <SPI.h>

/* 
 * GPIO config for W5500: 
 * 
 * MISO: GPIO19
 * MOSI: GPIO23
 * SCLK: GPIO18
 * SCS:  GPIO5
 * INT:  GPIO22
 */

AsyncWebServer server(80);

bool setupW5500() {

  WiFi.begin();
  tcpip_adapter_set_default_eth_handlers();

  // Initialize TCP/IP network interface
  ESP_ERROR_CHECK(esp_netif_init());
  esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
  esp_netif_t *eth_netif = esp_netif_new(&cfg);

  esp_eth_mac_t *eth_mac = NULL;
  esp_eth_phy_t *eth_phy = NULL;
  gpio_install_isr_service(0);

  spi_bus_config_t buscfg = {
    .mosi_io_num = 23,  // MOSI
    .miso_io_num = 19,  // MISO
    .sclk_io_num = 18,  // SCLK
    .quadwp_io_num = -1,
    .quadhd_io_num = -1,
  };
  ESP_ERROR_CHECK(spi_bus_initialize(SPI3_HOST, &buscfg, 1));

  spi_device_handle_t spi_handle = NULL;
  spi_device_interface_config_t devcfg = {
      .command_bits = 16,   // Address phase in W5500 SPI frame
      .address_bits = 8,    // Control phase in W5500 SPI frame
      .mode = 0,
      .clock_speed_hz = 12 * 1000 * 1000,
      .spics_io_num = 5,    // SCS
      .queue_size = 20
  };
  ESP_ERROR_CHECK(spi_bus_add_device(SPI3_HOST, &devcfg, &spi_handle));

  /* W5500 ethernet driver uses spi driver */
  eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(spi_handle);
  w5500_config.int_gpio_num = 22;   // INT

  eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  phy_config.reset_gpio_num = -1;

  eth_mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
  if(eth_mac == NULL){
    log_e("esp_eth_mac_new_esp32 failed");
    return false;
  }

  eth_phy = esp_eth_phy_new_w5500(&phy_config);
  if(eth_phy == NULL){
    log_e("esp_eth_phy_new failed");
    return false;
  }
    
  esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(eth_mac, eth_phy);
  esp_eth_handle_t eth_handle = NULL;
  ESP_ERROR_CHECK(esp_eth_driver_install(&eth_config, &eth_handle));

  uint8_t macArr[] = { 0x02, 0x00, 0x00, 0x12, 0x34, 0x56 };
  ESP_ERROR_CHECK(esp_eth_ioctl(eth_handle, ETH_CMD_S_MAC_ADDR, macArr));

  /* Attach Ethernet driver to TCP/IP stack */
  ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle)));

  /* Start Ethernet driver state machine */
  ESP_ERROR_CHECK(esp_eth_start(eth_handle));

  return true;
}

void setupWebserver() {
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
      request->send(200, "text/plain", "Hello world");
  });
  server.onNotFound([](AsyncWebServerRequest *request){ 
    request->send(404, "text/plain", "Not found");
  });
  server.begin();
}

void setup() {
  Serial.begin(115200);
  setupW5500();
  setupWebserver();
}

void loop() {}

From here, it might be manageable to integrate, right? A short assessment from @OttoWinter or maybe @jesserockz would be very helpful!

@rradar rradar added the help wanted Extra attention is needed label Jul 10, 2022
@majkrzak
Copy link

Do you know if there is any progress of this?

I have noticed that @kbx81 in esphome/esphome#3751 mentioned he may be able to prepare PR for it, but no further updates.

@nagyrobi
Copy link
Member

Unfortunately, no

@nagyrobi
Copy link
Member

nagyrobi commented Dec 7, 2022

esphome/esphome#3564 upgrades ethernet for ESPHome to include 2 more drivers and esp-idf support added in esphome/esphome#3565.

This should make it possible to add further Ethernet drivers more easily.

@alve89
Copy link

alve89 commented Dec 24, 2022

Since using the W5500 is so pretty easy on Arduino, I just stumbled over this FR. I'd also appreciate an integration of W5500 support in ESPHome! 😊
Especially the USR-ES1 is cheap, highly available, easy to connect and well documented in lots of examples.

@Thomas7173
Copy link

Thomas7173 commented Feb 6, 2023

I would like to use ESP32 plus W5500 for energy monitoring via S0 counter inside a metallic switchboard. so WIFI is no option. Is any work going on in implementing the W5500 board?

@JeroenVanOort
Copy link

I have just made a draft PR to add support for the W5500 board. I have not been able to test this on an actual device yet, but anyone is welcome to give it a try!

@mhendriks
Copy link

mhendriks commented Mar 12, 2023

Thanks!!! Works great with some changes for the ESP32C3 with W5500 25 MHz clock.

#include <Arduino.h>
#include <WiFi.h>
#include <ETH.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include "driver/spi_master.h"
#include <SPI.h>

AsyncWebServer server(80);

bool setupW5500() {

  WiFi.begin();
  tcpip_adapter_set_default_eth_handlers();

  // Initialize TCP/IP network interface
  ESP_ERROR_CHECK(esp_netif_init());
  esp_netif_config_t cfg = ESP_NETIF_DEFAULT_ETH();
  esp_netif_t *eth_netif = esp_netif_new(&cfg);

  esp_eth_mac_t *eth_mac = NULL;
  esp_eth_phy_t *eth_phy = NULL;
  gpio_install_isr_service(0);

  spi_bus_config_t buscfg = {
    .mosi_io_num = 6,  // MOSI
    .miso_io_num = 5,  // MISO
    .sclk_io_num = 4,  // SCLK
    .quadwp_io_num = -1,
    .quadhd_io_num = -1,
  };
//SPI_DMA_CH_AUTO for the ESP32C3
//SPI2_HOST ... the only one left for the ESP32C3
  ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO));

  spi_device_handle_t spi_handle = NULL;
  spi_device_interface_config_t devcfg = {
      .command_bits = 16,   // Address phase in W5500 SPI frame
      .address_bits = 8,    // Control phase in W5500 SPI frame
      .mode = 0,
      .clock_speed_hz = 25 * 1000 * 1000,
      .spics_io_num = 10,    // SCS
      .queue_size = 20
  };
//SPI2_HOST ... the only one left for the ESP32C3
  ESP_ERROR_CHECK(spi_bus_add_device(SPI2_HOST, &devcfg, &spi_handle));

  /* W5500 ethernet driver uses spi driver */
  eth_w5500_config_t w5500_config = ETH_W5500_DEFAULT_CONFIG(spi_handle);
  w5500_config.int_gpio_num = 1;   // INT

  eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
  eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();
  phy_config.reset_gpio_num = -1;

  eth_mac = esp_eth_mac_new_w5500(&w5500_config, &mac_config);
  if(eth_mac == NULL){
    log_e("esp_eth_mac_new_esp32 failed");
    return false;
  }

  eth_phy = esp_eth_phy_new_w5500(&phy_config);
  if(eth_phy == NULL){
    log_e("esp_eth_phy_new failed");
    return false;
  }
    
  esp_eth_config_t eth_config = ETH_DEFAULT_CONFIG(eth_mac, eth_phy);
  esp_eth_handle_t eth_handle = NULL;
  ESP_ERROR_CHECK(esp_eth_driver_install(&eth_config, &eth_handle));

  uint8_t macArr[] = { 0x02, 0x00, 0x00, 0x12, 0x34, 0x56 };
  ESP_ERROR_CHECK(esp_eth_ioctl(eth_handle, ETH_CMD_S_MAC_ADDR, macArr));

  /* Attach Ethernet driver to TCP/IP stack */
  ESP_ERROR_CHECK(esp_netif_attach(eth_netif, esp_eth_new_netif_glue(eth_handle)));

  /* Start Ethernet driver state machine */
  ESP_ERROR_CHECK(esp_eth_start(eth_handle));

  return true;
}

void setupWebserver() {
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
      request->send(200, "text/plain", "Hello world W5500 test");
  });
  server.onNotFound([](AsyncWebServerRequest *request){ 
    request->send(404, "text/plain", "Not found");
  });
  server.begin();
}

void setup() {
  Serial.begin(115200);
  USBSerial.begin(115200); //initialise CDC output
  setupW5500();
  setupWebserver();
//  USBSerial.print(F("ETH IP : ")); //CDC output
//  USBSerial.println(ETH.localIP().toString());
//  USBSerial.print(F("ETH MAC: ")); //CDC output
//  USBSerial.println(ETH.macAddress());
}

void loop() {}

@DjordjeMandic
Copy link

Basically if this gets added we can use esp32c3 for eth-ble bridge?

@NajibOladosu
Copy link

Sorry, I'm new to this but i need to know if theUSR-ES1 W5500 can be used to power a pyboard directly or is some device needed. If I need a device, can I get your recommendations?

@yellobyte
Copy link

Another ESP32-Ethernet board variant hosting a W5500: https://github.com/yellobyte/ESP32-DevBoards-Getting-Started/tree/main/boards/YB-ESP32-S3-ETH(YelloByte) ...and waiting to get native support.
As indicated under platformio/platform-espressif32#1123 this is not targeted for Arduino Cores < 3.0.0. However, Ethernet(2) lib is sufficient for most requirements though.

@vitoraugustodesm
Copy link

I bought this w5500 shield and was hoping w5500 could be natively supported on esphome. https://aliexpress.com/item/1005003589665341.html

@fightforlife
Copy link

See this PR discussion for working ethernet component on ESP32 Arduino and ESP-IDF.
esphome/esphome#4424

@cybershoe
Copy link

I'd love to see this extended to include the rp2040, not just ESP-IDF. The PoE Featherwing along with the RP2040-shim makes for a very compact platform for wired PoE sensors.

@pixelwave
Copy link

Waiting eagerly for native W5500 support to use the Lilygo T-ETH-Lite S3 with ESPHome!

@linkedupbits
Copy link

I have tested this with

It works, but needs to have a slower clock speed .
This is my YAML (referencing the external components):

esphome:
  name: t-relay
  friendly_name: t-relay

esp32:
  board: esp32dev
  framework:
    type: esp-idf
    

external_components:
  - source:
      type: git
      url: https://github.com/JeroenVanOort/esphome/
      ref: eth-w5500
    components: [ ethernet,network,api,esp32]

# Enable logging
# Logger must be at least debug (default)
logger:
  level: DEBUG


# Enable Home Assistant API
# Enable Home Assistant API
api:
  encryption: 
    key: !secret grandviewcontrol_encryption_key

ota:

ethernet:
  type: W5500
  clk_pin: GPIO22
  mosi_pin: GPIO26
  miso_pin: GPIO13
  cs_pin: GPIO27
  interrupt_pin: GPIO36
  reset_pin: GPIO23
  clock_speed: 16Mhz #works
#  clock_speed: 32Mhz #doesn't

@pixelwave
Copy link

pixelwave commented Mar 12, 2024

Trying to access the ESP32 connected via POE/Ethernet results now in the message:

INFO ESPHome 2024.2.2
INFO Reading configuration /config/woesp32-s3-7p-eth.yaml...
ERROR Unable to import component ethernet:
Traceback (most recent call last):
  File "/esphome/esphome/loader.py", line 169, in _lookup_module
    module = importlib.import_module(f"esphome.components.{domain}")
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/config/.esphome/external_components/2f02e929/esphome/components/ethernet/__init__.py", line 11, in <module>
    from esphome.const import (
ImportError: cannot import name 'CONF_SPI' from 'esphome.const' (/esphome/esphome/const.py)
Failed config

api: [source /config/woesp32-s3-7p-eth.yaml:17]
  
  Component api requires component network.
  encryption: 
    key: 2RUdO7HGxXxo0WOYrW6MXLJ3axAMqkLLGCSHsPjszVk=
ota: [source /config/woesp32-s3-7p-eth.yaml:21]
  
  Component ota requires component network.
  password: 0a20e03c82db327d1524d8c2b8c8d555
ethernet: [source /config/woesp32-s3-7p-eth.yaml:31]
  
  Component not found: ethernet.
  type: W5500
  clk_pin: GPIO10
  mosi_pin: GPIO12
  miso_pin: GPIO11
  cs_pin: GPIO09
  interrupt_pin: GPIO13
  reset_pin: GPIO14
  clock_speed: 30Mhz
web_server_base: None
  {}
Component web_server_base requires component network

The ESP32 (Lilygo T-ETH-S3) with W5500 still functions with this integration:

esphome:
  name: ...
  friendly_name: ...
  platformio_options:
    board_build.flash_mode: dio

external_components:
  - source:
      type: git
      url: https://github.com/JeroenVanOort/esphome/
      ref: eth-w5500
    components: [ ethernet ]

ethernet:
  type: W5500
  clk_pin: GPIO10
  mosi_pin: GPIO12
  miso_pin: GPIO11
  cs_pin: GPIO09
  interrupt_pin: GPIO13
  reset_pin: GPIO14
  clock_speed: 30Mhz

@dyrvigk
Copy link
Author

dyrvigk commented Mar 12, 2024 via email

@pixelwave
Copy link

pixelwave commented Mar 12, 2024

Ah ok thanks for clarification!

If it is already in the dev then I guess it will be soon also in the release version if testing goes well .. right?

@dyrvigk
Copy link
Author

dyrvigk commented Mar 12, 2024 via email

@archef2000
Copy link

archef2000 commented Mar 22, 2024

I have an ESP32-cam board with limited GPIO ports and got the ethernet W5500 component working with 20Mhz. But I would like to get higher speed for the camera.

ethernet:
  domain: .fritz.box
  type: w5500
  clk_pin: GPIO15 # SCK
  mosi_pin: GPIO14 # MO
  cs_pin: GPIO13 # CS
  miso_pin: GPIO12 # M
  clock_speed: 20Mhz

But I can't get any higher clock speed than 20Mhz.
Do I need the interrupt/reset pin to get anything higher?

@dyrvigk
Copy link
Author

dyrvigk commented Mar 23, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.