"Variable not found" returned by REST API

Hi there. I have a peculiar issue with one of my Photons that is installed in a remote area. According to the dashboard app, the board is up and running (breathing cyan). I’ve been able to flash it and re-flash it remotely without errors. My code is ridiculously simple. It reads an analog value from A0, and uses Particle.variable() to set it in the cloud. I have a command line alias that uses curl to fetch the JSON output. Something like this:

curl https://api.particle.io/v1/devices/device-id-here/Moisture?access-token-here

I had this working before the last re-flashing, and I got a response with the read value, the last handshake date, etc. However, after I re-flashed it, I’m now getting this:

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

My code is as simple as it gets, and I’m scratching my head wondering what is going on. The firmware version is 0.5.0. I flashed the board from the web UI. Here’s the code in it’s entirety:

int sGnd = D0;

void setup() {
  pinMode(sGnd, OUTPUT);
  digitalWrite(sGnd, HIGH); // Turn sensor on
  delay(1000);     // Delay 1000 ms
}

void loop() {
  digitalWrite(sGnd, HIGH); // Turn sensor on
  delay(1000);
  Particle.variable("Moisture", -1); // Should be immediately overwritten.
  int n = analogRead(A0);
  n = analogRead(A0); // Don't trust the first read.
  
  // Workaround for a strange bug where the read value is REALLY huge. 
  if (n < 10000) {
    Particle.variable("Moisture", n);
  }
  
  digitalWrite(sGnd, LOW); // Turn sensor off
  delay(120000);
}

I’ve had some interesting issues where the read value is HUGE, which is the reason I have two calls to analogRead() (in case the first one returns garbage), as well as the if (n < 10000) clause. Either way, the variable should be unconditionally set in the first call where I’m passing in -1. It still doesn’t seem to be available from the cloud API. The credentials are correct as far as I know, and I’m not getting any authentication errors when calling the REST API. I just get the “variable not found” error. What’s the best way of debugging this? Thanks.

That’s not the way Particle.variable() works. Your code should look more like:

int sGnd = D0;
int moisture = -1;

void setup() {
  pinMode(sGnd, OUTPUT);
  digitalWrite(sGnd, HIGH); // Turn sensor on
  Particle.variable("Moisture", moisture);
}

void loop() {
  digitalWrite(sGnd, HIGH); // Turn sensor on
  delay(1000);
  int n = analogRead(A0);
  n = analogRead(A0); // Don't trust the first read.

  // Workaround for a strange bug where the read value is REALLY huge.
  if (n < 10000) {
    moisture = n;
  }

  digitalWrite(sGnd, LOW); // Turn sensor off
  delay(120000);
}

You register a particle variable once, and whenever it’s queried, the current value is returned.

Also, particle variables are only registered during setup and very shortly thereafter. That’s why the variable is showing as not existing.

3 Likes

Thanks @rickkas7 ! Much better. It’s working fine now :slight_smile:

1 Like