# UDP.parsePacket() returns 0 when SYSTEM\_MODE set to SEMI\_AUTOMATIC/MANUAL \[SOLVED\]

**URL:** <https://community.particle.io/t/udp-parsepacket-returns-0-when-system-mode-set-to-semi-automatic-manual-solved/6317>\
**Category:** Troubleshooting\
**Created:** [August 11, 2014, 6:17am UTC](https://community.particle.io/t/udp-parsepacket-returns-0-when-system-mode-set-to-semi-automatic-manual-solved/6317 "2014-08-11T06:17:05Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![krvarma](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/krvarma/32/3089_2.png) [@krvarma](https://community.particle.io/u/krvarma)\
**Post date:** [August 11, 2014, 6:17am UTC](https://community.particle.io/t/udp-parsepacket-returns-0-when-system-mode-set-to-semi-automatic-manual-solved/6317/1 "2014-08-11T06:17:05Z")

</div>

When I set SYSTEM\_MODE to SEMI\_AUTOMATIC or MANUAL, the UDP.parsePacket() always returns 0. When I check WiFi.ready() it returns _true_, this means it is connected to WiFi and has an IP assigned. I am sending the UDP packet from the same network, so even if Spark Core is not connect to Cloud, the UDP should work, is it? But it is not when I set the SYSTEM\_MODE other than AUTOMATIC. What I want is that my Spark Core does not want to connect to Cloud but it should receive UDP packets from local network.

My actual code is not this but, for debugging purpose I am using the following code from Spark Core documentation.

```
#include "application.h"

SYSTEM_MODE(SEMI_AUTOMATIC);

// UDP Port used for two way communication
unsigned int localPort = 8888;

// An UDP instance to let us send and receive packets over UDP
UDP Udp;

void setup() {
	// start the UDP
	Udp.begin(localPort);

	// Print your device IP Address via serial
	Serial.begin(9600);
}

void loop() {
	int packets = Udp.parsePacket();

	Serial.print("Parse Packet returned ");
	Serial.print(packets);
	Serial.print(", WiFi Status ");
	Serial.println(WiFi.ready());

	// Check if data has been received
	if (packets > 0) {

		// Read first char of data received
		char c = Udp.read();

		// Ignore other chars
		Udp.flush();

		// Store sender ip and port
		IPAddress ipAddress = Udp.remoteIP();
		int port = Udp.remotePort();

		// Echo back data to sender
		Udp.beginPacket(ipAddress, port);
		Udp.write(c);
		Udp.endPacket();
	}
}

```

I added Serial.print for debugging purpose, am I missing something here?

**UPDATE #1:** The code is working when you move UDP.begin() inside the **loop()** method. Following code works, I should have tried this before posting here ☹

```
if(WiFi.ready() && !isInitialized ){
	// start the UDP
	Udp.begin(localPort);

	isInitialized = true;
}

```

**UPDATE #2:** For MANUAL mode, you should call `WiFi.connect()`
