If you go back and read the entire thread you will see I have already tried your suggestion and it did not fix the issue.
A 20 second wait after connecting to the cloud does fix the issue.
Its nothing to do with string handling. Its clearly a race condition with the cloud data.
If you read threads on other things like getting your external IP you would find the same thing, you cannot read values for these things as soon as Particle.connected is true but you can speed things up a bit by running Particle.process() before trying…
Yet I tried it with your code for the string handling and it failed without the delay.
Just once I saw it return the same data as another event my device subscribes to, which makes it look like the event handler can trigger before the buffer has been filled with the correct data. I believe there is only one buffer for all events?
By the way, you do know you are not supposed to try to Publish inside an event handler for a subscription? Saw that in another thread somewhere, and its due to buffer re-use.
FFS it shouldn’t be this hard to make such a simple request work.
I have another idea to try …
Make sure that your Particle.subscribe() is at the very top of your setup() function, well ahead of anything like your first call to publish().
You want to make sure you are registering the cloud services before anything else. I've seen unexpected things like what you are experiencing... if I wait too long to do that cloud setup.
how many subscribe() functions do you have? Another method would be to create only one handler for all of your responses to cloud events.
you can see by the code I posted, without anything else interfering, it works as expected.
I have just two subscribes, and they are at the start. I do have a lot of variables and functions registered, which is slowing things down . I can get rid of most of them, as they were only needed in the early stages of the project.
Going back to this method (with a small extra delay) - I’m not keen on relying on absolute time delays.
void reqName(void)
{
if (waitFor(Particle.connected, 30000))
{
while(Particle.syncTimePending())
{
Particle.process();
}
delay(1000); //small extra delay
Particle.publish("particle/device/name");
}
}
I still think it should be up to the system firmware to ensure valid data for a documented system call, or at least provide a flag if valid data is not available.
To be fair, I just moved away from device names completely and work with the Device ID instead. A very long number that doesn’t make it easy to analyze data manually, but at least it’s reliable…