Page 1 of 1
Two instances of WiFiUDP
Posted: Wed Nov 23, 2022 10:45 am
by JPMJPM
Hi,
Is it possible create two instances of WiFiUDP with different multicast address ?.
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.
Re: Two instances of WiFiUDP
Posted: Wed Nov 23, 2022 12:19 pm
by FRANCISCO2020
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];
Re: Two instances of WiFiUDP
Posted: Wed Nov 23, 2022 1:01 pm
by JPMJPM
Thanks but there is a compile error. IP address must be IPAddress type.
From WiFiUdp.h -> uint8_t beginMulticast(IPAddress a, uint16_t p);
Re: Two instances of WiFiUDP
Posted: Wed Nov 23, 2022 4:27 pm
by FRANCISCO2020
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
Re: Two instances of WiFiUDP
Posted: Wed Nov 23, 2022 6:34 pm
by JPMJPM
Yes, one at a time works perfect.
Yes, the wifi connection is ok.
Re: Two instances of WiFiUDP
Posted: Wed Nov 23, 2022 11:54 pm
by lbernstone
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.
Re: Two instances of WiFiUDP
Posted: Thu Nov 24, 2022 9:54 am
by JPMJPM
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
Re: Two instances of WiFiUDP
Posted: Fri Nov 25, 2022 11:43 am
by JPMJPM
This works.
#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);
}
Re: Two instances of WiFiUDP
Posted: Fri Nov 25, 2022 9:52 pm
by lbernstone
Thank you for posting your solution.