Photons reporting incorrect Device Name as "lantern_em"

I have several photons in production use, and just now I’m noticing that at least two of them are reporting a device name (via Particle.subscribe("particle/device/name", deviceNamehandler); of “lantern_em”. See attached screenshot for an example. Are devices getting crossed somehow in Particle Cloud?

It just corrected itself. Particle team, can you provide an explanation so that, if it’s something I’m doing wrong in my code, I can avoid it in the future?

You may want to show the code that published that event.

e.g. publishing from within a subscribe handler is a no-go (or only with precautions).

I have a global variable deviceName which is what my code is pulling from in the screenshot:

String deviceName;
Particle.variable("devname",deviceName);

In my setup() loop, I have:

Particle.subscribe("particle/device/name", deviceNamehandler);
Particle.publish("particle/device/name");

And my deviceNamehandler is:

void deviceNamehandler(const char* event, const char* data)
{
    char dmy[strlen(data)+1];
    strcpy(dmy, data);
    deviceName = String(dmy);
}

Anything in here that I should be doing differently?

I don’t quite understand why you use the double copy operation.
I’d just use a global char deviceName[32] and strcpy() into that. Paticle.variable() will also take a char[] as valid variable.

If you want you can also initialise deviceName in setup()

2 Likes

Thanks – you’re right, no reason for the double-copy, I think it was just quickly reusing some code. :slight_smile:

The lanterm_em device name is still happening to at least one other device now. Hmm… I wonder if it’s because I’m subscribing to particle/device/name as a public event, and someone is posting that publicly? I should change this to Private / MY_DEVICES.

1 Like

I am having the same problem with “lantern_em”. I have 5 Photons that run the same code but I need them to act differently depending on their device name. I detected this problem because the wrong name appears in subscribe messages (that I log). It seems like “lantern_em” is running my code. Hey ScruffR! Are you going to clear this up?

I must say, I never got any stray events in my subscriptions and hence it’s a bit difficult to advise.

If I only could reproduce the issue, diagnosis would be easier (if at all possible).

But for one thing, I always try to use PRIVATE/MY_DEVICES for my publish/subscribe statements. IIRC device name request will still work that way.

Yes @rch, what fixed it for me was to use PRIVATE. Otherwise, we are subscribing to the whole world’d events, and apparently lantern_em is publishing his device name publicly. :smile:

  Particle.subscribe("particle/device/name", deviceNamehandler, MY_DEVICES);
  Particle.publish("particle/device/name", NULL, 60, PRIVATE);

Note, after implementing this, I discovered there is a function to get the device’s ID which is probably a better way to differentiate between devices. I don’t know it offhand but stumbled upon it while looking for something else.

Ok. I added the MY_DEVICES/PRIVATE args but the problem didn’t go away.

Here’s my pub/sub log from last night–
1/31/2018,21:01; fmotor, fm5: bothDown, c:f, etime=88, lastrd=0~
1/31/2018,21:03; fmotor, fm2: bothDown, c:f, etime=221, lastrd=1017~
1/31/2018,21:03; fmotor, lantern_em: bothDown, c:f, etime=221, lastrd=2273~
1/31/2018,21:03; fmotor, lantern_em: bothDown, c:f, etime=221, lastrd=0~
The first 2 lines are valid, the last 2 are not.
Looks like whoever/whatever lantern_em is is running my code.

You’re 100% positive there are no stray Particle.subscribe lines out there without MY_DEVICES on them? I’m no expert here, but based on my understanding, that should prevent you from picking up any outside published messages.

Here is my current code where I get my device name (BTW: your Docs example does not show using MY_DEVICES/PRIVATE):

Particle.subscribe("particle/device/name", getname, MY_DEVICES);
Particle.publish("particle/device/name", PRIVATE);

Yesterday I added this:

if(strncmp(DevName, "fm", 2) != 0) { // My device names all start with "fm"
  sprintf(Estr, "ERROR: %s -- not my device", DevName);
  Particle.publish("fmotor", Estr, 100, PRIVATE);
}

I still got the lantern_em entries (and NOT that error message).
All of the publish calls using the “fmotor” tag have been updated as PRIVATE.

Also, your support functions should just have a safe direct call like: Particle.deviceName(). I can’t be the only person who wants to do identify different Photons running the same code.

I don’t know if this is related to your problem, but according to the doc here (you have to scroll down near the bottom of this function’s doc): https://docs.particle.io/reference/firmware/core/#particle-publish-

In order to publish a private event, you must pass all four parameters.

Meaning your publish line should actually be:
Particle.publish("particle/device/name", NULL, 60, PRIVATE);

However I don’t see how that would affect your Subscribe, which uses MY_DEVICES and looks identical to the code I’m using. That said, it couldn’t hurt to try.

I’m assuming you’ve read all the doc here and are following their guidelines? https://docs.particle.io/reference/firmware/core/#particle-subscribe-

Other than that, I’m afraid I’m not much help.

Re 4 parameters: Thanks, I’ll try that. None of this explains why lantern_em got my pub tag name.

And if that still doesn’t work, you could always differentiate your devices by the deviceID. I found the function for that:
System.deviceID()

Hi @rch

Can you show us the subscribe handler code? In particular if you are calling Particle.publish from the subscribe handler, you should stop doing that. The buffer for incoming and outgoing events is a circular buffer without any copying and calling publish from within a subscribe can cause problems. Set a flag and do the publish after control returns to your loop() function.

2 Likes

Actually providing a full running example that exherts this issue as early as possible (preferably in the opening post) would be the only way that doesn’t leave any questions open for guesses and helps getting to the root of things much quicker.

For example the event name shown in the screenshot in the first post suggests some kind of buffer corruption (e.g. due to Particle.publish() inside the subscribe handler or timer callbacks).

1 Like

Re 4 args to Particle.publish:
I finally got all of my Photons changed to that and the lantern_em messages have disappeared. This doesn’t explain why/where they came from. Also:

Why Particle.Docs’s example leaves off the security parameters?

No idea either, but I also couldn’t reproduce the issue whatsoever.
Have you got a full running sample to test this with?

1 Like