0

I am trying to send measurements over UDP, but the function Udp.endPacket continuously fails.

I use an Arduino Uno WiFi rev 2, WiFiNINA library version 1.4.0 and WiFiNINA firmware version 1.2.1 which seems to be the latest available.

WiFi connection setup works, obtaining a correct IP address in the 192.168.3.0/24 network.

This is a snippet of my code:

const char* udpDestIP = "192.168.3.126";
const int udpDestPort = 1844;
WiFiUDP Udp;

void loop() {
  // establish variables for distance result:
  long cm;

  // execute measurement
  cm = measureDistance();

  // check for false measurements and repeat if necessary
  if(minDist < cm && cm < maxDist) {
    Serial.print(cm);
    Serial.print(" cm");
    Serial.println();

    // Check WiFi connection and reconnect if necessary
    if (WiFi.status() != WL_CONNECTED) {
      connectToWifi();
    }

    // Send over UDP
    //if(!Udp.beginPacket("192.168.3.255", udpDestPort)) {
    if(!Udp.beginPacket(udpDestIP, udpDestPort)) {
      Serial.println("beginPacket fail");
    }
    String msg = "water_distance:";
    msg += cm;
    byte size = Udp.write(msg.c_str(), msg.length());
    if(DEBUG){
      Serial.print("Sending this message over UDP: ");
      Serial.println(msg);
      Serial.print("udp write: ");
      Serial.println(size);
    }
    if(!Udp.endPacket()) Serial.println("endPacket fail");

    delay(delayTime);
  }
}

Terminal output is as follows:

151 cm
Sending this message over UDP: water_distance:151
udp write: 18
endPacket fail
151 cm
Sending this message over UDP: water_distance:151
udp write: 18
endPacket fail
33 cm
Sending this message over UDP: water_distance:33
udp write: 17
endPacket fail
34 cm
Sending this message over UDP: water_distance:34
udp write: 17
endPacket fail
153 cm
Sending this message over UDP: water_distance:153
udp write: 18
endPacket fail

Which makes me conclude the beginPacket, and udp.write functions are executed correctly, but I see no reason why the endPacket function keeps failing. Does anyone have an idea? Thanks a lot.

5
  • add Udp.begin() to setup(). if it works I write an answer with explanation
    – Juraj
    Aug 7, 2019 at 8:19
  • This works, thanks a lot! As UDP is a connectionless protocol, is there any reason why one should open a port to listen for incoming UDP packets while the device is only acting as a client emitting UDP fragments?
    – PeterD
    Aug 7, 2019 at 9:54
  • The UDP packets have to come from somewhere. They don't just appear from nowhere. Like a faucet has to exist and be turned on for the water to come out of it.
    – Majenko
    Aug 7, 2019 at 10:03
  • The UDP.begin() function requires a source port as argument. For just sending UDP packets, it doesn't matter from which port the packets are coming, the OS can just select a random unused port. Just like it's done in Python or Java. Is there a way to make UDP.begin() use a random unused port as source?
    – PeterD
    Aug 7, 2019 at 10:24
  • UDP.begin(random(1024, 65536));
    – Majenko
    Aug 7, 2019 at 10:41

1 Answer 1

1

The Arduino Ethernet and WiFi library implementations require to specify the outgoing port with UDP.begin(port) before calling UDP.beginPacket().

But UDP.begin(port) starts listening for UDP messages on this port too, so it is not a clean API. Call UDP.end() to stop listening for incoming messages.

2
  • Top answer, thanks.
    – PeterD
    Aug 7, 2019 at 11:37
  • You can accept the answer to mark the question solved
    – Juraj
    Aug 7, 2019 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.