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);
  digitalWrite(led, 1);

  refTime = millis();
// isssue from here
  while ((millis() - refTime) < 3000) { yield(); }
// to here

  refTime = millis();
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
  // 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?

| improve this question | | | | |
  • why not just delay() 3 seconds? – dandavis Sep 25 '17 at 21:36
  • Because this way I can add some other code to the main loop and still get the blinking on time. I don't need it in this example, but I find it often useful. Still, why isn't it working? See also learn.sparkfun.com/tutorials/esp8266-thing-hookup-guide/… – FarO Sep 25 '17 at 22:50
  • the code looks alright, but maybe try a delay(1) in the loop instead of yield()... – dandavis Sep 26 '17 at 0:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.