Get Device Name returning garbage

I’ve pretty much copy/pasted the code example into my code.I thought I had this working, but today I find my test device returning crap as the device name:
eg

received particle/device/name: ¯ã½xOx©ïðfk^>Ÿv2tÓCÛÀ_ìZ®„gEV"…Y_GìF—%„ÊRù©ª³_Ý«¸Â~ªZgQ{˳mÒað𭯊ÅüÙZ¹R,%Éá_(7uvó^ Ê8++Hnì

from

void getName(const char *topic, const char *data) {
Serial.println("received " + String(topic) + ": " + String(data));
myName=String(data);

}

At the moment I only call

void reqName(void)

{
if (waitFor(Particle.connected, 30000)) {

Particle.publish("particle/device/name");

}

after a reset or power on event.

Any ideas?

Apart from the fact that I’d always recommend to stay away from String() try this instead

Serial.printlnf("received %s: %s", topic, data);

Nope. Same garbage.

received particle/device/name: 2I±lùDL±ˆd5³Xx£ÇB)x,?FõõòGXžh«/K]Ûå%Ë¿}Ç!¹Õ?¿ã„Î t¹˜î–‚¡I†Yå©Aw”cyÂûP®!ª=­¼.VYbé-°Fzúìî›Úö
ÆÅb—B*)b[v±WÑŸï÷²þ‚–V”N4öå„Ïþ)…AMûëKÕ_N
Ü”{醙RT¬K€¡(¯H´ßš³±·Eíî’Q¼ k…ŒI÷ß~[Bàb‡r QÈ‚(Œ±SFP[YÙnË~’R]á:Ìè|¥óŸ)Ž
received particle/device/name: 2I±lùDL±ˆd5³Xx£ÇB)x,?FõõòGXžh«/K]Ûå%Ë¿}Ç!¹Õ?¿ã„Î t¹˜î–‚¡I†Yå©Aw”cyÂûP®!ª=­¼.VYbé-°Fzúìî›Úö
ÆÅb—B*)b[v±WÑŸï÷²þ‚–V”N4öå„Ïþ)…AMûëKÕ_N
Ü”{醙RT¬K€¡(¯H´ßš³±·Eíî’Q¼ k…ŒI÷ß~[Bàb‡r QÈ‚(Œ±SFP[YÙnË~’R]á:Ìè|¥óŸ)Ž

This is the code:

void getName(const char *topic, const char *data) {
Serial.println("received " + String(topic) + ": " + String(data));
Serial.printlnf("received %s: %s", topic, data);
myName=String(data);

}

I have pretty much the same issue, although the returned value differs a bit, and subsequent calls do usually return the correct device name. Hope someone comes up with a solution here though

I can’t reproduce your problem using your code. I always get the correct name. What version of the firmware is on your device?

0.6.2
Its odd, because I’m sure this worked when I first wrote it.

Well this is interesting. Ive made a test version that does the Get Name every minute. The first request after a reset returns garbage, but subsequent requests return the correct name. So I guess the workaround is to do the request twice and ignore the first response.

Still seems strange that the first response is rubbish.

Even more interesting - its not the first message after reset, its the first message after connecting to particle cloud thats corrupted.

tested by doing this:

void reqName(void)

{
Particle.disconnect();
delay(1000);
Particle.connect();
if (waitFor(Particle.connected, 30000))
{

Particle.publish("particle/device/name");
}

}

garbage every time.

Exactly my issue as well. First request 50% of the time is faulty, but the second one 99% of the time is correct. Are you using a 3rd party simcard by any chance?

With my Particle simcard things worked fine, but I got one from Rogers (Canada; better deal on larger data volumes) and the issue only pertains to when I use Rogers sims.

No this is on a Photon via wifi. No sim card involved

It seems to be time dependent.
If I hit Get Name twice just after connecting to particle I get the same garbage twice. If I wait 20 seconds after connecting, everything is OK. 10 seconds is not enough. Didn’t bother hunting for the threshold timing.

So for some reason the event handler can be triggered before the firmware has a valid name from Particle, at which point it just serves up whatever crap happens to be in the buffer. This is looking like a firmware bug to me.

I should add I have threading enabled - maybe thats having unexpected consequences?

I think you are just seeing a race condition with the Cloud not starting up fully before you ask for the name. There are many similar case, such as the Cloud time setting. Can you try waiting for Time.isValid() to be true before asking?

https://docs.particle.io/reference/firmware/photon/#isvalid-

I'm sure you are correct but I don't think that's going to work in my case because:

Used to check if current time is valid. This function will return true if:
Time has been set manually using Time.setTime()
Time has been successfully synchronized with the Particle Cloud. The device synchronizes time with the Particle Cloud during the handshake. The application may also manually synchronize time with Particle Cloud using Particle.syncTime()
Correct time has been maintained by RTC. See information on Backup RAM (SRAM) for cases when RTC retains the time. RTC is part of the backup domain and retains its counters under the same conditions as Backup RAM.

I have a backup battery running the RTC.

Particle.syncTimePending() might work though

OK, this works:

void reqName(void)

{

if (waitFor(Particle.connected, 30000))
{
while(Particle.syncTimePending())
  {
    Particle.process();
  }

Particle.publish("particle/device/name");

}

}

Bugger. Above is not reliable.
Back to 20 sec wait.

Hi,

Just out of interest, what is the length of the device name?

I have seen this before when there is an overflow and you are using String.

The device name is only 8 characters long, so an overflow is rather unlikely.

maybe post your entire code; a bare minimum example of what exhibits your issue.

Perhaps there is another cause in there that is contributing to your problem.

Ha. No - its 2700 lines long, and commercially sensitive. I’ve already posted the relevant bits.

To me, it looks like something is stepping on "device name" memory address. If you take it down to its bare minimum, you will at least eliminate the possibility that it is organic to your code snippets above (which it appears not to be).

something else is likely then to be corrupting this data...