Cant see analog value in console log

I am trying to read an analog value and view the resunt in the console event log however no events happen with my code.

i have published text and this works fine

is there something missing in my code.

Thanks…Ben

int analogvalue = 10;
void setup()
{
    

 Particle.variable("analogvalue", analogvalue);
pinMode(A0, INPUT);
}

void loop()
{
  analogvalue = analogRead(A0);
  delay(5000);
 Particle.publish("analogvalue", analogvalue);

}

The data in a Particle.publish needs to be a string (String or const char*), not an int.

2 Likes

thanks for that. i now have this and it seems to work

char publishstring[40];
char analogvalue = 10;
void setup()
{
    

 //Particle.publish("analogvalue", analogvalue);
pinMode(A1, INPUT);
}

void loop()
{
  analogvalue = analogRead(A0);
  delay(5000);
 //Particle.publish("analogvalue", analogvalue);
  //sprintf(publishString,"%u:%u:%u",hours,min,sec);
  sprintf(publishstring,"%u",analogvalue);
        Particle.publish("Uptime",publishstring);

}

EDIT: Spark -> Particle :slight_smile:

You can also do this:

Particle.publish("analogvalue", String(analogvalue) );
or
Particle.publish("analogvalue", String::format("%4.2fV", (float)analogvalue/4095.0*3.3) );

Also you probably want to change

char analogvalue = 10;
to 
int analogvalue = 10;

A char is only an 8 bit data type, so you will not get readings higher than 255 with analogvalue typed that way. As @BDub said, that should be typed as an int

1 Like