1

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?

  • 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
-1

Insert refTime = millis(); inside while loop.

| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.