# Doing something wrong with Particle.(publish||subscribe)

**URL:** <https://community.particle.io/t/doing-something-wrong-with-particle-publish-subscribe/14926>\
**Category:** Cloud\
**Created:** [August 31, 2015, 11:11am UTC](https://community.particle.io/t/doing-something-wrong-with-particle-publish-subscribe/14926 "2015-08-31T11:11:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![clone137](https://avatars.discourse-cdn.com/v4/letter/c/e9a140/32.png) [@clone137](https://community.particle.io/u/clone137)\
**Post date:** [August 31, 2015, 11:11am UTC](https://community.particle.io/t/doing-something-wrong-with-particle-publish-subscribe/14926/1 "2015-08-31T11:11:54Z")

</div>

Hi all,

I seem to be having a weird problem with Particle.(publish||subscribe). I’m publishing data like this:

```
temperature = dht.getTempCelcius();

sprintf(publishString,"%u",temperature);
Particle.publish("temperature", publishString);

```

and subscribing to that specific Photon’s published data with:

```
void tempHandler(const char *event, const char *data) {
  if (data) {
    Serial.println(String(data));
  }
}

void setup() {
	Serial.begin(9600);
	Spark.subscribe("temperature", tempHandler, "2b00[REMOVED]3032");
}

```

Watching the values being printed via Serial.println(), I’m seeing a lot of this:

```
3526011
96.2
25.000000
22
70.14F - 70.14F - 71.15F - 58.10F Diff: 13.0500
19
68.00
3526016
70.14F - 70.14F - 71.15F - 58.10F Diff: 13.0500

```

The only data in there that is mine is the value “ **19** ”. It would seem even though I have subscribed to data published by another of my Photons (by specifying the device ID), I’m receiving data published by other Photons which aren’t my own.

Please tell me what I’m doing wrong here.

Thank you!

---

<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:** [August 31, 2015, 12:40pm UTC](https://community.particle.io/t/doing-something-wrong-with-particle-publish-subscribe/14926/2 "2015-08-31T12:40:47Z")

</div>

looks like you are getting someone else’s data.

instead of using the device ID as the filter, try using MY\_DEVICES, that assures that you only see your devices, not everyone using the (very very common) moniker for their temperature callback “temperature”

try changing your event name to something like “clone137\_temp” to make sure yours in unique.

---

<div class="post-metadata">

**Author:** ![clone137](https://avatars.discourse-cdn.com/v4/letter/c/e9a140/32.png) [@clone137](https://community.particle.io/u/clone137)\
**Post date:** [August 31, 2015, 1:05pm UTC](https://community.particle.io/t/doing-something-wrong-with-particle-publish-subscribe/14926/3 "2015-08-31T13:05:39Z")

</div>

Thanks for the reply. I understand that, and had it working correctly with a globally unique event name, but I would’ve thought by specifying the particular device ID I’m wanting the data from, it would limit to that device. I guess I’m wrong, but I then fail to understand what the purpose is of specifying the device ID in Particle.subscribe()

---

<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:** [August 31, 2015, 1:17pm UTC](https://community.particle.io/t/doing-something-wrong-with-particle-publish-subscribe/14926/4 "2015-08-31T13:17:50Z")

</div>

@clone137

frankly, I have tried using the unique device id with not much success, but that has been around getting a customized callback (e.g. to update only the device that made the request). I haven’t played around with it enough to know if it works properly with a pair of photons!

that being said, I have had success using this method, I hope you can see it in the example… adding the device ID to the response… searching for the device id in the handler… works for me:

```cpp
struct deviceTime{
  int Hour;
  int Minute;
};

deviceTime sunrise = {6,0};
deviceTime sunset = {18,30};

String myDeviceID;

char message[60] = "No Time Values Recieved";
bool startUpFlag = true;
unsigned long myTimer = 0;
void setup()
{
  myDeviceID = System.deviceID();
  Particle.variable("deviceID", myDeviceID, STRING);
  Particle.variable("SunriseTime", &message, STRING);
  Particle.subscribe("hook-response/sun_time", sunTimeHandler, MY_DEVICES); //MY_DEVICES
  char buffer[60] = "Webhook Test Begin...";
  Particle.publish("pushover", String(buffer), 60, PRIVATE);
}

void loop()
{
  if((millis() - myTimer > 10 * 60 * 1000UL) || startUpFlag)
  {
    startUpFlag = false;
    Particle.publish("sun_time", "{ \"myCity\": \"Weston\", \"myState\": \"FL\" }", 60, PRIVATE);
    myTimer = millis();
  }
}

void sunTimeHandler(const char * event, const char * data)
{
  String sunriseReturn = String(data);
  char sunriseBuffer[125] = "";
  sunriseReturn.toCharArray(sunriseBuffer, 125);
  char thisDeviceID[25] = "";
  myDeviceID.toCharArray(thisDeviceID, 25);
  if (strstr(sunriseBuffer, thisDeviceID)) // Filters responses unique to this device here!!
  {
    sunrise.Hour = atoi(strtok(sunriseBuffer, "\"~"));
    sunrise.Minute = atoi(strtok(NULL, "~"));
    sunset.Hour = atoi(strtok(NULL, "~"));
    sunset.Minute = atoi(strtok(NULL, "~"));
    char buffer[60] = "";
    sprintf(buffer, "Florida Sunrise: %02d:%02d, Sunset: %02d:%02d", sunrise.Hour, sunrise.Minute, sunset.Hour, sunset.Minute);
    Particle.publish("pushover", String(buffer), 60, PRIVATE);
    strcpy (message, buffer);
  }
}

```

with this webhook:

```json
{
	"event": "sun_time",
	"url": "http://api.wunderground.com/api/getYourOwnAPI/astronomy/q/{{myState}}/{{myCity}}.json",
	"requestType": "POST",
	"headers": null,
	"query": null,
	"responseTemplate": "{{#sun_phase}}{{sunrise.hour}}~{{sunrise.minute}}~{{sunset.hour}}~{{sunset.minute}}~{{/sun_phase}}{{SPARK_CORE_ID}}~",
	"json": null,
	"auth": null,
	"coreid": null,
	"mydevices": true
}

```

note the `{{SPARK_CORE_ID}}` in the response template

---

<div class="post-metadata">

**Author:** ![clone137](https://avatars.discourse-cdn.com/v4/letter/c/e9a140/32.png) [@clone137](https://community.particle.io/u/clone137)\
**Post date:** [August 31, 2015, 1:22pm UTC](https://community.particle.io/t/doing-something-wrong-with-particle-publish-subscribe/14926/5 "2015-08-31T13:22:46Z")

</div>

Thanks, appreciate the help here 😄

I’m just playing around with the publish/subscribe bits and was curious as to why specifying the device ID didn’t work as I expected it to. I’m perfectly fine with having to use a globally unique event name for now. I’m new to Photons (and Particle) and just trying a bit of everything while figuring them out.

Thanks again 😄

---

<div class="post-metadata">

**Author:** ![Dave](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/dave/32/181_2.png) [@Dave](https://community.particle.io/u/Dave)\
**Post date:** [September 2, 2015, 4:17pm UTC](https://community.particle.io/t/doing-something-wrong-with-particle-publish-subscribe/14926/6 "2015-09-02T16:17:36Z")

</div>

Hi @clone137,

It looks like this is a bug in the subscribe call for a device, that it’s not filtering to a particular device id. I’ll open a task for that and fix it as soon as I get a chance.

Thanks!  
David

---

<div class="post-metadata">

**Author:** ![clone137](https://avatars.discourse-cdn.com/v4/letter/c/e9a140/32.png) [@clone137](https://community.particle.io/u/clone137)\
**Post date:** [September 2, 2015, 7:10pm UTC](https://community.particle.io/t/doing-something-wrong-with-particle-publish-subscribe/14926/7 "2015-09-02T19:10:45Z")

</div>

Hi @Dave,

Thanks, at least it makes sense to me now why it wasn’t working. Just glad it wasn’t me 😄

Thanks

---

<div class="post-metadata">

**Author:** ![Ali](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@Ali](https://community.particle.io/u/Ali)\
**Post date:** [December 21, 2015, 10:16pm UTC](https://community.particle.io/t/doing-something-wrong-with-particle-publish-subscribe/14926/8 "2015-12-21T22:16:22Z")

</div>

Would you mind sharing the entire code with me. I am a bit of a noob but am trying to the same thing as you are.

---

<div class="post-metadata">

**Author:** ![joedonn](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@joedonn](https://community.particle.io/u/joedonn)\
**Post date:** [December 22, 2015, 3:31am UTC](https://community.particle.io/t/doing-something-wrong-with-particle-publish-subscribe/14926/9 "2015-12-22T03:31:47Z")

</div>

> [@Dave](#):
>
> Hi @clone137,
> 
> It looks like this is a bug in the subscribe call for a device, that it's not filtering to a particular device id. I'll open a task for that and fix it as soon as I get a chance.
> 
> Thanks!David

Dave,

That would be great. I posted a similar issue in this recent post:

> [@Particle.subscribe unique device ID not returning correct values](https://community.particle.io/t/particle-subscribe-unique-device-id-not-returning-correct-values/18422):
>
> I’m using the following command and the values returned are not correct when using the device ID : Particle.subscribe(“Humidity”, myHandler1, “2d002…xx…3230”); The values returned are somewhat random integers. Examples: Time:1:52, Soil Humidity1:29.00 Time:2:2, Soil Humidity1:50.00 Time:2:22, Soil Humidity1:50.00 While using: Particle.subscribe(“Humidity”, myHandler1, MY\_DEVICES); does return the correct value from the unique device (my only device with the event “Humidity”): Time:9:…

The particle.subscribe in the CLI appears to work, but not coding via the web IDE.

Thanks!

---

<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 22, 2015, 8:37am UTC](https://community.particle.io/t/doing-something-wrong-with-particle-publish-subscribe/14926/10 "2015-12-22T08:37:57Z")

</div>

See here  
[Particle.subscribe unique device ID not returning correct values](https://community.particle.io/t/particle-subscribe-unique-device-id-not-returning-correct-values/18422/5)
