UDP Connection while only connected to the WiFi (Not the cloud)

I’m trying to communicate with the Photon over a local WiFi network but I seem to be unsuccessful at it. To take a step back from the local network, I am using my normal Wi-Fi but I am keeping the photon in the SEMI_AUTOMATIC mode and only connecting to the Wi-Fi. Doing this, I get a successful connection (“Breathing Green” LED and I also checked the IP address through Serial connection and the WiFi.localIP() command) but neither my outgoing nor incoming UDP packets get through. Below is just a UDP communication code that I used to troubleshoot this issue(mainly the code copied from the particle firmware reference page). Any help/pointers would be appreciated!

SYSTEM_MODE(SEMI_AUTOMATIC);

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);

 // Use LED for troubleshooting
  pinMode(D7,OUTPUT);

  if (!WiFi.ready()) {
    Particle.process();
    WiFi.connect();
    while(WiFi.connecting()) {Particle.process();}
  }

  Serial.begin(9600);
  Serial.println(WiFi.localIP());
}

void loop() {

  if (Udp.parsePacket() > 0) {
digitalWrite(D7,!digitalRead(D7));
   
 // Read first char of data received
    int c = Udp.read();

    // Ignore other chars
    while(Udp.available())
      Udp.read();

    Serial.println(c);  
      digitalWrite(D7,HIGH);
      
  }
  
 char c='A';
 
   IPAddress ipAddress =;
   int port = ;

   Udp.beginPacket(ipAddress, port);
   Udp.write(c);
   Udp.endPacket();
  }

The bit starting with the “char c=‘A’” was just me trying to receive some sort of a UDP packet.

Can anyone see a reason why this wouldn’t be working? The second I connect to the cloud, the UDP comm works

Hi @gio926

There were a bunch of UDP problems fixed in 0.4.2 about 18 months ago, including one similar to this. Other than that, I have not seen UDP problems.

Which version of the system firmware are you running?

You could also check your router logs and make sure it is passing port 8888 on your local network.

Here’s a simple test program for receiving in Wi-Fi only mode:

#include "Particle.h"

SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);

const size_t UDP_BUFFER_SIZE = 513;
const int UDP_PORT = 7123;

UDP udp;
uint8_t udpBuffer[UDP_BUFFER_SIZE];

void setup() {
	Serial.begin(9600);

	WiFi.connect();
	waitUntil(WiFi.ready);

	udp.begin(UDP_PORT);

	Serial.printlnf("server=%s:%d", WiFi.localIP().toString().c_str(), UDP_PORT);
}

void loop() {
	// receivePacket() is preferable to parsePacket()/read() because it saves a buffering step but more importantly
	// it return an error code if an error occurs! This is important to reinitialize the listener on error.
	int count = udp.receivePacket(udpBuffer, UDP_BUFFER_SIZE - 1);
	if (count > 0) {
		udpBuffer[count] = 0;
		IPAddress remoteAddr = udp.remoteIP();

		Serial.printlnf("## packet size=%d addr=%s data=%s", count, remoteAddr.toString().c_str(), udpBuffer);
	}
	else
	if (count == 0) {
		// No packet
	}
	else {
		Serial.printlnf("** receivePacket error %d", count);
		udp.begin(UDP_PORT);
	}
}

I sent data at it using:

nc -u 192.168.1.44 7123

I tested with 0.6.0. I prefer the udp.receivePacket and udp.sendPacket functions because the are more efficient and more reliable. Also, if you are in threaded mode, make sure WiFi is ready before you call udp.begin(port). If you listen before WiFi.ready() is true, then the listen won’t work.

2 Likes

@bko @rickkas7

I got it to work, thanks a lot! I tested with the code that rickkas7 posted and it did the trick. The issue was calling Udp.begin before connecting to the WiFi as specified above. I didn’t pay attention to this before as you can see in my original comment. I will look into changing to receivePacket in my original application.

Once again, many thanks!

1 Like