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

Hi all,

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

temperature = dht.getTempCelcius();

Particle.publish("temperature", publishString);

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

void tempHandler(const char *event, const char *data) {
  if (data) {

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

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

70.14F - 70.14F - 71.15F - 58.10F Diff: 13.0500
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!

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.

1 Like

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()


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:

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:

	"event": "sun_time",
	"url": "{{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

Thanks, appreciate the help here :smile:

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 :smile:

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.


Hi @Dave,

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


1 Like

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.


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

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


See here
Particle.subscribe unique device ID not returning correct values