I'm experimenting with ESP8266 and I want it to cycle through various states: LED on/off, WiFi on/off and measure the connection time, deep sleep.
I wrote the following script, using millis()
to define the time intervals. I cut away the previous parts of the code to have a minimum test case.
I read that ESP8266 has the watchdog automatically on, so I used yield()
when spending time waiting, instead of using an empty while loop. I didn't use delay()
because it block further execution of the code, and I tend to avoid it. I know it would reset the watchdog too.
#include <ESP8266WiFi.h>
const char* ssid = "wifi";
const char* password = "pass";
const int led = 2;
void setup(void){
unsigned long refTime;
pinMode(led, OUTPUT);
Serial.begin(115200);
digitalWrite(led, 1);
refTime = millis();
// isssue from here
while ((millis() - refTime) < 3000) { yield(); }
// to here
Serial.println("Connecting...");
refTime = millis();
WiFi.begin(ssid, password);
Serial.println("");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// never gets here when "issue" code enabled
Serial.println(millis() - refTime);
digitalWrite(led, 0);
}
void loop(void){
}
If I use the yield()
, then I never get a WiFi connection (well, it happened only once, actually).
Why is yield causing issues?