Giga R1 WiFi Send/Receive UDP only when connected to laptop via serial

Hello there, I am attempting to communicate with UDP via WiFi, from my laptop to my Giga R1.
With the below code I can connect to WiFi and successfully send and receive a packet using packet sender on the laptop. However this is only when the Giga is connect via USB to a port in the laptop. I have an external power supply for the Giga and I want to be able to receive packets without having to be connected to the laptop. Any help is appreciated.

#include <SPI.h>
#include <WiFi.h>
#include <WiFiUdp.h>

int status = WL_IDLE_STATUS;
//#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = "SSID";         // your network SSID (name)
char pass[] = "Password";  // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;                  // your network key index number (needed only for WEP)

unsigned int localPort = 4210;  // local port to listen on

char PacketBuffer[250];  //buffer to hold incoming packet

WiFiUDP Udp; //Creates a named instance of the WiFi UDP class that can send and receive UDP messages


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ;  // wait for serial port to connect. Needed for native USB port only
  }

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true)
      ;
  }

  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) 
  {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 5 seconds for connection:
    delay(5000);
  }
  Serial.println("Connected to WiFi");
  printWifiStatus();

  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  Udp.begin(localPort);
}

void loop() {

  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if (packetSize) 
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remoteIp = Udp.remoteIP();
    Serial.print(remoteIp);
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    int len = Udp.read(PacketBuffer, 200);
    PacketBuffer[len] = 0;
    Serial.println("Contents:");
    Serial.println(PacketBuffer);
  }

Hi @joshm02.

This code will make your program stay in an infinite loop until the serial port is opened in Arduino IDE Serial Monitor (or an equivalent application). The reason that is useful is because on native USB boards like yours, the board is not reset when you open Serial Monitor. That means any serial output printed between the time the program starts running and when you get Serial Monitor open will be lost.

In applications where missing that serial output would be a problem, it's useful to add this code to the sketch in order to make the program wait for the Serial Monitor before running.

However, if you are wanting to run your program when the board is not connected to Serial Monitor, you must remove that while loop to allow the rest of the sketch to run.