Post topics, source code that relate to the Arduino Platform

User avatar
By dannybackx
#48147 I've been trying and can't get this to work. This works with unicast, not multicast.
Am I missing something obvious ?

Danny

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <mywifi.h>

void printWifiStatus();

int status = WL_IDLE_STATUS;
int keyIndex = 0; // your network key Index number (needed only for WEP)
unsigned int port = 1900; // local port to listen on
WiFiUDP Udp;

IPAddress multi(239, 255, 255, 250);
IPAddress uni(192, 168, 1, 102);

void loop1(), loop2();

void loop() {
loop2();
Serial.print(".");
delay(2000);
}

void loop1() {
Udp.beginPacket(uni, port);
Udp.write("this is a unicast packet");
Udp.endPacket();
}

void loop2() {
IPAddress local = WiFi.localIP();
Udp.beginPacketMulticast(multi, port, local);
Udp.write("this is a multicast packet");
Udp.endPacket();
}

void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

Serial.print("Starting WiFi... ");
WiFi.mode(WIFI_STA);

int wifi_tries = 3;
while (wifi_tries-- >= 0) {
WiFi.begin(MY_SSID, MY_WIFI_PASSWORD);
status = WiFi.waitForConnectResult();
if (status == WL_CONNECTED)
break;
Serial.printf(" %d ", wifi_tries + 1);
}

Serial.println("Connected to wifi");
printWifiStatus();

Serial.println("\nStarting connection to server...");
Udp.begin(port);
}

void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}
User avatar
By Barnabybear
#48150 Hi, your code for multicast looks good. This works for me:
Code: Select all      for (int j = 0; j < ((sizeof(Scene1)-1)/512); j++) {
    IP_Multicast_Out[3] = Universe_Numbers[j];
    Udp.beginPacketMulticast(IP_Multicast_Out, Port_Multicast_Out, WiFi.localIP());
    Udp.write(Packet_Header1, 111);
    Udp.write(Packet_Uni_Number [j]++);
    Udp.write(Zero);
    Udp.write(Universe_Number_MSB);
    Udp.write(Universe_Numbers[j]);
    Udp.write(Packet_Header2, 11);   
        for (int i = 0 + (512*j); i < (512 * (j + 1)); i++){
    Udp.write(pgm_read_byte_near(Scene1 + i));
        }     
    Udp.endPacket();
    delay(10);
  }

I havent been able to send two or more packets back to back without a delay between them or they fail.
Code: Select alldelay(10)

is enough to cure the problem.

I would stick a 'Serial.print' into 'void loop2()' just to ensure the loop is being called correctly.
User avatar
By dannybackx
#48208 I did more digging and it appears that I got it wrong. My app on one ESP8266 does successfully transmit those multicast messages, the app on another ESP doesn't receive them.

It does receive such messages from other devices on my network though, but I'm experimenting with UPnP and some devices send a mixture of multicast and unicast messages. This confused me for a while.

As it turns out, I didn't set my socket in one of the apps to receive multicast messages.

Thanks for helping out !

Danny