So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By erik@palsbo.com
#66878 I need to listen for udp.multicast messages. this works!
As long as the multicast messages ar comming in, I also need to listen to another udp port abd cinnybucate via this (simplified):

Code: Select all
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>

const char* ssid = "....";  //  your network SSID (name)
const char* pass = "......";       // your network password
byte packetBuffer[512]; //buffer to hold incoming and outgoing packets

WiFiUDP Udp;
WiFiUDP udp;

unsigned long timer;
bool status = true;
bool oldStatus = status;

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, pass);
  Serial.print("[Connecting]");
  Serial.print(ssid);
  int tries = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    tries++;
    if (tries > 30) {
      break;
    }
  }
  Serial.println();
  Serial.println("Connected to wifi");
  Serial.println(WiFi.localIP());
  Serial.println("Starting multicast");
  Udp.beginMulticast(WiFi.localIP(),  IPAddress(239, 255, 1, 1), 49707);
  //  Starting udp
  //udp.begin(49000L);  //  introducing this, cause multicast to stop listening (sometime after while) - why?
}

void loop() {
  if (millis() > timer) {
    status = false;
  }
  int noBytes = Udp.parsePacket();
  if ( noBytes ) {
    Udp.read(packetBuffer, noBytes); // read the packet into the buffer
    status = true;
    timer = millis() + 4000L;
  }
  if (oldStatus == status) return;

  //  here when status change!
  oldStatus = status;
  Serial.printf("%6d Status: %s\n", round(millis()/1000), status ? "online" : "offline");
  if (status) {
    //  now online - start using udp for communication on port 49000
  } else {
    //  now offline - wait for online again.
  }
}


The last line in setup() (udp.begin(49000L);) will cause multicast to stop listening for multicast messages.
Otherwise the loop will run forewer!

What am I doing worng?
Is there a way to solve the problem?

Tnx in advance.
Erik