Hi,
I attached a TMP36 sensor to the Photon and wrote some code based on the examples in the docs to take readings from the thermometer, calculate the temperature in C, and post it.
The temperature publishes sucessfully to the dashboard.
However, I cannot access the any of the variables through the link:
https://api.particle.io/v1/devices/your-device-ID-goes-here/VARIABLE?access_token=your-access-token-goes-here
The output I get when I go to it is
{
"ok": false,
"error": "Variable not found"
}
I've attached my code. If anyone can take a look I would be very greatful.
int dataYellow = A0; //data is passed over the yellow cable to A0
int powerSupplyGreen = A5; //power is passed over the green cable from A5
char tempForPubInt[2]; //the string argument for publishing the temperature
char tempForPubDouble[5]; // string argument for publishing the temperature to 2 decimal places
int convertToTempInt;
double convertToTempDouble;
void setup() {
pinMode(powerSupplyGreen, OUTPUT); //declare the green wire as power
pinMode(dataYellow, INPUT); //declare the yellow wire as input
Spark.variable("tempForPubInt", &tempForPubInt, STRING); // string var for int temperature
Spark.variable("tempForPubDouble", &tempForPubDouble, STRING); // string var for double temperature
Spark.variable("convertToTempInt", &convertToTempInt, INT); // int var for int temperature
Spark.variable("convertToTempDouble", &convertToTempDouble, DOUBLE); // int var for int temperature
}
void loop() {
analogWrite(powerSupplyGreen, 4095); //turn on the TMP36 using 3.3V (https://www.sparkfun.com/products/10988)
delay(5000); //give the TMP36 5s. It doesn't say to do this anywhere, I do it just in case
int interim; // a variable that stores an interim voltage value from the data wire connected to the TMP36
interim = 0;
int prior;
prior = 0;
int stack; // a variable to add the interim voltage value 10x (see loop below)
stack = 0;
//this loop reads the voltage. checks if the voltage is less than half of that last recorded.
//If it is, it rewinds back to the start of the loop.
// Check if interim is 2x bigger than the prior interim. If so, reset loop. This should only happen by the second reading.
// If not, it adds it to the stack, and waits 6s. Repeat 10x.
// This is an attempt to deal with a noisy sensor as I don't have a capacitor on hand.
int i;
i = 0;
while ( i < 10 ) {
i++;
if ((interim > (prior * 2)) && (prior != 0)) {
i = 0;
continue;
}
if ((interim < (prior / 2)) && (prior != 0))
{
i--;
continue;
}
else
{
interim = analogRead(dataYellow);
stack = interim + stack;
delay(6000);
}
}
stack = (stack / 10); // take the average of the stack
convertToTempInt = 0; // this is the int that will hold the converted temperature in C
convertToTempDouble = 0; // this is the int that will hold the converted temperature in C
// here we apply the formula to convert the voltage to a temperature
convertToTempInt = (25 + (((stack * 0.8) - 750) / 10));
convertToTempDouble = (25 + (((stack * 0.8) - 750) / 10));
//sprintf to convert the temperature int to a string
sprintf (tempForPubInt, "%d", convertToTempInt);
//apparently sprintf doesnt work with doubles and floats on the photon
//fixed this by using suggested solution here (https://community.particle.io/t/issues-with-converting-float-to-string-with-sprintf/12053/7)
String tempForPubDouble(convertToTempDouble, 2);
//publish both variables
Spark.publish("tempForPubInt", tempForPubInt);
Spark.publish("tempForPubDouble", tempForPubDouble);
//turn off the power to the temperature sensor
analogWrite(powerSupplyGreen, 0);
//wait 10 min before doing anything again
delay (600000);
}
-P