electrotwelve 3 Posted June 8, 2016 Share Posted June 8, 2016 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(); } Quote Link to post Share on other sites
SmokinGrunts 5 Posted June 10, 2016 Share Posted June 10, 2016 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; } ... Fmilburn and energia 2 Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.