Calculate Broadcast IP function?

My core needs to send several UDP packets on the Broadcast IP whenever the core connects to the Wifi Router. So far I’ve just blindly replaced the 4th byte of my IP address with 255. Works great almost always.

I know that this is a wrong way to do it and if your subnet is not 255.255.255.0 then you’re likely to end up in trouble. I’m might have to write a library myself to calculate the Broadcast IP using my IP address and Subnet-mask I get from Network .localIP() and Network.subnetMask(). There’s a lot of things to consider and I’m new to subnet-masks etc. I’d really appreciate it if someone already has a library out there for such a thing and maybe Spark could integrate a getBroadcastIP() function since it is generally useful.

Also I have a doubt.

  • In a scenario where there are two routers, Router-1 & Router-2
  • Router-2 is plugged into Router-1 via ethernet cable (Router-1 is the parent router)
  • A Sparkcore is connected to Router-2 via Wifi
  • Is it possible for a UDP packet sent on a Broadcast IP from the Sparkcore on Router-2 to reach another Sparkcore/PC which is connected to Router-1?