[Solved/Workaround] Spark Core can't send UDP broadcast packets without cloud connection

@bko So I’ve run both yours and @laml example programs, and both work fine. So we know it’s capable of sending multicast packets.

What I haven’t been able to achieve is sending multicast UDP packets while the cloud is disabled, as in SYSTEM_MODE(MANUAL);
I won’t have an internet connection on the network 1000m up.

I don’t know much about the firmware itself, but I suspect one of these things to be occuring.

  1. Some weird connection between cloud code and multicast
  2. Some garbage collection or process call that’s disabled in MANUAL mode
  3. Something weird with the Spark.process call.

I’ve tried testing 3 by adding tons of the calls as in the above post. I’ve also sent the same packet to the router on the network in manual mode, and received responses. So that just leaves 1.

Something in the UDP library specifically relating to multicast only works when Automatic mode is enabled. I think it’s a bug

Here’s my latest code. Change the IP address to your router’s IP address, put in some credentials, and you’re off to the races. Packets sent and received. Comment that IP address line, uncomment the one above for SSDP. Zilch.
Load @laml’s example above, with the same code with cloud enabled. Packets in and out.

Can anyone shed some light on why that would be so?

SYSTEM_MODE(MANUAL);
// UDP Port used for two way communication
unsigned int localPort = 1900;
#define MAX_SIZE 1024
char buffer[MAX_SIZE];

unsigned int once = 0;

unsigned int ssdpport = 1900;
//This is the multicast broadcast IP
// IPAddress ip( 239, 255, 255, 250 );

//This is my router's ip address
IPAddress ip( 192, 168, 8, 1 );
// An UDP instance to let us send and receive packets over UDP
UDP Udp;

void setup() {
    Spark.disconnect();
    Serial.begin(9600);
    WiFi.on();
    // Connects to a network with a specified authentication procedure.
    // Options are WPA2, WPA, or WEP.
    WiFi.clearCredentials();
    WiFi.setCredentials("deathstarplans", "notinthemaincomputer", WPA);
    WiFi.connect();
    // while(!Serial.available()) Spark.process();

    while (!WiFi.ready()) {
        Serial.println("Waiting for WiFi...");
        Spark.process();
        delay(1000);
    }
}

void loop() {
    Spark.process();
    if ((!once) && (WiFi.ready() == true)) {
       IPAddress addr = WiFi.localIP();

      if ((addr[0] != 0 ||addr[1] != 0 || addr[2] != 0 || addr[3] != 0) && (once == 0)) {
     Serial.println("UDP has begun");
        once = 1;
        Udp.begin(localPort);
        Spark.process();
        Serial.println(WiFi.localIP());
         //Sends SSDP Packet to multicast address
        Udp.beginPacket(ip, ssdpport);
        Spark.process();
        Udp.write("M-SEARCH * HTTP/1.1\r\nHOST: 239.255.255.250:1900\r\nMAN: \"ssdp:discover\"\r\nMX: 5\r\nST: ssdp:all\r\n\r\n");
        Spark.process();
        Udp.endPacket();
        Spark.process();
       }
      

       
    }
    //Module to parse received SSDP packet
    Spark.process();
    int rcvd = Udp.parsePacket();
    if (rcvd > 0) {
        Serial.print("Received UPD packet, length: ");
        Serial.println(rcvd);
        Serial.println(buffer);
        // Read first char of data received
        Udp.read(buffer, MAX_SIZE);
            Spark.process();
        if (rcvd > MAX_SIZE) {
            Serial.println("Too large packet");
            while (Udp.available())
                Udp.read();
                  Spark.process();
        }
    }
}
1 Like