Hi Horace,
I have printed the remote IP as you show, and I too get the correct IP of the sending device.
Why when I use packet.print(....) does it send to (0.0.0.0) and not the correct remote IP?
Jim
This is the sender
void broadcastMsg(String message)
{
repeatLastMessage.cancel(); // Stop any pending repeats of previous message
#ifdef DEBUG_PRINT
Serial.printf("Sending Message %s, length=%d (Repeats = %d)\n",message.c_str(), message.length(),repeatCount);
#endif
uint16_t length = message.length()+1;
uint8_t buffer[length+2]; // +1 cos its 0 based, +2 for the terminator
message.getBytes(buffer,length);
buffer[length+2] = '\0';
//This initializes udp and transfer buffer
txUdp.beginPacket(txIp, txPort);
txUdp.write(buffer, length+1);
txUdp.endPacket();
memset(buffer, 0, length+2);
}
and this is the receiver...
void setup()
{
WiFi.disconnect(true);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid,password);
//wifiConnect();
if(rxUdp.listen(RX_PORT))
{
Serial.printf("Listening for UDP on Port %04d, IP:",RX_PORT);
Serial.print(WiFi.localIP());
Serial.print(" MAC: ");
Serial.println(WiFi.macAddress());
rxUdp.onPacket([](AsyncUDPPacket packet)
{
Serial.print("UDP Packet Type: ");
Serial.print(packet.isBroadcast()?"Broadcast":packet.isMulticast()?"Multicast":"Unicast");
Serial.print(", From: ");
Serial.print(packet.remoteIP().toString().c_str());
Serial.print(":");
Serial.print(packet.localPort());
Serial.print(", To: ");
Serial.print(packet.localIP());
Serial.print(":");
Serial.print(packet.localPort());
Serial.print(", Length: ");
Serial.print(packet.length());
//Serial.print(", Data: ");
//Serial.write(packet.data(), packet.length());
Serial.println();
char* tempStr = (char*) malloc (packet.length()+1);
memcpy (tempStr, packet.data(), packet.length());
tempStr[packet.length()] = '\0';
String message = String(tempStr);
free(tempStr);
// Serial.println(message);
handleRxUdp(message);
if (millis() >= (lastMs + REPLY_MS))
{
packet.printf("Hello from %s\n",WiFi.macAddress().c_str());
lastMs = millis();
}
});
}
It is the line packet.printf("....") that is sending to IP 0,0,0,0
I also wrote an alternate sending funtction...
void sendUDP(String message, IPAddress sendIP)
{
Serial.printf("Sending Message %s, length=%d to IP %s\n",message.c_str(), message.length(),sendIP.toString().c_str());
uint16_t length = message.length()+1;
uint8_t buffer[length+2]; // +1 cos its 0 based, +2 for the terminator
message.getBytes(buffer,length);
buffer[length+2] = '\0';
//This initializes udp and transfer buffer
if(txUdp.beginPacket(sendIP.toString().c_str(), txPort)==0)
Serial.println("Error on beginPacket");
if(txUdp.write(buffer, length+1)==0)
Serial.println("Error on writePacket");
if(txUdp.endPacket()==0)
Serial.println("Error on endPacket");
memset(buffer, 0, length+2);
}
but this results in no message being sent from the receiver at all.