1. Why equals the len of myData 56
2. myData.p points to a string. When length of string varies, why does len of myData is constant ?
Hi Henk.
I don’t know.
I need to see your particular example to understand what might be wrong.
Can you share more details?
Regards,
Sara
@Sara
ESP32 Sender Sketch (ESP-NOW)
=============================
This is the definition of myData as in the original
typedef struct struct_message {
char a[32];
int b;
float c;
String d;
bool e;
} struct_message;
ESP32 Receiver Sketch (ESP-NOW)
===============================
This is the definition of myData as in the original
typedef struct struct_message {
char a[32];
int b;
float c;
String d;
bool e;
} struct_message;
Now it works like this:
=======================
When I send:
strcpy(myData.a, “THIS IS A CHAR”);
myData.b = random(1,20);
myData.c = 1.2;
myData.d = “Hello”;
myData.e = false;
I receive this
Bytes received: 56
Char: THIS IS A CHAR
Int: 6
Float: 1.20
String: Hello
Bool: 0
Now I change in Sender only the payload:
<myData.d = “Hello”;> into <myData.d = “Hello_1234”;>
I receive this
Bytes received: 56
Char: THIS IS A CHAR
Int: 6
Float: 1.20
String: Hello_1234
Bool: 0
I can’t figure it out how this is possible.
the string is lenghtenend by 5 chars, len does not change.
@Sara
It is from course “Learn ESP32 with Arduino IDE”,
module 8, Unit 1 ESP-NOW: Getting Started
ESP32 Sender Sketch (ESP-NOW)
and
ESP32 Receiver Sketch (ESP-NOW)
Hi.
I’m sorry, but I don’t know why that happens.
Is that a critical point for some project that you’re trying to develop?
Regards,
Sara
Hello Sara,
No no, its not very important. I do this course and I want understand.
That’s all
Thanks.
Hi.
I’ve experimented in several different ways and I think it has to do with the String format itself.
I think when you try to send a string inside a structure it reserves a certain space for the string that is fixed. If you try to send a bigger string, you’ll see that it won’t receive it properly. And if you change the size of the string, the structure size will be the same.
However, if you change the size of the char variable inside the structure, you’ll see that it changes size.
I don’t know why this happens. But, we should avoid sending string variables and use char instead.
Regards,
Sara