Arduino Forum

Using Arduino => Networking, Protocols, and Devices => Topic started by: Mark81 on Jan 18, 2019, 06:32 pm

Title: UDP parsePacket() doesn't work after send()
Post by: Mark81 on Jan 18, 2019, 06:32 pm
It seems that after sending an UDP packet I can't receive others anymore.
Here my code:

Code: [Select]

#include <LwIP.h>
#include <STM32Ethernet.h>
#include <EthernetUdp.h>

#include <OSCMessage.h>
#include <OSCBundle.h>
#include <OSCData.h>
#include <Servo.h>

byte mac[] = { /* ... */ };
byte ip[] = { 192, 168, 0, 222 };
const unsigned int port = 9999;
byte outIp[] = { 192, 168, 0, 250 };
const unsigned int outPort = 9999;

EthernetUDP Udp;
OSCErrorCode error;
long previousMillis = 0; 

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

  Ethernet.begin(mac, ip);
  Udp.begin(port);
}

void led_handler(OSCMessage &msg, int patternOffset)
{
  // do something
  Serial.println("received!");
}

void sendBundle(int ch)
{
  OSCBundle bundle;
  char address[32];

  sprintf(address, "/led/%u", ch + 1);
  bundle.add(address).add((int32_t) 1).add((float) 0.05); 
  Udp.beginPacket(outIp, outPort);
  bundle.send(Udp);
  Udp.endPacket();

  Serial.println("sent!");
}

void processOSCMessage(void)
{
  OSCBundle bundle;
  int size = Udp.parsePacket();
 
  if (size > 0)
  {
    while (size--) bundle.fill(Udp.read()); 
    if (bundle.hasError())
    {
      error = bundle.getError();
      Serial.print("error: ");
      Serial.println(error);
    }
    else
    {
      bundle.route("/led", led_handler);
    }
  }
}


void loop()
{
  processOSCMessage();

  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > 5000) {
    previousMillis = currentMillis;   
    sendBundle(1);
  }

}


Every 5 s it sends out the UDP packet, but it receives the incoming packets only before sending out the first one.
Am I missing anything obvious?
Title: Re: UDP parsePacket() doesn't work after send()
Post by: pylon on Jan 18, 2019, 07:08 pm
Don't you think it might be an error in the STM32 implementation only?

Code: [Select]
#include <STM32Ethernet.h>
Title: Re: UDP parsePacket() doesn't work after send()
Post by: Mark81 on Jan 18, 2019, 08:17 pm
I don't know... that's why I asked here :)
You are aware of such a problem? Or, do you see any error in my code?
Title: Re: UDP parsePacket() doesn't work after send()
Post by: pylon on Jan 22, 2019, 07:38 pm
Quote
I don't know... that's why I asked here
STM32 is not a broadly used platform on the Arduino IDE.

Quote
Or, do you see any error in my code?
Code: [Select]
byte outIp[] = { 192, 168, 0, 250 };

  Udp.beginPacket(outIp, outPort);



beginPacket() accepts either an IPAddress object or a character array (which your argument is) but in that case it expects a DNS name. Your value is neither the string of an IP address nor a DNS name (which couldn't be resolved anyway as the DNS server is missing).

That might be a problem but I cannot see why it really sends a correct packet but stops receiving them. Might be some interaction with the try to contact the DNS server. Try with correct types and post results.