# Get Device Name returning garbage

**URL:** <https://community.particle.io/t/get-device-name-returning-garbage/37056>\
**Category:** Troubleshooting\
**Created:** [October 30, 2017, 10:51pm UTC](https://community.particle.io/t/get-device-name-returning-garbage/37056 "2017-10-30T22:51:18Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![BulldogLowell](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bulldoglowell/32/4305_2.png) [@BulldogLowell](https://community.particle.io/u/BulldogLowell)\
**Post date:** [November 1, 2017, 10:36pm UTC](https://community.particle.io/t/get-device-name-returning-garbage/37056/22 "2017-11-01T22:36:02Z")

</div>

> [@twospoons](#):
>
> Serial.println("received " + String(topic) + ": " + String(data));

I think the String concatenation was f'ing up your data...

try this:

```cpp
//char deviceName[32] = "unknown";
String deviceName = "unknown";

void setup()
{
  Serial.begin(9600);
  Particle.subscribe("particle/device/name", getName, MY_DEVICES);
  Particle.variable("DeviceName", deviceName);
}

void loop()
{
}

void getName(const char *event, const char *data)
{
  Serial.printlnf("received %s: %s", event, data);
  //strcpy(deviceName, data); // also works with char array
  deviceName = data;
}

```

---

<div class="post-metadata">

**Author:** ![twospoons](https://avatars.discourse-cdn.com/v4/letter/t/bbce88/32.png) [@twospoons](https://community.particle.io/u/twospoons)\
**Post date:** [November 2, 2017, 1:58am UTC](https://community.particle.io/t/get-device-name-returning-garbage/37056/23 "2017-11-02T01:58:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Viscacha](https://avatars.discourse-cdn.com/v4/letter/v/919ad9/32.png) [@Viscacha](https://community.particle.io/u/Viscacha)\
**Post date:** [November 2, 2017, 8:11am UTC](https://community.particle.io/t/get-device-name-returning-garbage/37056/24 "2017-11-02T08:11:30Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![BulldogLowell](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bulldoglowell/32/4305_2.png) [@BulldogLowell](https://community.particle.io/u/BulldogLowell)\
**Post date:** [November 2, 2017, 10:21am UTC](https://community.particle.io/t/get-device-name-returning-garbage/37056/25 "2017-11-02T10:21:02Z")

</div>

> [@twospoons](#):
>
> 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.

No, I mean I could duplicate your issue even with a 20 second startup delay.

```cpp
//char myName[32] = "unknown";
String myName = "unknown";

void setup()
{
  pinMode(D7, OUTPUT);
  Serial.begin(9600);
  Particle.subscribe("particle/device/name", getName, MY_DEVICES);
  Particle.variable("DeviceName", myName);
  uint32_t now = millis();
  while (millis() - now < 20000)
    Particle.process();
  digitalWrite(D7, HIGH);
}

void loop()
{

}

void getName(const char *topic, const char *data) {
  Particle.publish("received " + String(topic) + ": " + String(data)); //<<<<<< . NOTE THIS SMALL CHANGE
  //Serial.printlnf("received %s: %s", topic, data);
  myName=String(data);
}

/*void getName(const char *event, const char *data)
{
  //strcpy(myName, data); // works with char array
  myName = data;
}*/

```

with this happening:

```bash
event: particle/device/name
data: {"data":"testing","ttl":60,"published_at":"2017-11-02T10:02:01.867Z","coreid":"api"}

event: received particle/device/name: testing
data: {"data":"null","ttl":60,"published_at":"2017-11-02T10:02:01.885Z","coreid":"280041000d47353136383631"}

```

note the null in the data returned on the publish. and this I see in my Variable:

```bash
��W.���Ӓ��Y��n5��肷�3.����Þĩ�|xaS0���%=��Z)���tL�>��E����'�'�P��_n��~'֨� v�ˣ�����+�O&����Q��H(pZ�WF�ԟqh�c�U�RY�;$d`�W�\���

```

Removing your string handling (as with the code I posted earlier)... it then works **with or without the delay**.

it is strange, maybe some other folks can test the code posted here...

---

<div class="post-metadata">

**Author:** ![twospoons](https://avatars.discourse-cdn.com/v4/letter/t/bbce88/32.png) [@twospoons](https://community.particle.io/u/twospoons)\
**Post date:** [November 2, 2017, 9:15pm UTC](https://community.particle.io/t/get-device-name-returning-garbage/37056/26 "2017-11-02T21:15:06Z")

</div>

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 …

---

<div class="post-metadata">

**Author:** ![BulldogLowell](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bulldoglowell/32/4305_2.png) [@BulldogLowell](https://community.particle.io/u/BulldogLowell)\
**Post date:** [November 2, 2017, 9:54pm UTC](https://community.particle.io/t/get-device-name-returning-garbage/37056/27 "2017-11-02T21:54:45Z")

</div>

> [@twospoons](#):
>
> I have another idea to try ...

**_maybe two_**...

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.

> [@twospoons](#):
>
> I believe there is only one buffer for all events?

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.

---

<div class="post-metadata">

**Author:** ![twospoons](https://avatars.discourse-cdn.com/v4/letter/t/bbce88/32.png) [@twospoons](https://community.particle.io/u/twospoons)\
**Post date:** [November 2, 2017, 10:54pm UTC](https://community.particle.io/t/get-device-name-returning-garbage/37056/28 "2017-11-02T22:54:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![superpanda](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@superpanda](https://community.particle.io/u/superpanda)\
**Post date:** [December 13, 2017, 5:14pm UTC](https://community.particle.io/t/get-device-name-returning-garbage/37056/29 "2017-12-13T17:14:18Z")

</div>

For me it is 100% of the time I get garbage. I am using the Redbear DUO if that makes a difference, but at this point I can not get the device name.

---

<div class="post-metadata">

**Author:** ![Vitesze](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/vitesze/32/15427_2.png) [@Vitesze](https://community.particle.io/u/Vitesze)\
**Post date:** [December 13, 2017, 11:35pm UTC](https://community.particle.io/t/get-device-name-returning-garbage/37056/30 "2017-12-13T23:35:47Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [December 14, 2017, 8:28am UTC](https://community.particle.io/t/get-device-name-returning-garbage/37056/31 "2017-12-14T08:28:48Z")

</div>

If you are using Web IDE can you post a snapshot-link?  
Otherwise post your full code?

For me it works 100% of the time - even with Duo. So it’s not the system as such 😉

[Previous page](https://community.particle.io/t/get-device-name-returning-garbage/37056.md?page=1)
