Particle constant for current running application name?

Hey all! New to posting in this community but have been a lurker for quite some time. Really enjoying hacking my new Photon.

One issue I’ve had over the years of hacking MCUs is forgetting what program I had flashed onto them. Particle’s cloud API helps remedy this by exposing remote function calls (remote variable evaluation is awesome too). The issue now is keeping the filename string updated in the code.

I searched for a native constant that holds the filename currently flashed on the device but couldn’t fine one. Does one exist? If so, it would be awesome if there was one added eventually.

Something like:

PARTICLE_USER_APP_NAME // or similar

Additionally, the same constant could be updated on the dashboard next to the relevant device so you could just look there as well. Either solution would be great.

You can do something like Spark.publish("app-name:) in setup() for now if you need something that works :smiley:

That's very similar to what I've done. Having a constant available however just makes it simple, since the expectation is that it would always contain the correct/current value.

What about using the C++ standard predefined macro to put the file name into the code?

#define COMPILED_FILENAME __FILE__
const char * programVersion = COMPILED_FILENAME;
2 Likes

Excellent suggestion! I'm going to include that snip in all of my apps. Thanks!

1 Like

:blush:

also, you can strip out all of the cloud path stuff like this:

const char * fileName = (strrchr(programVersion,'/') + 1);
Particle.variable("pgmVersion", fileName, STRING);

and note the change in file extension, that the compiler does:

@BulldogLowell cool!

Can you share the full code snippet? I copied both and pasted but doesn’t compile

Also, i opened an issue here: https://github.com/spark/docs/issues/180 if you don’t mind us sharing as an example!

1 Like

no problem:

#define COMPILED_FILENAME __FILE__

const char * programVersion = COMPILED_FILENAME;

void setup()
{
  const char * fileName = (strrchr(programVersion,'/') + 1);
  Particle.variable("pgmVersion", fileName, STRING);
}
//
void loop()
{

}
4 Likes

you can make it more compact (I wanted to show the variable type returned in the example) and use other C macros as well:

void setup()
{
  const char * fileName = (strrchr(__FILE__,'/') + 1);
  char fileInfo[125] = "";
  sprintf(fileInfo, "Filename: %s compiled on %s at %s UTC", fileName , __DATE__ , __TIME__ );
  Particle.variable("pgmVersion", fileInfo, STRING);
}
//
void loop()
{

}

yields:

3 Likes

Hi, I’m reading this forum since some days and new the Particle community.

I tried to run the code above on my Photon (firmware 0.4.5) and simply copy & pasted it to the Web IDE. Unfortunately it does not work for me. Only __FILE__ has correct value while __DATE__ & __TIME__ are empty.

Any idea what could be wrong?

Thx

1 Like

to be honest, I only tested using Particle Dev.

it looks like an issue with sprintf() on the Build IDE. Try this:

void setup()
{
  const char * fileName = (strrchr(__FILE__,'/') + 1);
  const char * theDate = __DATE__ ;
  const char* theTime = __TIME__;
  Particle.variable("pgmVersion", fileName, STRING);
  Particle.variable("date", theDate, STRING);
  Particle.variable("time", theTime, STRING);
}
//
void loop()
{

}

That’s working all there variables are available, so indeed it is related to sprintf(). I will try using Particle Dev later this after noon, but since that is compiled in the cloud as well, shouldn’t the result be the same?

Should it be the same? Seems to me ‘yes’

Are they the same? Well…

:wink:

1 Like

@BulldogLowell Mat mentioned about the issue in the Docs PR where the buffer becomes garbage after the setup() so you can take a look at his comments!

I didn't see that compiling in Particle DEV. Something wonky in Build, I think.

It’s the same build farm so the firmware version compiled against might be different.

See: https://github.com/spark/docs/issues/180#issuecomment-140594292

I had a similar thought a while ago :

I flashed the same program onto the same Photon and didn’t change the firmware. can you explain how would I get two different results?

Something must be different, Kenneth, no?

They use the same build farm so i will not say it is different. I mentioned earlier. Firmware version difference. ^^

I have no clue if Particle Dev allows firmware version selection. Dev and Build shares the same build farm, that i am very certain.

That code has a flaw - the variable buffer is allocated on the stack which will become overwritten once the setup() function exits.

To fix, move fileInfo to global scope.

1 Like