Two instances of WiFiUDP

JPMJPM
Posts: 23
Joined: Fri Oct 13, 2017 4:35 pm

Two instances of WiFiUDP

Postby JPMJPM » Wed Nov 23, 2022 10:45 am

Hi,

Is it possible create two instances of WiFiUDP with different multicast address ?.
  1. WiFiUDP Udp;
  2. IPAddress grupo_multicast(224, 1, 1, 10);
  3. int udpport = 6000;
  4. char datain[100];
  5.  
  6. WiFiUDP Udp_video;
  7. IPAddress grupo_multicast_video(224, 1, 1, 1);
  8. int udpport_video = 6000;
  9. char datain_video[1460];
  10.  
  11. void setup() {
  12.  
  13. Udp.beginMulticast(grupo_multicast, udpport);
  14. Udp_video.beginMulticast(grupo_multicast_video, udpport_video);
  15.  
  16. }
  17.  
  18. void loop() {
  19.  
  20.   if(Udp_video.parsePacket()){
  21.     int len_video = Udp_video.read(datain_video, 1460);
  22.     if (len_video > 0){
  23.       datain_video[len_video] = 0;
  24.       Serial.printf("RXUDP_VIDEO: %s\n", datain_video);      
  25.     }
  26.   }
  27.  
  28.   if(Udp.parsePacket()){
  29.     int len = Udp.read(datain, 100);
  30.     if (len > 0){
  31.       datain[len] = 0;
  32.       Serial.printf("RXUDP: %s\n", datain);
  33.       }
  34.     }
  35.    
  36. }
When ESP32 receive something from 224.1.1.10 prints "RXUDP: something" and "RXUDP_VIDEO: something"

Any help ? Thanks.

FRANCISCO2020
Posts: 21
Joined: Mon Sep 20, 2021 9:13 am

Re: Two instances of WiFiUDP

Postby FRANCISCO2020 » Wed Nov 23, 2022 12:19 pm

HAS TRIED..

Code: Select all

WiFiUDP Udp;
grupo_multicast= '224,1,1,10';
udpport = 6000;
char datain[100];
 
WiFiUDP Udp_video;
grupo_multicast_video= '224,1,1,1';
udpport_video = 6000;
char datain_video[1460];


JPMJPM
Posts: 23
Joined: Fri Oct 13, 2017 4:35 pm

Re: Two instances of WiFiUDP

Postby JPMJPM » Wed Nov 23, 2022 1:01 pm

Thanks but there is a compile error. IP address must be IPAddress type.
From WiFiUdp.h -> uint8_t beginMulticast(IPAddress a, uint16_t p);

FRANCISCO2020
Posts: 21
Joined: Mon Sep 20, 2021 9:13 am

Re: Two instances of WiFiUDP

Postby FRANCISCO2020 » Wed Nov 23, 2022 4:27 pm

OK, yes it's wrong.
Have you tried one at a time?

I assume that the wifi connection to the router and internet is working

JPMJPM
Posts: 23
Joined: Fri Oct 13, 2017 4:35 pm

Re: Two instances of WiFiUDP

Postby JPMJPM » Wed Nov 23, 2022 6:34 pm

Yes, one at a time works perfect.
Yes, the wifi connection is ok.

lbernstone
Posts: 828
Joined: Mon Jul 22, 2019 3:20 pm

Re: Two instances of WiFiUDP

Postby lbernstone » Wed Nov 23, 2022 11:54 pm

If you enable verbose logging (Tools->Core Debug Level->Verbose), I suspect you will see that it is unable to bind the second address to the service, and it should give you some reason why.
Take a look at AsyncUDP (https://github.com/espressif/arduino-es ... AsyncUDP.h). I think it has a slightly more sophisticated approach to multicast and should be able to handle listening to a couple addresses/ports with a single object.

JPMJPM
Posts: 23
Joined: Fri Oct 13, 2017 4:35 pm

Re: Two instances of WiFiUDP

Postby JPMJPM » Thu Nov 24, 2022 9:54 am

With "verbose logging" activated I can see something related to WiFi connection, nothing more.

About AsyncUDP I have tested two AsyncUDP instances and one AsyncUDP instance with two listenMulticast()
without any success.

I don't know what else I can do :(

JPMJPM
Posts: 23
Joined: Fri Oct 13, 2017 4:35 pm

Re: Two instances of WiFiUDP

Postby JPMJPM » Fri Nov 25, 2022 11:43 am

This works.

  1. #include "WiFi.h"
  2. #include "AsyncUDP.h"
  3. #include "Arduino.h"
  4.  
  5. const char * ssid = "xxxxxxxxx";
  6. const char * password = "yyyyyyyyyyyyy";
  7.  
  8. AsyncUDP udp;
  9.  
  10. IPAddress multicast_address_1 = IPAddress(224, 1, 1, 1);
  11. uint16_t udp_port_1 = 6000;
  12. IPAddress multicast_address_2 = IPAddress(224, 1, 1, 10);
  13. uint16_t udp_port_2 = 6000;
  14.  
  15. void setup() {
  16.     Serial.begin(115200);
  17.     WiFi.mode(WIFI_STA);
  18.     WiFi.begin(ssid, password);
  19.     if (WiFi.waitForConnectResult() != WL_CONNECTED) {
  20.         Serial.println("WiFi Failed");
  21.         while(1) {
  22.             delay(1000);
  23.         }
  24.     }
  25.     Serial.println("WiFi OK");
  26.  
  27.     udp.listenMulticast(multicast_address_1, udp_port_1);
  28.     udp.listenMulticast(multicast_address_2, udp_port_2);
  29.  
  30.     udp.onPacket([](AsyncUDPPacket packet) {
  31.       if (packet.isMulticast() & packet.localIP() == multicast_address_1) {
  32.         Serial.printf("RX1: %s\n", packet.data());
  33.         char *datatx1 = "HELLO 1";
  34.         udp.writeTo((uint8_t *)datatx1, strlen(datatx1), multicast_address_1, udp_port_1);
  35.       }
  36.       if (packet.isMulticast() & packet.localIP() == multicast_address_2) {
  37.         Serial.printf("RX2: %s\n", packet.data());
  38.         char *datatx2 = "HELLO 2";
  39.         udp.writeTo((uint8_t *)datatx2, strlen(datatx2), multicast_address_2, udp_port_2);
  40.       }
  41.     });
  42. }
  43.  
  44. void loop()
  45. {
  46.     delay(10);
  47. }

lbernstone
Posts: 828
Joined: Mon Jul 22, 2019 3:20 pm

Re: Two instances of WiFiUDP

Postby lbernstone » Fri Nov 25, 2022 9:52 pm

Thank you for posting your solution.

Who is online

Users browsing this forum: No registered users and 39 guests