Particle Subscribe

I can’t figure out why my photon isn’t subscribing. Photon one sends out a publish succesfully and I can see that and its associated data in the dashboard, however, photon two isn’t seeing it. I have the handler taking the data and storing it to a particle variable and my variable was never updating. At first I thought it was how I was converting my strings to a number or something but then I threw in the handler for it to publish its own event so I could troubleshoot if it was even subscribing correctly. It never publishes that event so something is wrong with my subscribe code?!

// the pertinent code
// photon 1

void loop() {
Particle.publish("CurrentTemperature", temperature); // publish to cloud
}

// photon 2
double poolTemp = 0.0;

void setup() {
Particle.subscribe("CurrentTemperature", tempHandler, MY_DEVICES);
Particle.variable("PoolTemp", poolTemp);
}

void loop() {
}

void tempHandler(const char *event, const char *data)
{
poolTemp = atof(data);
Particle.publish("hey", "on");
}

Your publish statement should include PRIVATE. Hopefully, you have some delay in there so you're not publishing every millisecond or so.

Particle.publish("CurrentTemperature", temperature, PRIVATE);

Also, note this info from the docs,

NOTE 2: Particle.publish() and the Particle.subscribe() handler(s) share the same buffer. As such, calling Particle.publish() within a Particle.subscribe() handler will wipe the subscribe buffer! In these cases, copying the subscribe buffer's content to a separate char buffer prior to calling Particle.publish() is recommended.

1 Like

Yeah the code has lots of other stuff in there. Actually it only publishes once every four hours. I see it come through in the dashboard but my subscribe wouldn’t work. Okay so with that note maybe my subscribe was working but maybe I’m not handling the data correctly?

Hey thanks adding private made that work! Thanks :slight_smile: I knew I was close! Are we not able to get data from a public publish? I'm just confused why that worked I racked my brain yesterday trying to fifure this out!