# Atypical Particle.subcribe Handling On 1.5.4-rc.1 for Device Name Functionality

**URL:** <https://community.particle.io/t/atypical-particle-subcribe-handling-on-1-5-4-rc-1-for-device-name-functionality/57135>\
**Category:** Firmware\
**Created:** [July 24, 2020, 8:08pm UTC](https://community.particle.io/t/atypical-particle-subcribe-handling-on-1-5-4-rc-1-for-device-name-functionality/57135 "2020-07-24T20:08:43Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![hansA](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/hansa/32/24722_2.png) [@hansA](https://community.particle.io/u/hansA)\
**Post date:** [July 24, 2020, 8:08pm UTC](https://community.particle.io/t/atypical-particle-subcribe-handling-on-1-5-4-rc-1-for-device-name-functionality/57135/1 "2020-07-24T20:08:43Z")

</div>

Hello,

I’m trying to track down a menacing bug in my code when trying to call a Particle.subscribe() handler. I want to send my device name in a JSON payload to parse with a firebase DB. In order to do that I’m using the code provided in the reference files of the particle website. Here is the referenced stock code:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/6/c/6c15ea82dd66424d9c00a2375456fb3e845421ae.png)

I now am going to paste the code I have on my device, I have it split up because its referenced in different areas of the code so I’m snipping here for clarity:  
Here is the setup of the subscribe command and the publish command at the beginning of my code

```auto
void setup() {
    Particle.subscribe("particle/device/name", deviceNameHandler, MY_DEVICES);
    Particle.publish("admin","particle/device/name", MY_DEVICES);

```

Here is another part of my code where I issue the publish command to call the handler to check if the name has changed (it only does this once)

```auto
if (deviceNameFlag == false ) {
            Particle.publish("admin","particle/device/name", MY_DEVICES);
            deviceNameFlag = true;    

```

Here is the handler function I have outside of the loop():

```auto
void deviceNameHandler(const char *topic, const char *data) {
	deviceName = data;
    Particle.publish("debug2", "device Handler Called");
}

```

The weird part is that I got it working last night but now its not working at all. I double-checked everything and its the same as it was last night.  
This is what I’m seeing In my Event Viewer:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/3/1/31e49069976a0a038331dbd38f8b9d337c980245.png)

---

<div class="post-metadata">

**Author:** ![armor](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/armor/32/18398_2.png) [@armor](https://community.particle.io/u/armor)\
**Post date:** [July 24, 2020, 8:17pm UTC](https://community.particle.io/t/atypical-particle-subcribe-handling-on-1-5-4-rc-1-for-device-name-functionality/57135/2 "2020-07-24T20:17:24Z")

</div>

Could you share the code that formats the Report\_Payload event. Also, how do you define deviceName? If deviceName is a String you can’t just use equal operator to copy it from a const char\* type.

---

<div class="post-metadata">

**Author:** ![hansA](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/hansa/32/24722_2.png) [@hansA](https://community.particle.io/u/hansA)\
**Post date:** [July 24, 2020, 8:24pm UTC](https://community.particle.io/t/atypical-particle-subcribe-handling-on-1-5-4-rc-1-for-device-name-functionality/57135/3 "2020-07-24T20:24:22Z")

</div>

Sure: this is the payload function, I’m using a JsonParserGeneratorRK to parse the information for me in the first part and I’m also sending it outside as part of me debugging. I was also having this problem without the JsonParserGeneratorRK being used so I don’t think its related to the library:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/d/d/dda0f7d3ed8991d1e46f097ac75f1c9be5f40829.png)

---

<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:** [July 24, 2020, 8:37pm UTC](https://community.particle.io/t/atypical-particle-subcribe-handling-on-1-5-4-rc-1-for-device-name-functionality/57135/4 "2020-07-24T20:37:18Z")

</div>

> [@hansA](#):
>
> `Particle.publish("admin","particle/device/name", MY_DEVICES);`

This will _ **not** _ trigger the `Particle.subscribe("particle/device/name", ...)` handler.  
You need to publish like this

```cpp
Particle.publish("particle/device/name", PRIVATE);

```

_(for `particle/...` events the scope is irrelevant but in general you need to publish as `PRIVATE` when subscribing for `MY_DEVICES` and `PUBLIC` when subscribing for `All_DEVICES`)_

You subscribe to the _event name_ (first parameter) _ **not** _ to the payload (second string parameter).

---

<div class="post-metadata">

**Author:** ![armor](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/armor/32/18398_2.png) [@armor](https://community.particle.io/u/armor)\
**Post date:** [July 24, 2020, 8:39pm UTC](https://community.particle.io/t/atypical-particle-subcribe-handling-on-1-5-4-rc-1-for-device-name-functionality/57135/5 "2020-07-24T20:39:58Z")

</div>

How are you defining deviceName?

---

<div class="post-metadata">

**Author:** ![nrobinson2000](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/nrobinson2000/32/15624_2.png) [@nrobinson2000](https://community.particle.io/u/nrobinson2000)\
**Post date:** [July 24, 2020, 9:00pm UTC](https://community.particle.io/t/atypical-particle-subcribe-handling-on-1-5-4-rc-1-for-device-name-functionality/57135/6 "2020-07-24T21:00:06Z")

</div>

I see at least two possible bugs here.

First, you are publishing to the `"admin"` topic, with `"particle/device/name"` as the data. In the docs it says to publish to the `"particle/device/name"` topic with no data. Are you sure this was working last night?

```cpp
Particle.publish("admin","particle/device/name", MY_DEVICES);

```

@ScruffR beat me to it on this one.

Second, in `deviceNameHandler` the following line may have unexpected results:

```cpp
deviceName = data;

```

Since `data` is a pointer to a char array, when you set `deviceName` to the same pointer, the data at that address likely changes before you dereference `deviceName` later in your application.

As @armor asked, how have you defined `deviceName` in your code? In order for your code to work as expected, it should be a char array. (If it was a `String` object the compiler likely gave you a warning.)

For example:

```cpp
char deviceName[25]; // 24 chars + null terminator

```

In order to copy the contents of `data` into `deviceName` you need to use a function like [`strncpy`](http://www.cplusplus.com/reference/cstring/strncpy/).

```cpp
strncpy(deviceName, data, sizeof(deviceName);

```

If `deviceName` and `data` are of different lengths you will need to set the null terminator manually in `deviceName`.

For example:

```cpp
deviceName[24] = '\0';

```

---

<div class="post-metadata">

**Author:** ![hansA](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/hansa/32/24722_2.png) [@hansA](https://community.particle.io/u/hansA)\
**Post date:** [July 24, 2020, 9:22pm UTC](https://community.particle.io/t/atypical-particle-subcribe-handling-on-1-5-4-rc-1-for-device-name-functionality/57135/7 "2020-07-24T21:22:11Z")

</div>

> [@ScruffR](#):
>
> `Particle.publish("particle/device/name", PRIVATE);`

Unfortunately even after changing the Particle.subscribe() to this in my code:

```auto
Particle.publish("particle/device/name", PRIVATE);

```

![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/c/d/cdcb5783f1c0108e9331b2661ffed368b696cd28.png)

I still am not seeing it in Events:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/6/6/66bdbe4ef747aac1993a22414555f21da30047ae.png)

---

<div class="post-metadata">

**Author:** ![nrobinson2000](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/nrobinson2000/32/15624_2.png) [@nrobinson2000](https://community.particle.io/u/nrobinson2000)\
**Post date:** [July 24, 2020, 9:24pm UTC](https://community.particle.io/t/atypical-particle-subcribe-handling-on-1-5-4-rc-1-for-device-name-functionality/57135/8 "2020-07-24T21:24:14Z")

</div>

> [@hansA](#):
>
> I still am not seeing it in Events:

Have you addressed the issue with `deviceName` that @armor and I mentioned?

---

<div class="post-metadata">

**Author:** ![hansA](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/hansa/32/24722_2.png) [@hansA](https://community.particle.io/u/hansA)\
**Post date:** [July 24, 2020, 9:37pm UTC](https://community.particle.io/t/atypical-particle-subcribe-handling-on-1-5-4-rc-1-for-device-name-functionality/57135/9 "2020-07-24T21:37:05Z")

</div>

I think you may be onto something here, but I haven’t got it to work yet. I will keep you all informed on my progress.

---

<div class="post-metadata">

**Author:** ![hansA](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/hansa/32/24722_2.png) [@hansA](https://community.particle.io/u/hansA)\
**Post date:** [July 24, 2020, 10:12pm UTC](https://community.particle.io/t/atypical-particle-subcribe-handling-on-1-5-4-rc-1-for-device-name-functionality/57135/10 "2020-07-24T22:12:43Z")

</div>

So I’m still getting null value. Here is how I declare deviceName:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/c/2/c248931550d3d6ff16395dc052f1f838c54dbcf4.png)

and here is how the handler is coded:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/e/d/ede8401ff586d32bd6bcdef988b2e9cc673693e7.png)

Still nothing in console:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/3/1/317dc6f3f7b06d714fcfd0cd147f3601f811073c.png)

It’s almost as if the handler isn’t getting called from the Particle.subscribe command because if it was I would see this debug Particle.publish(“device handler called” in console:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/0/1/010e75afc74d1012633003b7daa458a83bdf58d8.png)

---

<div class="post-metadata">

**Author:** ![nrobinson2000](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/nrobinson2000/32/15624_2.png) [@nrobinson2000](https://community.particle.io/u/nrobinson2000)\
**Post date:** [July 24, 2020, 10:32pm UTC](https://community.particle.io/t/atypical-particle-subcribe-handling-on-1-5-4-rc-1-for-device-name-functionality/57135/11 "2020-07-24T22:32:50Z")

</div>

Do you have physical access to the device? Printing debug messages over serial is much preferable over publishing events because events can get throttled and publishes and subscriptions use the same buffer.

A quick Serial print in `deviceNameHandler` could tell you a lot:

```cpp
// At the beginning of the function
Serial.println(data);

// At the end of the function
Serial.println(deviceName);

```

Also, on line 1029, that publish might not be getting through because you didn’t specify `PRIVATE`.

Also, you defined the length of `deviceName` to 24, but you set the null terminator on index 24, which is out of range. You should set it on index 23, which is the last index. That yellow squiggle might be a warning about this. You could also define the length as 25. Also, since `data` is likely less than 24 characters, you could remove that line setting the null terminator altogether.

---

<div class="post-metadata">

**Author:** ![hansA](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/hansa/32/24722_2.png) [@hansA](https://community.particle.io/u/hansA)\
**Post date:** [July 24, 2020, 10:42pm UTC](https://community.particle.io/t/atypical-particle-subcribe-handling-on-1-5-4-rc-1-for-device-name-functionality/57135/12 "2020-07-24T22:42:13Z")

</div>

I will try but I currently don’t have access.

---

<div class="post-metadata">

**Author:** ![nrobinson2000](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/nrobinson2000/32/15624_2.png) [@nrobinson2000](https://community.particle.io/u/nrobinson2000)\
**Post date:** [July 24, 2020, 10:52pm UTC](https://community.particle.io/t/atypical-particle-subcribe-handling-on-1-5-4-rc-1-for-device-name-functionality/57135/13 "2020-07-24T22:52:46Z")

</div>

Try specifying `PRIVATE` in the publish on line 1029, and comment out line 1028, where you set the null terminator out of bounds.

---

<div class="post-metadata">

**Author:** ![hansA](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/hansa/32/24722_2.png) [@hansA](https://community.particle.io/u/hansA)\
**Post date:** [July 24, 2020, 11:16pm UTC](https://community.particle.io/t/atypical-particle-subcribe-handling-on-1-5-4-rc-1-for-device-name-functionality/57135/14 "2020-07-24T23:16:32Z")

</div>

Okay so I got it working but I don’t know how. I put the

```auto
Particle.publish("particle/device/name", PRIVATE);

```

inside the loop() function instead of in the void do\_report() and it started working.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/a/7/a78abf122ee4c1d3ad91d699de3487569e39baec.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/b/e/be5dc6de3c240bac7ee2ff1d0b40e66176a80c25.png)

Why? I don’t know…

---

<div class="post-metadata">

**Author:** ![hansA](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/hansa/32/24722_2.png) [@hansA](https://community.particle.io/u/hansA)\
**Post date:** [July 24, 2020, 11:28pm UTC](https://community.particle.io/t/atypical-particle-subcribe-handling-on-1-5-4-rc-1-for-device-name-functionality/57135/15 "2020-07-24T23:28:20Z")

</div>

Okay, so I finally figure out what was causing the fault condition, but I dont know why… Maybe someone else can shed some light.

In order for Particle.subscribe() to call the handler function

```auto
Particle.subscribe("particle/device/name", deviceNameHandler, MY_DEVICES);

```

Particle.publish(function below) has to be called at least twice:

```auto
Particle.publish("particle/device/name", PRIVATE);

```

My code was set up in a way that the Particle.publish() command was only sent once, so the handler was not called.

I don’t know if that’s a bug or something specific to my code but that’s what seems to have fixed it.

Here’s some proof: Look at my Event viewer, the first time my do\_report was called the deviceName was not updated, but the second time the Particle.publish command was called it did work.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/9/2/92e9e6628df3228923c715280defba6c0a8dc8d6.png)

If anyone has experienced similar behavior please let me know.

---

<div class="post-metadata">

**Author:** ![hansA](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/hansa/32/24722_2.png) [@hansA](https://community.particle.io/u/hansA)\
**Post date:** [July 24, 2020, 11:48pm UTC](https://community.particle.io/t/atypical-particle-subcribe-handling-on-1-5-4-rc-1-for-device-name-functionality/57135/16 "2020-07-24T23:48:26Z")

</div>

Sweet!!

![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/6/e/6e133f5b78dfb1abada19974c8ee087b1110484d.png)

---

<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:** [July 25, 2020, 3:15am UTC](https://community.particle.io/t/atypical-particle-subcribe-handling-on-1-5-4-rc-1-for-device-name-functionality/57135/17 "2020-07-25T03:15:02Z")

</div>

> [@hansA](#):
>
> Particle.publish(function below) has to be called at least twice:

I don't think so - I'd rather think it's some race condition where your first publish just happens too early.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/3/2/3291e3c9351789fe8620ae5bcec8d3fec67ff221.png) [@system](https://community.particle.io/u/system)\
**Post date:** [January 23, 2021, 3:15pm UTC](https://community.particle.io/t/atypical-particle-subcribe-handling-on-1-5-4-rc-1-for-device-name-functionality/57135/18 "2021-01-23T15:15:20Z")

</div>

Closed on January 23, 2021, 3:15pm UTC

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
