Using a constant with Spark.variable

Is it possible to use a constant with Spark.variable()? I’m trying to output a version number for a url endpoint. Maybe there’s a better way to do this? I want to avoid using a variable if possible. This is what I’ve tried but I get an error:

#define VERSION 1.0
Spark.variable("appVer", VERSION, DOUBLE);

You could make it publish the value in the setup? There’s no limit on those, and they’re pushed.

@greatwitenorth, to use Spark.variable, you would do this:

const double version = 1.0;
Spark.variable("appVer", &version, DOUBLE);

:smile:

1 Like

@peekay123 I thought so too, but whenI do that I get the following errors:

ps does anyone know how to copy and paste errors from Spark Dev?

Hi @greatwitenorth

The variable cannot be declared const since then it will live in flash not in RAM. Try taking the const keyword off of the declaration.

1 Like

OK I figured that was the issue. I suppose I’ll just have to keep the version number a variable.

This is not currently available, but would perhaps be nice as a feature request;
When a Spark device comes online, the cloud broadcasts a Server Sent Event with some info about said device (see code below). In this info, the CC3000 patch version is contained. Perhaps it would be nice if it were possible to add a user configurable value to that, which get’s broadcast upon connectivity. One could put his/her version number in it, which would then be always available.
Since @Dave doesn’t seem to have enough work already, let’s ask him what he thinks about this ;)? (All others are free to comment as well, I just like pinging @Dave :p)

{ data: 'online',
  ttl: '60',
  published_at: '2015-02-06T21:12:11.010Z',
  coreid: '50ff7blablabla0270487',
  name: 'spark/status' }

{ data: '1.28',
  ttl: '60',
  published_at: '2015-02-06T21:12:11.114Z',
  coreid: '50ff770blablabla50270487',
  name: 'spark/cc3000-patch-version' }
1 Like

Hey All,

You can always add your own by just putting a spark.publish call in your setup function:

setup() {
  Spark.publish("app_version", String(your_version_constant));
}

Thanks!
David

1 Like

I guess the actual location should not make a lot of difference unless you try changing the value of your const variable.
As long you are sure not to change the value, you could just cast the const away

const double version = 1.0;
Spark.variable("appVer", (void*)&version, DOUBLE);

This compiles fine and should work, too - I haven't tested it tho' :wink:

3 Likes

@ScruffR Thanks that did the trick perfectly. As a side note, I thought you cannot change the value of a const. Wouldn’t that create a error while compiling?

Exactly - this was the reason for my warning to not even try :wink:

But no, if you fool the compiler into believing it is changeable it won’t warn you.
On the other hand even if you cast the const away, the memory location will still be in flash and hence unchangeable and any attempt to write to this “variable” will crash the program.
But if you had a RAM variable once referenced as a const type and once as a changeable (e.g. as union or via pointer) the compiler would just treat the memory location according to the access restrictions imposed on the reference when declared.