Use this forum to chat about hardware specific topics for the ESP8266 (peripherals, memory, clocks, JTAG, programming)

User avatar
By wisipior
#94903 Howdy,

Ive got problem of ESP8266 nature. I use Arduino IDE libraries ver 3.0.2.

My project involves ESP8266 unit sending data to ESP8266 operation station. Sometime the first unit has to download data from the station onto its flash memory.
Thing is that sequential write operations completes only if WiFi is not connected.
I have isolated the code so the simple sequentail write loop of zero-filled buffer would finish correctly only if I comment out whole Wifi section.
Ive heard about Serial.Flush() necessity as well as yield() and I confirm without those the loop starts even worse.
So can anyone tell me how is this possible that active WiFi connection interferes flash write operations.
I also confirmed that reading instead of writing completes without error.
I also noticed that the writing interrupts exactly after writing 7th, 15th or 31th block of 1024 bytes.
Below the isolated code.

Code: Select all#include <ESP8266WiFi.h>
#include <LittleFS.h>

void setup() {
  Serial.begin(115200);
  LittleFS.format();
  Serial.println("Formatted");
  LittleFS.begin();
  delay(1000);
 
/*  - if this commented - further code would complete fine
  WiFi.persistent(false);
  WiFi.mode(WIFI_STA);
  WiFi.begin("ssid", "passwd");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(100);
  }
  Serial.println("Connected.");

*/ 

char fnam[] = "/server.bin";
uint8_t buf[1024] = {0};

  Serial.flush();           
  File f = LittleFS.open(fnam, "w");
  if (!f)
    Serial.println("error file not opened");
  else {
    int len = 0;
    Serial.println("opened");
    delay(10000);
   
    while (len < 200) {
      yield();
      delay(50);
      Serial.flush();       
      delay(50);
      f.write(buf, 1024);
      delay(50);

      len++;
      Serial.print("#");
      if (len % 32 == 0)
        Serial.printf(" %d kB\n", len);
    }
    f.flush();             
    Serial.printf(" %d kB Done.\n", len);
  }
  f.close();

  LittleFS.end();             
  WiFi.disconnect();
  Serial.flush();
  Serial.end();
  yield();
  delay(7000);
  ESP.restart();
}

void loop() {
}