Accessing variable with JSON

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.

Picture of setup.
Imgur

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

If you open https://api.particle.io/v1/devices/your-device-ID-goes-here/?access_token=your-access-token-goes-here What do you see?

Here is the ouput

{
"id": "DEVICEID",
"name": "DEVICENAME",
"connected": true,
"variables": {},
"functions": ,
"cc3000_patch_version": null,
"product_id": 6,
"last_heard": "2015-06-28T04:59:11.425Z"
}

Removed device name and id from my last post as I wasn't sure if it was bad to post that sort of thing thing on the forum, even without the token.

@pvtodorov

Based on your code:

https://api.particle.io/v1/devices/your-device-ID/tempForPubInt?access_token=your-access-token

or any other variable though Spark.variable must work. Hope you have checked your deviceid and access token.

1 Like

This is exactly what I thought and why I'm searching for a solution. The output from the photon has me convinced I have device ID and access token are correct.

Here is what I get using with proper device ID and access token.

{
"ok": false,
"error": "Variable not found"
}

If I give the incorrect device ID

{
"error": "Permission Denied",
"info": "I didn't recognize that device name or ID, try opening https://api.particle.io/v1/devices?access_token=REDACTEDMYDEVICETOKENFROMHERE"
}

If I give the incorrect token

{
"error": "invalid_token",
"error_description": "The access token provided is invalid."
}

@pvtodorov, the two STRING Spark.variable() declarations that refer to char[] arrays are incorrectly using the “&” with the pointer. With an array, the name of the array IS the pointer to the array OR “&” can be used with an explicit reference of the first index of the array. So you this:

Spark.variable("tempForPubInt", &tempForPubInt, STRING); // string var for int temperature
Spark.variable("tempForPubDouble", &tempForPubDouble, STRING); // string var for double temperature

Needs to change to:

Spark.variable("tempForPubInt", tempForPubInt, STRING); // string var for int temperature
Spark.variable("tempForPubDouble", tempForPubDouble, STRING); // string var for double temperature

or:

Spark.variable("tempForPubInt", &tempForPubInt[0], STRING); // string var for int temperature
Spark.variable("tempForPubDouble", &tempForPubDouble[0], STRING); // string var for double temperature
1 Like

Also @pvtodorov char arrays need room for zero terminator in C/C++ so your length 2 and length 5 arrays seem really short. If you want two digits of temperature, you need at least an array of size 3, since there are two digits and the terminator.

1 Like

@pvtodorov the reason I asked you to bring that response up is that it shows that your Spark.variables() aren't registered with the cloud. The other posts in this thread may explain why that is however I was trying to show that when you get a error": "Variable not found" on the first things you should look into is what variables are registered with the cloud. In your case no variables are so you are always going to get a Variable not found error