Particle variables and functions disappear when Particle.publish with particular Strings

Sometimes when I flash my Boron, all my Particle.function and Particle.variable registrations stop working. By that I mean they don’t show in the web console, cli (particle list) and the functions return a 404 when I try to execute them over the cloud API, function not found. I don’t get an error when flashing, everything seems fine.

I’ve worked out that it is caused by certain calls to Particle.publish, but only sometimes. For example, this code worked fine one day, then the next day not.

StaticJsonDocument<1024> doc;
  DeserializationError error = deserializeJson(doc, args.c_str());
  if(error) {
    String errorDetails = String(error.c_str());
    String message = String("parseObject() failed: " + errorDetails);
    Particle.publish("cc/post/error", message, PRIVATE); // this is the line that causes the issue, today
    return 0;
  }

It’s tedious to debug as I have to comment out all my Particle.publish calls and add them back in one by one to see which one is causing the issue.

Anyone have any suggestions? Thanks in advance :slight_smile:

Device OS 1.5.0

Where, when and how do you register your variables and functions?
Do the variables and functions show up before you publish and then disappear or are you publishing before you even register them?

Thanks Scruff :slight_smile:

I register variables and functions right… hmm now that you mention it, would having Mesh.off() before registering cause an issue?

void setup() {
  Mesh.off();
  Particle.variable("config_readingFrequency", config_readingFrequency);
  Particle.function("publishSensorReading", publishSensorReading);
  Particle.function("setConfig", setConfig);

I have done some more testing with this and what I can say is that for a period of about 15 minutes, changing the value of message to be a simple string (no concat, no brackets) “fixed” the issue consistently (I went back and forth a few times to confirm it was a consistent pattern). Now I have just re-flashed with the exact original code that seemed to be the issue, and it’s “fixed”.

I realise reading your response that I just assumed the Particle.publish line never gets executed because it only runs if there is a DeserializationError, which generally shouldn’t happen… but maybe it is sometimes. I will do some more testing tomorrow to confirm this.

Appreciate your input!

1 Like

Nope, that should not impact the registration.

BTW, we usually encourage people to stay away from String and rather use char arrays in conjunction with snprintf() to construct strings.

Also about this

Why that?
When error already "is" (sort of) a String why would you need to convert it to a const char* just to wrap it back into another String which is then assigned to yet another String?
You can directly assign a const char* to a String or use it in your concatenation.

Here an alternative

  char msg[64];
  snprintf(msg, sizeof(msg), "parseObject() failed: %s", error.c_str());

This saves the creation of 4+ temporary String objects.

This is my first time writing c++, total noob :upside_down_face:
I had a couple assumptions in there that lead to that really inefficient and unnecessary code. So thanks for the helpful suggestion, my OCD is very pleased!

I'll post here again if the original issue I was having pops up again.

1 Like

I think it might be the length of your function and or variable name. IIRC, there is a 12 character limit.

That restriction has been at 12 characters until 0.8.0 from then on it was/is 64.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.