Position of Particle Variables/Functions in Setup

Hello everyone. I’d like to share the issue I was just troubleshooting for a while when my Photons transitioned from 0.5.3 to 0.6.0 firmware. First of all, I use SYSTEM_MODE(MANUAL). In setup(), I was first connecting to the cloud before declaring any particle.variables and particle.functions in my firmware. I was making some changes to my base firmware so I uploaded the new firmware to the Photon, which also caused the Photon to update to 0.6.0. After exiting setup, my Photon would reset due to the SOS Hard Fault after about 15 seconds of operation.

Before it would hit the SOS, I could see the registered particle.variables in Particle Dev, and strangely enough, the first particle.variable in the list had a garbage name that I did not create when it should have been “Temp” and Particle Dev was also spitting out an error. That error was as follows:
“Uncaught Error: Syntax error, unrecognized expression: #particle-dev-cloud-variables [data-id=44$o=45$p=34] td:eq(2)”.

The only thing I can do to get rid of the hard fault is change the location of Particle.connect in Setup(). If Particle.connect is after my particle.variable and particle.functions declarations, then the SOS goes away. Otherwise, it persists. :confused:

I tried to create a test case so this could be investigated further by Particle, but I’m unable to reproduce the issue with any other firmware other than the one I’ve described above. My firmware is over 10k lines of code so I won’t bother posting it here. I did see another post similar to this some months back, but they didn’t experience the SOS like I am.

So, the whole point of this post is to document this issue for anyone else who might experience this issue in the future.

Have you got a waitUntil(Particle.connected) before you set up your variables?
Manipulating cloud stuff while the system is also “messing” with the same data might cause race conditions.

On the other hand it’s safe to set up things first and only connect afterwards. I’d usually do it that way with non-AUTOMATIC modes anyway.
Since I’d only connect after I found need for it and not unconditionally in setup() (that somehow seems to defeat some of the purpose of these modes IMHO).

Great info @ScruffR. Thanks!
I wasn’t aware of the race condition you mentioned. I’ll configure all variables/functions before connecting to the cloud.