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.
DeserializationError error = deserializeJson(doc, args.c_str());
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
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.
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.
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
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.
This is my first time writing c++, total noob
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.