Particle Variables and Functions

So I am currently trying to display an analog pin value to a webpage. All the circuit is is a variable resistor being fed 5V, connected to an analog pin so that the value can be varied. Here is my photon code:

#define pot_input A0
int analog_value = 0;

void setup(){
  Particle.function("getval", getValue);
  Particle.variable("analog_value", analog_value);
  pinMode(pot_input, INPUT);
  pinMode(D7, OUTPUT);
  digitalWrite(D7, HIGH);
}

void loop(){
}

int getValue(String cmd){
  analog_value = analogRead(pot_input);
  return 0;
}

Here is my html:

<!DOCTYPE html>
<html>
<head>
  <script charset="utf-8" src=
  "http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type=
  "text/javascript">
  </script>
  <title></title>
</head>
<body>
  <h1>Current Value at Pin A0:</h1><br>
  <h3 id="val"><strong>NO VALUE YET</strong></h3>
  <script type="text/javascript">
    var deviceID = "MY_ID";
    var accessToken = "MY_ACCESS_TOKEN";
    var getFunc = "getval";
  
    window.setInterval(function() {
      requestURL = "https://api.spark.io/v1/devices/" + deviceID + "/" + getFunc + "/?access_token=" + accessToken;
      $.getJSON(requestURL, function(json){
        document.getElementById("val") = json.result + "units";
      });
    }, 1000);
  </script>
</body>
</html>

The error that I’m getting from the particle API is the following:

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

Does anyone known why my function cannot be accessed? When I run particle list in the command line It appears as a function. I have checked my device id and access token about a million times, they are correct (initially I had an error saying they were wrong so I made sure to check). Any help is greatly appreciated, thanks!

Could you check this page if they show up? If they don’t, there’s probably an issue with the code on the device.

They both show up, under the names “getval” and “analog_value” as expected. Any other ideas?

Yup, the function is called “getval” and the variable is “analog_value”. If you want to get the value using the variable endpoint, you should be calling the correct one.

2 Likes

On the hardware side of things, please take measures to ensure the voltage on your analog pin never exceedes 3.3V.
I'm not sure how you'd do that with only on "variable resistor".

https://docs.particle.io/datasheets/photon-datasheet/#peripherals-and-gpio

And for firmware, you might like to alter this to have some extra way to retrieve your value.

int getValue(String cmd){
  analog_value = analogRead(pot_input);
  return analog_value;
}
2 Likes