Jump to content
43oh

Recommended Posts

My subnet is 255.255.255.0 and my broadcast IP is 192.168.1.255. I'm trying to broadcast UDP packets using this code but I'm not sure its working. When I open my IP and port on SocketTest, I don't see anything on the console. What am I missing?

unsigned int localPort = 2390;
char sendBuffer[] = {"pullingyourhairoutisnotthesolution"};

WiFiUDP BroadcastSendUDP;

void sendBroadcastPacket() { 
  IPAddress sendIP(192, 168, 1, 101);

  int counter = (sizeof(sendBuffer)/sizeof(sendBuffer[0]));

  BroadcastSendUDP.beginPacket(sendIP, localPort);
  for (int i=0; i<counter; i++) {
    BroadcastSendUDP.write(sendBuffer[i]);
  }
  BroadcastSendUDP.endPacket();

  Serial.print("Packet sent: ");
  Serial.println(sendBuffer);
  Serial.println();
}
Link to post
Share on other sites

Gotta see more code to really know what's going on. 
 
Do you have your UDP object properly initialized? something like:

do { // Detect if we've started the UDP server
    Serial.print("."); // print dots while we wait
    delay(300);
    }
    while(!BroadcastSendUDP.begin(localPort));

Notice that BroadcastSendUDP.beginPacket() will *not* get a socket and bind it to a port for you, you must call begin() first.

 

Relevant snippet for reference:

int WiFiUDP::beginPacket(IPAddress ip, uint16_t port)
{
    //
    //make sure a port has been created
    //!! this doesn't create a port if one doesn't exist. Is that ok? <--------- Look here
    //
    if (_socketIndex == NO_SOCKET_AVAIL) {
        return 0;
    }

...
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...