Two instances of WiFiUDP

JPMJPM
Posts: 25
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 ?.

Code: Untitled.c Select all


WiFiUDP Udp;
IPAddress grupo_multicast(224, 1, 1, 10);
int udpport = 6000;
char datain[100];

WiFiUDP Udp_video;
IPAddress grupo_multicast_video(224, 1, 1, 1);
int udpport_video = 6000;
char datain_video[1460];

void setup() {

Udp.beginMulticast(grupo_multicast, udpport);
Udp_video.beginMulticast(grupo_multicast_video, udpport_video);

}

void loop() {

if(Udp_video.parsePacket()){
int len_video = Udp_video.read(datain_video, 1460);
if (len_video > 0){
datain_video[len_video] = 0;
Serial.printf("RXUDP_VIDEO: %s\n", datain_video);
}
}

if(Udp.parsePacket()){
int len = Udp.read(datain, 100);
if (len > 0){
datain[len] = 0;
Serial.printf("RXUDP: %s\n", datain);
}
}

}
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: 25
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: 25
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: 1131
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: 25
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: 25
Joined: Fri Oct 13, 2017 4:35 pm

Re: Two instances of WiFiUDP

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

This works.

Code: Untitled.c Select all


#include "WiFi.h"
#include "AsyncUDP.h"
#include "Arduino.h"

const char * ssid = "xxxxxxxxx";
const char * password = "yyyyyyyyyyyyy";

AsyncUDP udp;

IPAddress multicast_address_1 = IPAddress(224, 1, 1, 1);
uint16_t udp_port_1 = 6000;
IPAddress multicast_address_2 = IPAddress(224, 1, 1, 10);
uint16_t udp_port_2 = 6000;

void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("WiFi Failed");
while(1) {
delay(1000);
}
}
Serial.println("WiFi OK");

udp.listenMulticast(multicast_address_1, udp_port_1);
udp.listenMulticast(multicast_address_2, udp_port_2);

udp.onPacket([](AsyncUDPPacket packet) {
if (packet.isMulticast() & packet.localIP() == multicast_address_1) {
Serial.printf("RX1: %s\n", packet.data());
char *datatx1 = "HELLO 1";
udp.writeTo((uint8_t *)datatx1, strlen(datatx1), multicast_address_1, udp_port_1);
}
if (packet.isMulticast() & packet.localIP() == multicast_address_2) {
Serial.printf("RX2: %s\n", packet.data());
char *datatx2 = "HELLO 2";
udp.writeTo((uint8_t *)datatx2, strlen(datatx2), multicast_address_2, udp_port_2);
}
});
}

void loop()
{
delay(10);
}

lbernstone
Posts: 1131
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: Google [Bot], PerplexityBot and 8 guests