Hello. I have been trying to connect my ESP32-S3 (No PSRAM varient) to the internet using the W5500 ethernet module. I am using the standard W5500 chinese module. I have tried several different libraries. The one which did work for me is the LilyGO-T-ETH-Series one (I am attaching the code). So it does connect to the internet using this code occasionally, that too when I fiddled with the jumper wires. However, I did triple check the wires with the multimeter if any of them were broken from the inside. I also checked the header pins of my ESP and the W5500 as well if they were loose. I also tried powering up my W5500 module with a buck converter because I read somewhere that ethernet modules can be power hungry and the first time it worked was when it was hooked up to a buck converter. I am testing the code on the Arduino IDE 2.3.3 and ESP32 Core version 3.0.5.
Code I used
#if ESP_ARDUINO_VERSION < ESP_ARDUINO_VERSION_VAL(3, 0, 0)
#include <ETHClass2.h> //Is to use the modified ETHClass
#define ETH ETH2
#else
#include <ETH.h>
#include <NetworkClientSecure.h>
#endif
#include <PubSubClient.h>
#include <SPI.h>
#include "utilities.h" //Board PinMap
#include <WiFi.h>
bool eth_connected = false;
WiFiClient ethClient;
PubSubClient client(ethClient);
// //---------------------------------------------------------------------------------------
// #define W5500_RESET_COMMAND 0x80 // Reset command for W5500 (writing to MR)
// #define W5500_MR_ADDRESS 0x00 // Mode Register (MR) address for W5500
// // Function to write to Mode Register (MR) of W5500
// void writeMR(uint8_t value) {
// // SPI communication to write to Mode Register
// // Ensure the SPI interface is correctly set up before calling this
// // Example: SPI.transfer(), SPI.beginTransaction(), etc.
// SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE0)); // Adjust SPI settings accordingly
// digitalWrite(ETH_CS_PIN, LOW); // Select W5500
// SPI.transfer(W5500_MR_ADDRESS); // Address for MR register
// SPI.transfer(value); // Write the reset command to the MR register
// digitalWrite(ETH_CS_PIN, HIGH); // Deselect W5500
// SPI.endTransaction();
// }
// // Function to read from Mode Register (MR) of W5500
// uint8_t readMR() {
// uint8_t mr;
// SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE0)); // Adjust SPI settings accordingly
// digitalWrite(ETH_CS_PIN, LOW); // Select W5500
// SPI.transfer(W5500_MR_ADDRESS); // Address for MR register
// mr = SPI.transfer(0x00); // Read the value from the MR register
// digitalWrite(ETH_CS_PIN, HIGH); // Deselect W5500
// SPI.endTransaction();
// return mr; // Return the value read from the Mode Register
// }
// // W5500 Soft Reset Function
// uint8_t softReset() {
// uint16_t count = 0;
// // Write the reset command to Mode Register (MR)
// writeMR(W5500_RESET_COMMAND);
// // Wait for the reset to complete
// do {
// uint8_t mr = readMR();
// // Reset complete when MR is 0
// if (mr == 0) {
// return 1; // Success: Reset complete
// }
// delay(1); // Wait a short period before checking again
// } while (++count < 20); // Wait up to 20ms for the reset to complete
// return 0; // Failure: Reset did not complete in time
// }
// //--------------------------------------------------------------------------------------------------------------------
void WiFiEvent(arduino_event_id_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 callback(char *topic, uint8_t *payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
void setup() {
Serial.begin(115200);
WiFi.onEvent(WiFiEvent);
#ifdef ETH_POWER_PIN
pinMode(ETH_POWER_PIN, OUTPUT);
digitalWrite(ETH_POWER_PIN, HIGH);
#endif
#if CONFIG_IDF_TARGET_ESP32
if (!ETH.begin(ETH_TYPE, ETH_ADDR, ETH_MDC_PIN,
ETH_MDIO_PIN, ETH_RESET_PIN, ETH_CLK_MODE)) {
Serial.println("ETH start Failed!");
}
#else
if (!ETH.begin(ETH_PHY_W5500, 1, ETH_CS_PIN, ETH_INT_PIN, ETH_RST_PIN,
SPI3_HOST,
ETH_SCLK_PIN, ETH_MISO_PIN, ETH_MOSI_PIN)) {
Serial.println("ETH start Failed!");
}
#endif
// Note - the default maximum packet size is 128 bytes. If the
// combined length of clientId, username and password exceed this use the
// following to increase the buffer size:
client.setBufferSize(255);
while (!eth_connected) {
Serial.println("Wait eth connect...");
// digitalWrite(ETH_RST_PIN, LOW);
// delay(1);
// digitalWrite(ETH_RST_PIN, HIGH);
// softReset();
delay(1000);
}
//set server and port
client.setServer("broker-cn.emqx.io", 1883);
//set callback
client.setCallback(callback);
const char *clien_id = "esp32eth";
while (!client.connect(clien_id)) {
delay(200);
Serial.println("Connecting....");
}
client.subscribe("/esp32eth");
// publish example
// client.publish("outTopic", "hello world");
Serial.println("MQTT Connected!");
}
void loop() {
client.loop();
}
[The Github repository ]
(GitHub - Xinyuan-LilyGO/LilyGO-T-ETH-Series)
The above shared link is of the repository I got the code from. They have multiple boards however, I found their LILYGO_T_ETH_LITE_ESP32S3 board most relevant to my setup.
The pins I am using for the connections are:
W5500 <--> ESP32-S3
MISO <--> 11
MOSI <--> 12
SCLK <--> 10
CS <--> 9
INT <--> 13
RST <--> 14
Any ideas as to where I might be going wrong or what I can check to debug my issue. I am happy to answer anymore questions that might required for the debugging.
Thanks