[SOLVED]Particle.variable is not working

Hi!

I am having issues using Cloud variable, they are not registering at all.

I use the following code:

#include <math.h>

char publishString[40];
unsigned long lastPlantMeasure;
int value;

void setup()
{
  lastPlantMeasure = 0;

  RGB.control(true);
  RGB.color(255,0,0);
  RGB.brightness(0);

  measurePlant();
}

void measurePlant() {
  lastPlantMeasure = millis();

  value = analogRead(A0);
  sprintf(publishString,"%d", value);
  Spark.publish("plantv1", publishString);
  
  if(Particle.variable("myPlantHumidityv1", value) == false) {
    Spark.publish("message", "VARIABLE NOT REGISTERED");
  };
}

void loop(void) {
  if(millis() - lastPlantMeasure > 60000) {
    measurePlant();
  }
}

VARIABLE NOT REGISTERED is getting published every minute. Also if i query the device it’s reporting no cloud variables at all:

curl https://api.particle.io/v1/devices/{deviceID}\?access_token\={token}
{
  "id": "{deviceID}",
  "name": "particle-pinny",
  "last_app": null,
  "last_ip_address": "{myip}",
  "last_heard": "2016-03-18T15:17:17.937Z",
  "product_id": 6,
  "connected": true,
  "platform_id": 6,
  "cellular": false,
  "status": "normal",
  "variables": {},
  "functions": [],
  "cc3000_patch_version": "wl0: Nov  7 2014 16:03:45 version 5.90.230.12 FWID 01-628c21c3"
}

I use 0.4.9 firmware.
What’s wrong?

The maximum length for a Particle cloud variable name is 12 characters - your name is too long, which is why it’s not registered.

Also, you should put the Particle.variable in setup(), not called out of loop. You only need to do it once, as it registers the variable. The current value of the variable is retrieved automatically when requested.

2 Likes

Docs for reference…
https://docs.particle.io/reference/firmware/photon/#particle-variable-

1 Like

Thank you guys, i was changing names to short and long before neither of them worked, i totally forgot the 12 character limit.
My real issue was, that every time i wanted to refresh the variable i tried to re-register it in the loop.
Registering it one time in setup and just modifying the variable behind does the trick.
Thanks!