0

I am using Wemos Mini ESP8266 and trying to make control panel on this platform. But i have stuck when it needs to save data to a file.

Tried to using LittleFS method to save json to file, still causes crashing.

Here is my code:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <Wire.h>
#include <LittleFS.h>
#include <ArduinoJson.h>
#include <Arduino.h>

const char* ssid = "wifi01";
const char* password = "password01";
const char *dataFile = "/data.json";

unsigned long last_save = 0;

ESP8266WebServer server(80);

void handleRoot() {
  server.send(200, "text/plain", "Index page");
}

bool saveData() {
  LittleFS.begin();
  StaticJsonDocument<200> doc;
  doc["foo"] = 10;
  doc["bar"] = 50;

  File data = LittleFS.open(dataFile, "w");
  serializeJson(doc, data);
  LittleFS.end();
  return true;
}

void setup(void) {

  Serial.begin(9600);

  WiFi.mode(WIFI_AP);
  WiFi.softAP(ssid, password);

  IPAddress myIP = WiFi.softAPIP();

  server.on("/", handleRoot);

  server.begin();
  Serial.println("Server started");
  last_save = millis();
}

void loop(void) {
  server.handleClient();
  if(millis() - last_save > 5000){
    saveData();
  }
}

Here is an exception stack


Exception (3):
epc1=0x4010076d epc2=0x00000000 epc3=0x00000000 excvaddr=0x4006f441 depc=0x00000000

>>>stack>>>

ctx: cont
sp: 3ffffcc0 end: 3fffffd0 offset: 0150
3ffffe10:  402021dc 3ffffe20 ffffffff 00002000  
3ffffe20:  00000040 3ffef8fc 00000020 401009f4  
3ffffe30:  00000000 00000001 3ffef884 40202a3c  
3ffffe40:  0000ef6f 00000001 3ffefc36 3ffef884  
3ffffe50:  3ffeea44 3ffef884 3ffef87c 40204a1f  
3ffffe60:  00000008 3ffffed0 37400000 402044fc  
3ffffe70:  00000000 00000001 00000003 000011c0  
3ffffe80:  500ffc30 00010002 ffffffff ffffffff  
3ffffe90:  37400408 3ffffed0 00000005 00000005  
3ffffea0:  000007e0 000000fc 3ffe85ec 40100672  
3ffffeb0:  00000a80 00000150 3ffe85ec 40100672  
3ffffec0:  00000988 00000131 3ffe85ec 40100672  
3ffffed0:  00000cb8 00000197 3ffe85ec 3ffeeb54  
3ffffee0:  3ffeea44 3ffef884 3ffef87c 4020a97d  
3ffffef0:  3fffff1c 00000000 3ffef87c 4020aa7c  
3fffff00:  3ffeea44 00000000 3ffeeb28 402085b6  
3fffff10:  3ffe85d4 3fffda00 00000000 00000000  
3fffff20:  00000000 feefeffe feefeffe feefeffe  
3fffff30:  40207d0c feefeffe 40210584 40209f36  
3fffff40:  00040000 feefffff 00030000 ffff0020  
3fffff50:  402114e8 3fffff70 00000001 402109dc  
3fffff60:  3fffdad0 00000000 3ffee7f0 4020898d  
3fffff70:  4021126c 00000000 00001388 3fffff50  
3fffff80:  00000000 00001461 970a3d70 001790d6  
3fffff90:  00000000 00000000 feefeffe 3ffeeb54  
3fffffa0:  3fffdad0 00000000 3ffeeb28 3ffeeb54  
3fffffb0:  3fffdad0 00000000 3ffeeb28 4020e2c8  
3fffffc0:  feefeffe feefeffe 3fffdab0 40100c99  
<<<stack<<<

And here is decoded exception

Exception 3: LoadStoreError: Processor internal physical address or data error during load or store
PC: 0x4010076d
EXCVADDR: 0x4006f441

Decoding stack results
0x402021dc: lfs_dir_find_match at c:\users\������\appdata\local\arduino15\packages\esp8266\hardware\esp8266\3.1.2\libraries\littlefs\lib\littlefs/lfs.c line 1296
0x401009f4: malloc(size_t) at C:\Users\������\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\umm_malloc\umm_malloc.cpp line 912
0x40202a3c: lfs_init at c:\users\������\appdata\local\arduino15\packages\esp8266\hardware\esp8266\3.1.2\libraries\littlefs\lib\littlefs/lfs_util.h line 221
0x40204a1f: lfs_mount at c:\users\������\appdata\local\arduino15\packages\esp8266\hardware\esp8266\3.1.2\libraries\littlefs\lib\littlefs/lfs.c line 4097
0x402044fc: lfs_commitattr at c:\users\������\appdata\local\arduino15\packages\esp8266\hardware\esp8266\3.1.2\libraries\littlefs\lib\littlefs/lfs.c line 3880
0x40100672: umm_free_core(umm_heap_context_t*, void*) at C:\Users\������\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\umm_malloc\umm_malloc.cpp line 642
0x40100672: umm_free_core(umm_heap_context_t*, void*) at C:\Users\������\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\umm_malloc\umm_malloc.cpp line 642
0x40100672: umm_free_core(umm_heap_context_t*, void*) at C:\Users\������\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\umm_malloc\umm_malloc.cpp line 642
0x4020a97d: littlefs_impl::LittleFSImpl::_tryMount() at C:\Users\������\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\libraries\LittleFS\src/LittleFS.h line 285
0x4020aa7c: littlefs_impl::LittleFSImpl::begin() at C:\Users\������\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\libraries\LittleFS\src/LittleFS.h line 193
0x402085b6: saveData() at C:\Users\������\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/Memory/ResourceManager.hpp line 31
0x40207d0c: handleRoot() at C:\Users\������\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\libraries\ESP8266WebServer\src/ESP8266WebServer-impl.h line 518
0x40210584: std::_Function_handler ::_M_manager(std::_Any_data&, std::_Any_data const&, std::_Manager_operation) at c:\users\������\appdata\local\arduino15\packages\esp8266\tools\xtensa-lx106-elf-gcc\3.1.0-gcc10.3-e5f9fec\xtensa-lx106-elf\include\c++\10.3.0\bits/std_function.h line 268
0x40209f36: WiFiServer::accept() at C:\Users\������\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\libraries\ESP8266WiFi\src\WiFiServer.cpp line 151
0x402109dc: WiFiClient::operator bool() at C:\Users\������\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\libraries\ESP8266WiFi\src\WiFiClient.cpp line 342
0x4020898d: esp8266webserver::ESP8266WebServerTemplate ::handleClient() at C:\Users\������\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\libraries\ESP8266WebServer\src/ESP8266WebServer-impl.h line 282
0x4020e2c8: loop_wrapper() at C:\Users\������\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\cores\esp8266\core_esp8266_main.cpp line 258

I have readed about every forum to solve the problem, but it gave nothing.

7
  • Has your file system been initialized/formatted? Did you upload a basic empty filesystem? randomnerdtutorials.com/… Commented Feb 14 at 20:43
  • Yep, filesystem has been formatted. And i have tested LittleFS with examples from ESP8266, it is working there. I think deal in handleClient, but i dont know how to solve this. Commented Feb 14 at 21:18
  • Well, no, the stack trace shows that the exception occurred during LittleFS::begin, so it's clearly something related to the file system. That line (1296) is calling its comparison callback to decide whether the name is a match. What happens if you leave off the leading /? Commented Feb 15 at 0:00
  • Just tried to format filesystem and remove "/" from file path, but nothing changes. In my real code i am printing almost every action, so I know exception is rasing when i use "serializeJson" or "file.write" methods. Commented Feb 15 at 7:21
  • Your code had loop calling saveData, but the stack shows handleRoot calling saveData. Are we seeing the actual code? Or do you have a different stack dump? Commented Feb 15 at 19:02

1 Answer 1

0

as suggested by @hcheung after 5 second save data may call continuously which can append data multiple time and can cause out of memory.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.