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);
}
Ric
February 7, 2017, 9:31pm
2
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
BDub
February 7, 2017, 10:36pm
4
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;
Ric
February 7, 2017, 10:44pm
5
benjiiiiuk:
char 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