Published Variable "Code Problems"

I am trying to code a project that uses a temperature sensor hooked to one photon to publish a temperature as an event, the second photon has a fan circuit and needs to activate two relays either on or off depending on temperature. I know basic coding but i cant seem to figure out how the subscribe/variable system works.


@cpeel, first off, your subscribe is not as per documentation:

Particle.subscribe("the_event_prefix", theHandler, MY_DEVICES);

In your case you are specifying the ID of the specific Photon which won’t work. I suggest you change it to “MY_DEVICES” so the Photon only subscribes to publishes on your devices.

Second, Particle.variable() is NOT a way to get values from another device. Instead, it is a way to make the value of the specified variable available for query via a REST API call to the Cloud (also, your syntax is incorrect). In your case, the char *data string variable of control() is what Particle.subscribe() will use to hold the string version of the published temperature.

So, to get the published temperature on your second Photon, control() needs to have a little extra code to extract it from char *data:

void control(const char *event, const char *data)
{
  temperature = atof(data);  // This converts the temperature string to a double value
  ...
}

Be sure to declare the temperature var as a double above setup() if you want it to be a global. If you want to make it a integer instead, you can modify the above line as follows:

  temperature = atoi(data));  // This converts the temperature string to a int value

Again, be sure to declare the temperature var as a int.

2 Likes

Thank you for the help,It works perfect now.

1 Like