If I sent my data in this way:
float rx_val[]={1.1,2.2,3.3,4.4}; //Serial.read();
Serial.print("udp_send: ");
for(int i=0;i<4;i++){
Serial.println(rx_val[i]);
udp.beginPacket(udpAddress,udpPort);
udp.write(uint8_t(rx_val[i]));
udp.endPacket();
delay(1000);
}
How can receiving the data by using udp.read(char( )
because udp.read use just char
Udp.read(packetBuffer, MaxSize);
Parameters:
packetBuffer
: buffer to hold incoming packets (char)
MaxSize
: maximum size of the buffer (int)
Change to:
uint8_t * dataPtr = (uint8_t *) &rx_val[i];
udp.write(dataPtr[0]);
udp.write(dataPtr[1]);
udp.write(dataPtr[2]);
udp.write(dataPtr[3]);
Then when you read the packet you can put the bytes back into a float:
float val;
uint8_t * dataPtr = (uint8_t *) &val;
Udp.read(packetBuffer, MaxSize);
dataPtr[0] = packetBuffer[0];
dataPtr[1] = packetBuffer[1];
dataPtr[2] = packetBuffer[2];
dataPtr[3] = packetBuffer[3];
Can send rx_val as a one packet? how can do that ?
Do you mean "Send all four values in rx_val[] in one packet?" That should work if the packet buffer is large enough:
uint8_t * dataPtr = (uint8_t *) &rx_val[0];
for (i = 0; i < sizeof rx_val; i++)
udp.write(dataPtr[i]);
float val[4];
uint8_t * dataPtr = (uint8_t *) &val;
Udp.read(packetBuffer, MaxSize);
for (i = 0; i < sizeof val; i++)
dataPtr[i] = packetBuffer[i];
Yes, send all the array as a packet. Is this mean send one value after another is that right ?
udp.write(dataPtr[i]);
What about
packetBuffer
?
I cant send and receive anything!!
sender
float rx_val[]={1.1,2.2,3.3,4.4};
uint8_t * dataPtr = (uint8_t *) &rx_val[0];
for (int i = 0; i < sizeof rx_val; i++){
Serial.println(dataPtr[i]);
udp.write(dataPtr[i]);
udp.endPacket();
delay(1000);}
Receiver
float val[4];
uint8_t * dataPtr = (uint8_t *) &val;
udp.read(dataPtr, 4);
for (int i = 0; i < sizeof val; i++){
val[i] = dataPtr[i];
Serial.println(val[i]);}
delay(1000);
That means add one byte to the packet. The udp.endPacket();
sends them all.
It looks like you are trying the read a 16-byte packet but you don't have a 16-byte buffer. Try this:
float val[4];
byte packetBuffer[sizeof val];
int MaxSize = sizeof packerBuffer;
Udp.read(packetBuffer, MaxSize);
uint8_t * dataPtr = (uint8_t *) &val;
for (i = 0; i < sizeof val; i++)
dataPtr[i] = packetBuffer[i];
If I use
float rx_val[]={1.1,2.2,3.3,4.4};
uint8_t * dataPtr = (uint8_t *) &rx_val[0];
for (int i = 0; i < sizeof rx_val; i++){
Serial.println(rx_val[i]);
udp.write(dataPtr[i]);
udp.endPacket();
delay(1000);}
I got on this output
1.10
2.20
3.30
4.40
1.97
1.96
0.00
0.00
0.00
1.96
2.21
0.00
0.00
1.97
0.00
0.00
and I can not receive anything. so
I update on the sender to be like this:
byte rx_val[]={1.1,2.2,3.3,4.4}; //Serial.read();
Serial.print("udp_send: ");
for(int i=0;i<4;i++){
Serial.println(rx_val[i]);
udp.beginPacket(udpAddress,udpPort);
udp.write(rx_val,4);
udp.endPacket();
delay(1000);
and used this code as receiver
float val[4];
byte packetBuffer[sizeof val];
int MaxSize = sizeof packerBuffer;
Udp.read(packetBuffer, MaxSize);
uint8_t * dataPtr = (uint8_t *) &val;
for (i = 0; i < sizeof val; i++)
dataPtr[i] = packetBuffer[i];
But the problems
1- I can not send float it sent just 1 ,2,3,4
2- the output like this
1
2
3
4
0
0
0
0
0
0
0
0
0
0
0
0
You should at least mention which devices / MCU's you are using.. Try this:
//Transmitter
float data[] = {1.1, 2.2, 3.3, 4.4};
Udp.beginPacket();
Udp.write((uint8_t*)data, sizeof(float) * 4);
Udp.endPacket();
//Receiver
int ps = Udp.parsePacket();
if (ps == (sizeof(float) * 4))
{
float data[4];
Udp.read((uint8_t*)data, ps);
Serial.print("Received:");
for (uint8_t i = 0; i < 4; i++)
{
Serial.print(' ');
Serial.print(data[i]);
}
Serial.println();
}
else if (ps > 0)
{
Serial.print("Wrong packet size: ");
Serial.println(ps);
}
The same problem I can not send or receive anything!
When I print ps
I get on zero.
Problem must be in what you did not provide: Info about MCU's, wiring, the rest of the code and so on..
I use esp32 now.
Transmitter
#include <WiFi.h>
#include <WiFiUdp.h>
const char* ssid = ""; //your wifi ssid
const char* password = "";//your wifi password
const char * udpAddress = "192.168.1.100";
WiFiUDP udp;
unsigned int udpPort=1234; // Server port
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
Serial.println(WiFi.localIP());
Serial.println("Status: Connected");
/* Enable udp */
udp.begin(udpPort);
Serial.println(udpPort);
}
void loop() {
float data[] = {1.1, 2.2, 3.3, 4.4};
Serial.print("udp_send: ");
Serial.println(data[i]);
udp.beginPacket(udpAddress,udpPort);
udp.write((uint8_t*)data, sizeof(float) * 4);
udp.endPacket();
delay(1000);
}
Receiver
#include <WiFi.h>
#include <WiFiUdp.h>
const char* ssid = ""; //your wifi ssid
const char* password = "";//your wifi password
const char * udpAddress = "192.168.1.109"; // IP for esp32
WiFiUDP udp;
unsigned int udpPort=1234; //udp port to connect
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
Serial.println(WiFi.localIP());
Serial.println("Status: Connected");
/* Enable udp */
udp.begin(udpPort);
Serial.println(udpPort);
}
void loop() {
int ps = udp.parsePacket();
Serial.println(ps);
if (ps == (sizeof(float) * 4))
{
float data[4];
udp.read((uint8_t*)data, ps);
Serial.print("Received:");
for (uint8_t i = 0; i < 4; i++)
{
Serial.print(' ');
Serial.print(data[i]);
}
Serial.println();
}
else if (ps > 0)
{
Serial.print("Wrong packet size: ");
Serial.println(ps);
}
else{ Serial.print("ZERO packet size: ");
}
}
My guess is that the transmitter is sending to a wrong IP-address. In the receiver, packet size of zero bytes means no incoming packet which is not an error.
I did not change the IP address
Which IP-address is printed when the receiver is booted? In the transmitter try to modify this:
if (udp.beginPacket(udpAddress,udpPort))
{
udp.write((uint8_t*)data, sizeof(float) * 4);
udp.endPacket();
}
else Serial.println("Unable to begin packet");
If you get the "unable" message, then you are trying to send to a unresolveable address.
system
Closed
March 24, 2023, 12:14pm
16
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.