Subscribe particle getting weird data instead of temperature?

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

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.

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

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”

1 Like

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.

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);
  }
}

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

1 Like

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

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

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

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.