ESP32 allocating IP adress

Hi,

We have a Waveshare ESP32 S3 ETH and we would like to connect this device via LAN cable to a router (192.168.1.1) in a standalone network environment. The ESP32 should have the static IP 192.168.1.7. My sketch reads in sensor data and sends this data every second via UDP to a specific target IP.

The problem is, that my sketch works fine, if i connect the ESP32 to my notebook and connect it via USB (COM9) und run ArduinoIDE. Via Wireshark I am able to verify this. So in this case ESP32 we receive Power and Data via USB. Even via the web user interface of the router, I can see this device/IP.

If I now connect the ESP32 directly to a electric socket and connect it via LAN (data) to the router, then the code seems not to work. The router can not identify the device and there are no UDP packages. Or in other words: It seems, that there is no allocation of IP address.

The main parts of the sketch are here:

// W5500 Pin Definitions
#define W5500_CS     14                               // Chip Select pin
#define W5500_RST     9                               // Reset pin 
#define W5500_INT    10                               // Interrupt pin 
#define W5500_MISO   12                               // MISO pin
#define W5500_MOSI   11                               // MOSI pin
#define W5500_SCK    13                               // Clock pin

byte mac[] = { 0xAB, 0xCD, 0xDE, 0xAB, 0xCD, 0xEF };
IPAddress ip(192, 168, 1, 7);
IPAddress dns(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
unsigned int targetPort = 8889;
unsigned int localPort = 8888; 

EthernetClient client;
EthernetUDP Udp;
void setup() {
  Serial.begin(115200);   // Start serial communication
  while (!Serial) {
    ;                     // Wait until serial port is ready
  }  
// Initialize SPI with custom pin configuration
  SPI.begin(W5500_SCK, W5500_MISO, W5500_MOSI, W5500_CS);
  // Initialize Ethernet with static IP settings
  Ethernet.init(W5500_CS);
  Ethernet.begin(mac, ip, dns, gateway, subnet);
  Udp.begin(localPort);
  // Verify if IP address is properly assigned
  if (Ethernet.localIP() == IPAddress(0, 0, 0, 0)) {
    Serial.println("Failed to configure Ethernet with static IP");
    while (true);         // Halt on failure
  }
…
}

Can somebody explain me, if there are “blocking” parts of the code, if i run this without an ArduinoIDE/notebook, but in a standalone network? Are there maybe any problem, that the sketch can not create a Serial object?

Thanks

My guess is this part of code waits for serial port to get ready, and since your laptop is not connected to the unit, it waits indefinitely. Try this variation instead:

unsigned long startTime = millis();
while (!Serial && (millis() - startTime < 5000)) {
} 

This would wait for 5 seconds for serial port before continuing.

Welcome to the forum

You have this in your sketch

 while (!Serial) {
    ;                     // Wait until serial port is ready
  }  

If the board is not connected to the PC via USB then there will not be a Serial connection and this while loop will never end

Thanks for your answers. What is exactly the behaviour of all coming up “Serial.println(…)”-lines, if there is no Serial connection? Will they be skipped? Or is there any possibilty to distinguish between “debugging environment” (with Serial) and “production environment" (without Serial) ?

I mean, of course I make a condition in front of every “Serial.println();” statement, like

if (Serial) { Serial.println(““); }

But in my opinion, this produces more unnecessary and unreadable code.