You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am attempting to dynamically set up the display name using the homespan.begin(...) function. However, when I pass the c_str() of a String object into the begin function, it seems to affect the character pointer, resulting in the display name being corrupted. I assume that the begin() function should not modify the display name. This functionality was working correctly in version 1.8.
This is example code:
char nameSuffix[20];
sprintf(nameSuffix, "%06d", 101);
String displayName = "My Device Number " + String(nameSuffix);
...
homeSpan.begin(Category::Lighting, displayName.c_str());
The output looks something like this:
Accessory configuration number: 1
l��? is READY!
Trying to connect to XXXXXX_XXXXXX. Waiting 1 sec...
Trying to connect to XXXXXX_XXXXXX. Waiting 2 sec...
E (7391) wifi:sta is connecting, return error
WiFi Connected! IP Address = xxx.21.31.205
Starting MDNS...
HostName: HomeSpan-CAE729DD642F.local:80
Display Name: l��?
Model Name: HomeSpan-ESP32
Setup ID: HSPN
Passing a constant string into begin() call works perfectly fine.
homeSpan.begin(Category::Lighting, "My Device 101");
My Device 101 is READY!
Trying to connect to XXXXXX_XXXXXX. Waiting 1 sec...
WiFi Connected! IP Address = xxx.21.31.205
Starting MDNS...
HostName: HomeSpan-CAE729DD642F.local:80
Display Name: My Device 101
Model Name: HomeSpan-ESP32
Setup ID: HSPN
Thank you for the amazing library!
The text was updated successfully, but these errors were encountered:
This is because the homeSpan.begin() function does not copy the full character array for any names passed to it (though I probably should do that in a future update). It only copies the pointers.
This means that if String is declared within setup(), it will go out of scope once the setup() function is completed by the time homeSpan.poll() is called within the main loop(), and the pointer to displayName will likely no longer be valid. Depending on the sketch, the memory occupied by displayName may or may not still contain a valid character array. If this worked in 1.8.0, it was only by luck.
The workaround is to define your String as a global variable instead of within setup so the name is retained and can be used once homeSpan.poll() is called. I'll see about reserving space to copy the full string in a future update.
This means that if String is declared within setup(), it will go out of scope once the setup() function is completed by the time homeSpan.poll() is called within the main loop(), and the pointer to displayName will likely no longer be valid. Depending on the sketch, the memory occupied by displayName may or may not still contain a valid character array. If this worked in 1.8.0, it was only by luck.
Thank you for the clarification. I've been using the same pattern for both serial numbers and pairing codes. I will transfer all related data to a global scope until a permanent fix is implemented.
I am attempting to dynamically set up the display name using the
homespan.begin(...)
function. However, when I pass thec_str()
of a String object into thebegin
function, it seems to affect the character pointer, resulting in the display name being corrupted. I assume that thebegin()
function should not modify the display name. This functionality was working correctly in version 1.8.This is example code:
The output looks something like this:
Passing a constant string into
begin()
call works perfectly fine.Thank you for the amazing library!
The text was updated successfully, but these errors were encountered: