# Subscribe particle getting weird data instead of temperature?

**URL:** <https://community.particle.io/t/subscribe-particle-getting-weird-data-instead-of-temperature/27539>\
**Category:** Troubleshooting\
**Created:** [November 27, 2016, 1:35am UTC](https://community.particle.io/t/subscribe-particle-getting-weird-data-instead-of-temperature/27539 "2016-11-27T01:35:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![front2back](https://avatars.discourse-cdn.com/v4/letter/f/e95f7d/32.png) [@front2back](https://community.particle.io/u/front2back)\
**Post date:** [November 27, 2016, 1:35am UTC](https://community.particle.io/t/subscribe-particle-getting-weird-data-instead-of-temperature/27539/1 "2016-11-27T01:35:05Z")

</div>

In a nutshell, I have two particles, one reading temperature from Dallas DS18B20 One-Wire temperature sensor and publishing that data, and another that is subscribing to the data and should be turning on/off LED’s based on the “if” statements for the data. However, my subscribe particle is getting **VERY** weird data and is constantly just turning on one LED from it. I was wondering if anyone could help me figure out why.

The first particle publishing the temperature data has code that is almost an exact clone of the Temp Logger Particle Tutorial found [Here](https://docs.particle.io/tutorials/projects/maker-kit/#tutorial-4-temperature-logger). The only changes are the publishing data:

```
String temperature = String(fahrenheit); 
 Particle.publish("bfurlong_current_temp", temperature, PUBLIC); 
  delay(10000); 
}

```

Here is what it looks like on the console:

 ![](https://us1.discourse-cdn.com/flex026/uploads/particle/original/2X/4/4471aa7e25d016cd34f465919b5dad01c2e8bc14.png)

Next is my Subscribe particle. My professor helped me a lot with this but while it does compile, it does not work and seems to be getting a constant 536895503 data string. I found this number by converging on it trying to find what number I was getting. Here is the code and explanations for it.

```
void setup() 
// These Pins all have LED's on them. 
{ 
pinMode(D0, OUTPUT);
pinMode(D7, OUTPUT);
pinMode(D2, OUTPUT);
Particle.subscribe("bfurlong_current_temp", myHandler);
}
// My professor told me I needed this empty loop in order to run. 
void loop () {
    
}
void myHandler(const char *event, const char *data) 
//These are all if statements only because if else did not work.
//To prevent LED's from coming on when they shouldn't,
//I set them to LOW. 
//My professor told me that int(data) would suffice comparing the temp data.
{
if(int(data)<=74) {
      digitalWrite(D7,HIGH);
      digitalWrite(D0,LOW);
      digitalWrite(D2,LOW);
  }
if((int(data)>74)&&(int(data)<85)) {
      digitalWrite(D2,LOW);
      digitalWrite(D0,HIGH);
      digitalWrite(D7,LOW);
  }
  if(int(data)>=85) {
      digitalWrite(D7,LOW);
      digitalWrite(D0,LOW);
      digitalWrite(D2,HIGH);
  }
}

```

It will only turn on the final LED and keep it on because, again, it seems to be getting 536895503. Temperature change does not seem to affect this string. Anybody have any clues as to why it’s doing this or how I could make my code read it? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Ric](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ric/32/514_2.png) [@Ric](https://community.particle.io/u/Ric)\
**Post date:** [November 27, 2016, 2:00am UTC](https://community.particle.io/t/subscribe-particle-getting-weird-data-instead-of-temperature/27539/2 "2016-11-27T02:00:12Z")

</div>

I think you either misunderstood your professor, or he/she is steering you wrong. You should convert the const char\* to an int with atoi().

```cpp
void myHandler(const char *event, const char *data) {
   int num = atoi(data);
   if (num<=74) {
      digitalWrite(D7,HIGH);
      digitalWrite(D0,LOW);
      digitalWrite(D2,LOW);
  }else if (num>74 && num<85) {
      digitalWrite(D2,LOW);
      digitalWrite(D0,HIGH);
      digitalWrite(D7,LOW);
  }else {
      digitalWrite(D7,LOW);
      digitalWrite(D0,LOW);
      digitalWrite(D2,HIGH);
  }
}

```

I don’t know if it was a typo, but there is no “if else”; it’s “else if”

---

<div class="post-metadata">

**Author:** ![front2back](https://avatars.discourse-cdn.com/v4/letter/f/e95f7d/32.png) [@front2back](https://community.particle.io/u/front2back)\
**Post date:** [November 27, 2016, 2:24am UTC](https://community.particle.io/t/subscribe-particle-getting-weird-data-instead-of-temperature/27539/3 "2016-11-27T02:24:21Z")

</div>

That’s a great suggestion! I have some weird error from that though and I can’t seem to find what needs to be corrected with it.  
 ![](https://us1.discourse-cdn.com/flex026/uploads/particle/original/2X/c/ce34e59e99aecc2c42322286d9629ac65c174913.png)  
Here’s the current code. Maybe I need to add something else?

```
void setup() 
{ 
pinMode(D0, OUTPUT);
pinMode(D7, OUTPUT);
pinMode(D2, OUTPUT);
Particle.subscribe("bfurlong_current_temp", myHandler);
}
void loop () {
    
}
void myHandler(const char *event, const char *data) {
   int num = atoi(data);
   if(num<=74) {
      digitalWrite(D7,HIGH);
      digitalWrite(D0,LOW);
      digitalWrite(D2,LOW);
  }
  else if((num>74) && num<85)) {
      digitalWrite(D2,LOW);
      digitalWrite(D0,HIGH);
      digitalWrite(D7,LOW);
  }
  else {
      digitalWrite(D7,LOW);
      digitalWrite(D0,LOW);
      digitalWrite(D2,HIGH);
  }
}
```

---

<div class="post-metadata">

**Author:** ![Ric](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ric/32/514_2.png) [@Ric](https://community.particle.io/u/Ric)\
**Post date:** [November 27, 2016, 2:26am UTC](https://community.particle.io/t/subscribe-particle-getting-weird-data-instead-of-temperature/27539/4 "2016-11-27T02:26:35Z")

</div>

You have an extra parenthesis in your else if line. The error message tells you what line the error is on.

---

<div class="post-metadata">

**Author:** ![front2back](https://avatars.discourse-cdn.com/v4/letter/f/e95f7d/32.png) [@front2back](https://community.particle.io/u/front2back)\
**Post date:** [November 27, 2016, 2:40am UTC](https://community.particle.io/t/subscribe-particle-getting-weird-data-instead-of-temperature/27539/5 "2016-11-27T02:40:37Z")

</div>

Thank you so much! It works like a charm now! You’ll be credited in the project for your help.

---

<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:** [November 27, 2016, 6:47am UTC](https://community.particle.io/t/subscribe-particle-getting-weird-data-instead-of-temperature/27539/6 "2016-11-27T06:47:03Z")

</div>

Just to point out your original error.  
To start with, @Ric is right about the need to convert the string to a number if you want to compare a value range.  
But if you’d expect a data like `HIGH` vs. `LOW` it would be enough to just check the first byte.

But this doesn’t do that

```cpp
if(int(data)<=74) 

```

This actuall checks the address stored in the char pointer and not the byte the pointer is pointing to.

You’d rather do that

```cpp
if (data[0] == 'H')
// or
if ((byte)(*data) == 72) // where 72 is the ASCII value of H

```

But I guess you actually want to use `atof()` or `atoi()`

Maybe your professor meant you won’t need `float` (`atof()`) although you get decimals from your subscribe, `int` (`atoi()`) will do and forget the decimals.
