Photon device name not returning anymore

Hi there,

For a while I have this code in my Photon:

void handler(const char *topic, const char *data) {
    String deviceName = String(data);        
    Particle.publish("received " + String(topic) + ": " + deviceName);
}
void getDeviceName() {
    Particle.subscribe("spark/", handler);
    Particle.publish("spark/device/name");
}

In setup I call getDeviceName()
Code is based on: https://docs.particle.io/reference/firmware/photon/#get-device-name

But now the handler is not being called anymore! :frowning: My device is dependent on it’s device name to determine the location it’s in (i.e each photon has a different name defining it’s location, so I can run the same code on multiple devices)

Are there more people having this issue? Thanks!

Would you mind sharing the rest of the code, since that might provide some additional insights?
Which mode are you running in? Are you using threading?

Re threading: not as far as I can tell (didn’t even know you can make threads on photon)

And rest of the code is a bit hard since it’s a paid project where my client owns the code, but I can run a minimal version and see if that works.

A minimal version would be even better, since that will help narrow down potential issues. If that doesn’t work, we can possibly take a look at the code in a PM, to see if there’s something sketchy going on.

1 Like

One criticla point is when you register the subscription. That should happen no longer than 20sec after cloud connect - best would be immediately in void setup() (as shown in the docs).

1 Like

HI there,

I just made the minimal version (full source code below) and it returned my device name! So whatever it is, it’s not related to the particle cloud but in my code somewhere.

void handler(const char *topic, const char *data) {
    String deviceName = String(data);        
    Particle.publish("received " + String(topic) + ": " + deviceName);
}
void getDeviceName() {
    Particle.subscribe("spark/", handler);
    Particle.publish("spark/device/name");
}
void setup() {
    Particle.publish("Getting device name");
    getDeviceName();
}
void loop() {
    
}

Alright I found the issue. Apparently mqtt conflicts with the device name function. I now call mqtt.loop when the device name is set at least once in a global variable. This solved the issue but am not exactly sure how.

I just submitted this as a “support request”:
I need to detect the Photon device name (the one I assigned to it) from inside a script. I copied the code from Particle’s reference docs:

Particle.subscribe("spark/device/name", handler);
Particle.publish("spark/device/name");

Silly me. I assumed that the use of “spark” was past its sell-by-date so I changed it to “photon”. Didn’t work (returned “null”, no error). But “spark” worked. When are you going to stamp out the old name? What’s it been? 2 years?

Spark was the company name and Core their “first” product.
Now the company is called Particle and the Photon is one of their products.

Hence particle/device/name would be the correct evolution from spark/device/name :wink:

If you try the correct substitute you may find it has already been taken care of :sunglasses:

It has NOT been taken care of. Did you read my submission all the way through? I have a core, 9 photons & an electron. I remember the spark/particle change.

What submission?

I read this

And "didn't work" wasn't much of a surprise when using photon instead of particle in that context.

But this works as expected

void setup() 
{
  Particle.subscribe("particle/device/name", handler);
  Particle.publish("particle/device/name");
}

void handler(const char* event, const char* data)
{
    char dmy[strlen(data)+1];
    strcpy(dmy, data);
    Particle.publish("response", dmy, PRIVATE);
}
1 Like