Can Particle.connect() be called in a thread?

I am trying to call Particle.connect() in a thread. Before this thread, the WiFi credentials are set and WiFi.connect succeeded. The check is as follows.

os_thread_return_t WorkerThread(void *p){
for( ;; ){
bool status = waitFor(Particle.connected, 40000);

After calling Particle.connect(), the particle LED blinks red and ends up in rapid blinking cyan. The console output is follows:
0000363394 [system] WARN: Cloud handshake failed, code=-1
0000363401 [system] INFO: connected to cloud
0000363401 [system] INFO: Cloud socket connected
0000363401 [system] INFO: Starting handshake: presense_announce=1
0000363401 [comm.sparkprotocol.handshake] INFO: Started: Receive nonce
0000363486 [comm.sparkprotocol.handshake] INFO: Encrypting handshake nonce
0000363633 [comm.sparkprotocol.handshake] INFO: Sending encrypted nonce
0000363636 [comm.sparkprotocol.handshake] INFO: Receive key
0000363644 [system] INFO: Cloud: disconnecting
0000363645 [system] INFO: Cloud: disconnected
0000363646 [comm.sparkprotocol.handshake] ERROR: Unable to receive key -1
0000363646 [system] WARN: Cloud handshake failed, code=-1
0000364646 [system] INFO: Cloud: connecting
0000364646 [system] INFO: Read Server Address = type:1,
0000364653 [system] INFO: Resolved host to
0000364704 [system] INFO: Cloud: connecting
0000364705 [system] INFO: connected to cloud
0000364705 [system] INFO: Cloud socket connected
0000364705 [system] INFO: Starting handshake: presense_announce=1
0000364705 [comm.sparkprotocol.handshake] INFO: Started: Receive nonce
0000364707 [comm.sparkprotocol.handshake] ERROR: Could not receive nonce: -1
0000364707 [system] WARN: Cloud handshake failed, code=-1
0000364957 [system] INFO: Cloud: disconnecting


You might need !Particle.connected()? (the !)

1 Like

Sorry that was a typo. I am using if(!Particle.connected()) in my code. That’s why it called Particle.connect() and went into rapid blinking cyan.