#include <WiFi.h>
#include <WiFiUdp.h>
const char* ssid = "********";
const char* password = "********";
WiFiUDP Udp;
unsigned int localUdpPort = 4210; // local port to listen on
char incomingPacket[255]; // buffer for incoming packets
char replyPacket[] = "Hi there! Got the message :-)"; // a reply string to send back
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");
Udp.begin(localUdpPort);
Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
}
void loop()
{
int packetSize = Udp.parsePacket();
if (packetSize)
{
// receive incoming UDP packets
Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
int len = Udp.read(incomingPacket, 255);
if (len > 0)
{
incomingPacket[len] = 0;
}
Serial.printf("UDP packet contents: %s\n", incomingPacket);
// send back a reply, to the IP address and port we got the packet from
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(replyPacket); <====
Udp.endPacket();
}
}
But can't compile.
Error message: invalid conversion from 'char*' to 'uint8_t {aka unsigned char}' [-fpermissive] Udp.write(replyPacket);[/i] Please help.
lesept:
In the library WiFiUdp, the argument to write is supposed to be uint8_t:
// Write a single byte into the packet
virtual size_t write(uint8_t);
Maybe you shoud try something like:
int i = 0;
while (replyPacket[i] != 0) Udp.write((uint8_t)replyPacket[i++]);
(not tested)
Edit the code:
#include <WiFi.h>
#include <WiFiUdp.h>
const char* ssid = "********";
const char* password = "********";
WiFiUDP Udp;
unsigned int localUdpPort = 4210; // local port to listen on
char incomingPacket[255]; // buffer for incoming packets
char replyPacket[] = "Hi there! Got the message :-)"; // a reply string to send back
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");
Udp.begin(localUdpPort);
Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
}
void loop()
{
int packetSize = Udp.parsePacket();
if (packetSize)
{
// receive incoming UDP packets
Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
int len = Udp.read(incomingPacket, 255);
if (len > 0)
{
incomingPacket[len] = 0;
}
Serial.printf("UDP packet contents: %s\n", incomingPacket);
// send back a reply, to the IP address and port we got the packet from
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
int i = 0;
while (replyPacket[i] != 0)
Udp.write((uint8_t)replyPacket[i++]);
Udp.endPacket();
}
}
Found other solution for this issue which much more simple from here
In ESP32 Arduino package the WiFiUdp.h doesn't pull in all Print class write functions. As consequence the WiFiUdp class doesn't know write(const char *str). Use Udp.print(message); if message is a zero-terminated char array or Udp.write(message, size); if message is a byte array
Instead of using:
int i = 0;
while (replyPacket[i] != 0) Udp.write((uint8_t)replyPacket[i++]);
Also could use:
Udp.print(message);
Hope this could help anyone who facing same issue like me...