Stack Overflow When Reconnecting


#1

I was testing some code for turning off and on the WiFi, and got a stack overflow error with the following simple code. The SOS is shown twice, then it connects like it should. Any ideas why this is happening?

Timer fifteenSecondTimer(15000, turnOffWifi, true);
Timer resetTimer(30000, connectToCloud, true);

 SYSTEM_THREAD(ENABLED);
 SYSTEM_MODE(SEMI_AUTOMATIC);

void setup() {
	Serial.begin();
	Time.zone(-7);
 	connectToCloud();
}

void loop() {}

void connectToCloud() {
	  if (!Particle.connected()) {
		  Particle.connect();
		  if (waitFor(Particle.connected, 60000)) {
			  Serial.println("starting timer");
			  fifteenSecondTimer.start();
		  }else {
			  System.reset();
		  }
 	  }
  }


  void turnOffWifi() { // handler for fifteenSecondTimer
	  Particle.disconnect();
	  WiFi.off();
	  resetTimer.start();
  }

This is on a Photon running 0.7.0


#2

Not sure why the stack overflow, but I had forgotten about timer callbacks being similar to ISR, and therefore should return quickly. If I only set a flag in the resetTimer callback function, I no longer get the SOS.


#3

Correct, you can’t make those calls from a timer.

The reason for the stack overflow is that software timers (Timer class) run from a separate thread with a 1024 byte stack. The normal loop thread has a 6144 byte stack.