← index #16496Issue #16732
Related · high · value 0.624
QUERY · ISSUE

dual ethernet in esp32 port

openby littlegreenbean33opened 2024-12-29updated 2025-03-03
enhancementport-esp32

Description

As per Micropython documentation it is possible to have (at least) 2 LAN ports.

https://docs.micropython.org/en/v1.24.0/library/network.LAN.html#network.LAN
id is the number of the Ethernet port, either 0 or 1.

However, in the esp32 port the LAN object is implemented as a static object.
As per @robert-hh this requires convert this object into a dynamic object and the effort would not be substantial. It's unlikely I can pitch in development, but I may be able to help testing.

Code Size

My use case is in bridging 2 networks that for security reason ( or other reasons ), need to remains isolated ( at level 2/3 ISO layers). No ethernet packets or IP packets between networks. However, it is OK to exchange data at application layer on verified appliances.

This case is frequent in industrial setups, where multiple networks are isolated but frequently there is need to exchange information between them. The simplest case if between devices isolated in network due to lack of security compliance and the rest of IT infra. Think an old robot still using Windows X that cost millions to replace. Or an old automation system, a third party system that cannot be audited/patched.

Bandwidth is not an issue frequently in these systems, either wise bare metal programing is required.

This is the code I have been testing on a ESP module prog 1

mport network, time, urequests
from machine import Pin, SPI,reset
import socket

a = False
#if a :
spi1 = SPI(1, miso=Pin(12), mosi=Pin(13), sck=Pin(14))
#spi.write(b'\x00\x39\x00')
#print(spi.read(1))
lan1 = network.LAN(0,phy_type=network.PHY_W5500, phy_addr=0, cs=Pin(15), int=Pin(11), spi=spi1)
lan1.config(mac=b"\x02\x00\x00\x12\x34\x56")
lan1.active(1)
time.sleep(1)
while lan1.status() != network.ETH_GOT_IP:
    print(".", end="")
    time.sleep(1)
print(f" {lan1.ipconfig('addr4')}, {network.ipconfig('dns')}") 
print(f" {lan1.ifconfig()}")
#else:
spi2 = SPI(2, miso=Pin(47), mosi=Pin(21), sck=Pin(18))
#spi.write(b'\x00\x39\x00')
#print(spi.read(1))
lan2 = network.LAN(1,phy_type=network.PHY_W5500, phy_addr=1, cs=Pin(16), int=Pin(17), spi=spi2)
lan2.config(mac=b"\x02\x00\x00\x12\x34\x57")
lan2.active(1)
time.sleep(1)
while lan2.status() != network.ETH_GOT_IP:
    print(".", end="")
    time.sleep(1)
print(f" {lan2.ipconfig('addr4')}, {network.ipconfig('dns')}") 
print(f" {lan2.ifconfig()}")


With the net results of

lan1 is lan2 
>>>True

Implementation

I hope the MicroPython maintainers or community will implement this feature

Code of Conduct

Yes, I agree

CANDIDATE · ISSUE

Reinitializing network.LAN with W5500 Causes Connection Loss Until Hard Reset

closedby hosseinghaheriopened 2025-02-10updated 2025-02-19
bugport-esp32

Port, board and/or hardware

esp32 port

MicroPython version

All Versions
MicroPython v1.25.0-preview.283.g11c9656fa on 2025-02-10; Generic ESP32 module with SPIRAM with ESP32

Reproduction

Issue Description:

When running the following MicroPython script on an ESP32 with a W5500 Ethernet module, the connection works correctly on the first run after powering up the board. However, if the script is executed again without a full power cycle or machine.reset(), the device loses network connectivity (cannot be pinged).

  1. Power on the ESP32 with the W5500 module connected.
  2. Run the following MicroPython script:
import network
import time
from machine import Pin, SPI, reset

rst = Pin(15, Pin.OUT)
rst.value(0)
time.sleep(0.1)
rst.value(1)
time.sleep(0.5)

spi_nic = SPI(1, baudrate=26000000, sck=Pin(18), mosi=Pin(21), miso=Pin(19))

nic = network.LAN(phy_type=network.PHY_W5500, spi=spi_nic, phy_addr=1, cs=Pin(5), int=Pin(4))

config = ('192.168.1.120', '255.255.255.0', '192.168.1.1', '8.8.8.8')
nic.ifconfig(config)
nic.config(mac=b'\x02\xAB\xCD\xEF\x01\x02')

nic.active(True)
time.sleep(0.5)

while not nic.isconnected():
    print(".", end="")
    time.sleep(0.25)

print("\r\nip=", nic.ifconfig())

  1. Confirm that the device is reachable via ping.
  2. Run the script again without resetting the board.
  3. Observe that the device no longer responds to ping, and the network connection is lost.
  4. If machine.reset() is executed before re-running the script, the connection works fine again.

Expected behaviour

Re-running the script should properly reset the W5500 and establish a new network connection without requiring a full board reset.

Observed behaviour

After executing the script multiple times without a full reset, the ESP32 loses network connectivity. The W5500 module does not seem to reinitialize correctly unless a full machine.reset() is performed.

Additional Information

Workarounds Tried:

Explicitly deactivating nic before reinitializing:

try:
    nic.active(False)
    del nic
except:
    pass

→ Did not solve the issue.
Resetting the W5500 before reinitialization using rst pin:
→ No effect; issue persists.
Performing machine.reset() before re-running the script:
→ Works, but not an ideal solution

Code of Conduct

Yes, I agree

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied