# Published Variable "Code Problems"

**URL:** <https://community.particle.io/t/published-variable-code-problems/45291>\
**Category:** Troubleshooting\
**Created:** [November 15, 2018, 7:13pm UTC](https://community.particle.io/t/published-variable-code-problems/45291 "2018-11-15T19:13:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cpeel](https://avatars.discourse-cdn.com/v4/letter/c/9de0a6/32.png) [@cpeel](https://community.particle.io/u/cpeel)\
**Post date:** [November 15, 2018, 7:13pm UTC](https://community.particle.io/t/published-variable-code-problems/45291/1 "2018-11-15T19:13:28Z")

</div>

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.

 ![code](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/4/5/4569ee23f658e879b6c524f42ebcd1c2e754c108.png)  
 ![event%20(2)](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/e/1/e1a9102256e56d642de92d3995f4534f3bd7d60d.png)

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [November 15, 2018, 7:30pm UTC](https://community.particle.io/t/published-variable-code-problems/45291/2 "2018-11-15T19:30:53Z")

</div>

@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](https://docs.particle.io/reference/device-os/firmware/photon/#particle-variable-)). 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`:

```auto
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:

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

```

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

---

<div class="post-metadata">

**Author:** ![cpeel](https://avatars.discourse-cdn.com/v4/letter/c/9de0a6/32.png) [@cpeel](https://community.particle.io/u/cpeel)\
**Post date:** [November 15, 2018, 8:03pm UTC](https://community.particle.io/t/published-variable-code-problems/45291/3 "2018-11-15T20:03:29Z")

</div>

Thank you for the help,It works perfect now.
