Hello,
I was trying to send whatever is received on serial port by udp, but it doesn't work.
here is my code of sending from ESP8266
void loop()
{ char tosend[20];
String IncomingValues = Serial.readString();
IncomingValues.toCharArray(tosend, 20);
wifiMulti.run();
int packetSize = Udp.parsePacket();
// send back a reply, to the IP address and port we got the packet from
Udp.beginPacket(gateway, 3333);
Udp.write(tosend);
Serial.println(tosend);
//Udp.write("tosend");
Udp.endPacket();
delay(250);
}
Just as a test, set tosend[19] =0 just before the Udp.write() call.
You will probably find that the problem is your use of the toCharArray() method is not appropriate here and, if you are sure you are handling less than your maximum, 20 chars in this case, then the c_str() method will do what you want.
Something like: Udp.write( IncomingValues.c_str() ) ;
There are methods which don’t require the use of the String class which would be quite simple to implement in this case. Someone else will surely suggest this!
idir93:
Hi, thanks but why does it work with a UDP send/receive app on my phone and not on the ESP32?
Still didn't work with the changes you suggested!
You modified your code, but didn't post it. You send the ESP32 some text but you don't confirm that it got what you sent. Why not? Do you really like debugging by guesswork? I do not.
Hi,
This portion of code works fine with the ESP32 (to test UDP)
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WiFiUdp.h>
char replyPacket[] = "Hi there! Got the message :-)";
WiFiUDP Udp;
unsigned int localUdpPort = 5300; // local port to listen on
char incomingPacket[255]; // buffer for incoming packets
ESP8266WiFiMulti wifiMulti;
IPAddress staticIP(192,168,1,22);
IPAddress gateway(192,168,1,100);
IPAddress gateway1(192,168,43,1);
IPAddress subnet(255,255,255,0);
void setup()
{
WiFi.mode(WIFI_STA);
WiFi.begin("MICROTECH-E941", "AB000000");
//WiFi.begin("Dicipline", "1234567890");
Serial.begin(115200);
Serial.setDebugOutput(true);
Serial.println();
wifiMulti.addAP("MICROTECH-E941", "AB000000");
//wifiMulti.addAP("Dicipline", "1234567890");
Udp.begin(localUdpPort);
Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
Serial.println(WiFi.localIP());
}
void loop()
{ char tosend[20];
String IncomingValues = Serial.readString();
IncomingValues.toCharArray(tosend, 20);
wifiMulti.run();
int packetSize = Udp.parsePacket();
// send back a reply, to the IP address and port we got the packet from
Udp.beginPacket(gateway, 3333);
//Udp.beginPacket(gateway1, 3333);
Udp.print( tosend) ;
//Udp.write(tosend);
//Serial.println(tosend);
// Udp.write("2222@3333@4444");
Udp.endPacket();
delay(250);
}
and this portion of code works fine to test sending received serial data via UDP to phone
void loop()
{ char tosend[20];
String IncomingValues = Serial.readString();
IncomingValues.toCharArray(tosend, 20);
wifiMulti.run();
int packetSize = Udp.parsePacket();
// send back a reply, to the IP address and port we got the packet from
Udp.beginPacket(gateway, 3333);
//Udp.beginPacket(gateway1, 3333);
Udp.print( tosend) ;
//Udp.write(tosend);
//Serial.println(tosend);
// Udp.write("2222@3333@4444");
Udp.endPacket();
delay(250);
}
You have now mentioned a phone, an ESP8266 and an ESP32.
Have I got this correct: You can successfully send UDP packets from an ESP8266 to your phone. You cannot, however, send UDP packets from an ESP32 to your phone ?
It is not really clear from your various code snippets where they are running and what their target is.
Going back to the problem you mentioned at the beginning:
This line of code has a good chance of working : Udp.write("tosend");
a) because it is a well formed cstring literal which will automatically have a null terminating character to mark its end.
b) because its value does not have to be fetched from somewhere like your Udp.write(tosend) which attempts to get a value indirectly from Serial.readString();
Incidentally, Serial.readString() times out after 1 second (by default) so there may be a good chance that at the time it executed, no data was available. The first thing to check with debug statements is if this actually worked.
Further, the Udp.write(tosend) statement requires a length in bytes e.g. Udp.write(tosend, 20) otherwise it assumes the argument is one byte long.
6v6gt:
You have now mentioned a phone, an ESP8266 and an ESP32.
Have I got this correct: You can successfully send UDP packets from an ESP8266 to your phone. You cannot, however, send UDP packets from an ESP32 to your phone ?
It is not really clear from your various code snippets where they are running and what their target is.
Going back to the problem you mentioned at the beginning:
This line of code has a good chance of working : Udp.write("tosend");
a) because it is a well formed cstring literal which will automatically have a null terminating character to mark its end.
b) because its value does not have to be fetched from somewhere like your Udp.write(tosend) which attempts to get a value indirectly from Serial.readString();
Incidentally, Serial.readString() times out after 1 second (by default) so there may be a good chance that at the time it executed, no data was available. The first thing to check with debug statements is if this actually worked.
Further, the Udp.write(tosend) statement requires a length in bytes e.g. Udp.write(tosend, 20) otherwise it assumes the argument is one byte long.
Thanks a lot for the help. Yes you got it write!
Here is the deal: the Udp.write("tosend") works with both receivers (phone and ESP32) however the Udp.write(tosend) works with only my phone.
Now, why would the Serial.readString() be the problem since it has worked with my phone in the first place? just asking to understand.
Another things, I am very new to this, what do you mean by debug statement?
Regards,
You can see the WiFiUdp.write() method documented here:
// Write a single byte into the packet
virtual size_t write(uint8_t) =0;
// Write size bytes from buffer into the packet
virtual size_t write(const uint8_t *buffer, size_t size) =0;
You can give it a single byte or a pointer to a byte array (buffer). If you give it a byte array, you must also give the amount of bytes you want to copy into the packet.