I’ve been developing some code on photon and everything has been working great. I’m now starting to run it on Electron but immediately faced weird problems with software timers. All in all it’s a long piece of code but I simplified it down a bit and this works:
With the software timer example end result is that it does one ping and then electron keeps just breathing cyan as if everything was fine. However if any kind of action from cloud is tried (OTA flash or function call or anything) Electron just flashes several times red and then restarts itself. I’ve tried with both beta and production electron and both have 0.5.1 flashed. Any ideas? Can anyone else replicate this?
You can’t call Particle.publish() from a software timer. Yes, I realize it mostly works on a Photon, and it should be better documented. The problem is that software timers run in a separate thread. Particle.publish can’t be called from a thread other than the main loop thread.
Hmm ok. That definately should be documented somewhere. So I guess I’ll have to use some flags to work around it. Are there any reasons why in the upcoming versions Particle.publish can’t be made to work inside timer functions?
Flags or just timing using millis() in loop is the way to go. While Particle.publish I suppose could be made thread safe, it’s also a blocking call, and you shouldn’t run blocking code in a software timer, anyway.
Software timers run on their own dedicated thread, separate from the system and user threads. All of the timers run off a single thread, however, so if you block in a timer callback, it will block other timers.
Also, the software timers run with a much smaller stack than the user thread, so it’s best to avoid doing anything significant in a software timer callback.