Get the current core ID on the core itself?

Is there a way to get the core’s ID in the firmware itself? Something like a Spark.deviceID variable? I’m creating multiple, similar devices that need to identify themselves (over TCP or UDP) to a RaspPi server.

1 Like

We don’t have a nice abstracted way to do this; implementing Spark.deviceID() seems like a good solution. Putting it on the backlog. In the meantime, you can read out from the ID1 memory address. Here’s an example from some of our code:

{
     char id[12];
     memcpy(id, (char *)ID1, 12);
     print("Your core id is ");
     char hex_digit;
     for (int i = 0; i < 12; ++i)
     {
       hex_digit = 48 + (id[i] >> 4);
       if (57 < hex_digit)
         hex_digit += 39;
       serial.write(hex_digit);
       hex_digit = 48 + (id[i] & 0xf);
       if (57 < hex_digit)
         hex_digit += 39;
       serial.write(hex_digit);
     }
     print("\r\n");
   }
4 Likes

Thanks @zach , this works great!! Here is the code for it. The only thing is that while testing the code, I was able to get good results reading the results over serial, but I can’t read it over the cloud API. Do you know why?

#include <string.h>
char* id3;
char id[12];
char id2[100];
void setup() 
{
     Serial.begin(9600);
     
     Spark.variable("id3", &id3, STRING);
   }

void loop() {
delay(1000);
id3 = coreid();
Serial.println("The Core id is: ");
Serial.println(coreid());

}

char* coreid()
{
     int x=0;
     memcpy(id, (char *)ID1, 12);
     char hex_digit;
     
     for (int i = 0; i < 12; ++i)
     {
       hex_digit = 48 + (id[i] >> 4);
       if (57 < hex_digit)
         hex_digit += 39;
         id2[x]=hex_digit;
         x = x +1;
       hex_digit = 48 + (id[i] & 0xf);
       if (57 < hex_digit)
         hex_digit += 39;
         id2[x]=hex_digit;
         x = x +1;
     }
     return id2;
}

This was implemented in the core some time ago; see this thread:

It returns an Arduino String class object, so myString.toCharArray will get you a C string.

Returning the ID in a :spark: variable is a little odd since you need to know that device ID in order to get variables.

Thanks @bko I wan’t aware of that post!

Yeah I know it is odd to read the spark Id over the air, but I guess I wasn’t clear on that point. I’m not familiar with char* and I whenever I want to read a char* over the cloud, I can’t get to see the value. I guess it might be because it is a pointer?

Cheers
Manu

Ok, you need to pass Spark.variable the string with an ampersand in front. It helps sometimes to read char * as pointer to char string. You can read &stringvar as address of stringvar.

char resultstr[64];

void setup()
{
  Spark.variable("result", &resultstr, STRING);
}

Good luck!

Thanks @bko Yeah I was able to read strings before but I need to get more familiar with char *

Cheers