# Accessing variable with JSON

**URL:** <https://community.particle.io/t/accessing-variable-with-json/12843>\
**Category:** Troubleshooting\
**Created:** [June 28, 2015, 4:47am UTC](https://community.particle.io/t/accessing-variable-with-json/12843 "2015-06-28T04:47:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pvtodorov](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@pvtodorov](https://community.particle.io/u/pvtodorov)\
**Post date:** [June 28, 2015, 4:47am UTC](https://community.particle.io/t/accessing-variable-with-json/12843/1 "2015-06-28T04:47:33Z")

</div>

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](https://i.imgur.com/Jai4xYG.jpeg "imgur.com") ](https://imgur.com/Jai4xYG)

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

```auto

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

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [June 28, 2015, 4:55am UTC](https://community.particle.io/t/accessing-variable-with-json/12843/2 "2015-06-28T04:55:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pvtodorov](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@pvtodorov](https://community.particle.io/u/pvtodorov)\
**Post date:** [June 28, 2015, 5:08am UTC](https://community.particle.io/t/accessing-variable-with-json/12843/4 "2015-06-28T05:08:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TheVelozGroup](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/thevelozgroup/32/6289_2.png) [@TheVelozGroup](https://community.particle.io/u/TheVelozGroup)\
**Post date:** [June 28, 2015, 6:55am UTC](https://community.particle.io/t/accessing-variable-with-json/12843/5 "2015-06-28T06:55:16Z")

</div>

@pvtodorov

Based on your code:

[https://api.particle.io/v1/devices/your-device-ID/tempForPubInt?access\_token=your-access-token](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.

---

<div class="post-metadata">

**Author:** ![pvtodorov](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@pvtodorov](https://community.particle.io/u/pvtodorov)\
**Post date:** [June 28, 2015, 12:38pm UTC](https://community.particle.io/t/accessing-variable-with-json/12843/6 "2015-06-28T12:38:58Z")

</div>

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](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."  
> }

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [June 28, 2015, 1:19pm UTC](https://community.particle.io/t/accessing-variable-with-json/12843/7 "2015-06-28T13:19:30Z")

</div>

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

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

```

Needs to change to:

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

```

or:

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

```

---

<div class="post-metadata">

**Author:** ![bko](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bko/32/20704_2.png) [@bko](https://community.particle.io/u/bko)\
**Post date:** [June 28, 2015, 3:04pm UTC](https://community.particle.io/t/accessing-variable-with-json/12843/8 "2015-06-28T15:04:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [June 28, 2015, 4:55pm UTC](https://community.particle.io/t/accessing-variable-with-json/12843/9 "2015-06-28T16:55:46Z")

</div>

> [@pvtodorov](#):
>
> 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 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
