-->
Page 1 of 1

Using delay() causes ESP to reset

PostPosted: Wed Nov 04, 2015 5:44 pm
by intersect
Hello there,
first of all, many thanks for this great library and toolset for ArduinoIDE to work with ESP8266.

I have latest stable version ( 1.6.5-947-g39819f0). I started to write simple code to read null terminated strings using client.read() with simple timeout functionality.
Code: Select all#include <ESP8266WiFi.h>

const char* ssid = "NETWORK";
const char* password = "";

//random port :D
WiFiServer server(1234);

void setup() {
  Serial.begin(9600);
  Serial.println("Initializing device, awaiting TCP connection...");
  delay(10);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
  Serial.println("WiFi connected");
  server.begin();
  Serial.println("Server started at: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  String s = readLine(client);
  Serial.println(s);
}

String readLine(WiFiClient client){
  int delayCnt = 0;
  String line = "start";
  while(delayCnt < 1000){
    int i = client.available();
    if(i > 0){
      char next = client.read();
      if(next == 0){
        break;
      } else {
        line += next;
      }
    } else {
      delayCnt++;
      delay(1);
    }
  }
  return line;
}


I have read that calling delay() should call all necessary background functions to preserve Wifi and TCP/IP functionality. So I believed this code should be OK without breaking anything. But if I dont send any string to read and cause timeout to kick in, my ESP restarts itself writing few random-ish bytes to serial.

So I tried to comment this function out and just call delay(2000) directly in loop() and same thing happens.

It seems to me that blocking loop for longer than 100ms causes ESP to restart, even thou delay() should prevent this. Is there anything I do wrong, or is this a bug?

Thank you.

Re: Using delay() causes ESP to reset

PostPosted: Thu Nov 05, 2015 2:12 pm
by igrr
Most certainly, delay should not be causing a reset. I have tried uploading an empty sketch, with only delay(2000); added to loop(), to a NodeMCU 1.0 board. Doesn't seem to crash. Could you please share the minimal example which causes your board to reset? I'm interested in the sketch and .elf output file (enable verbose compiler output in arduino preferences to see its path).

Re: Using delay() causes ESP to reset

PostPosted: Sat Nov 07, 2015 4:51 pm
by intersect
In your scenario I have no problems with the chip restarting itself. Delay causes restarts only when wifi is initialized and connected. A have attached scetch and elf files as you requested. Plese fill in your wifi credentials if you want to test it out.

I have tried this example on 2 separate 8266 chips powered exactly at 3V3 with same result (R.I.P. third chip I accidentally connected to 5V5 unregulated supply :( ).

I have my own Java application to connect to ESP, but I believe even simple HTTP request from browser should suffice to pass thru connection check.

Code: Select all#include <ESP8266WiFi.h>

const char* ssid = "";
const char* password = "";

//random port :D
WiFiServer server(1234);

void setup() {
  Serial.begin(9600);
  Serial.println("Initializing device, awaiting TCP connection...");
  delay(10);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
  Serial.println("WiFi connected");
  server.begin();
  Serial.println("Server started at: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  Serial.println("Delay 2000");
  delay(2000);
  Serial.println("End of delay 2000");
}