Enabling WiFi

Someday I had this idea: I will use this device as xmpp client!
We could communicate outside while in range, or inside over internet (for example from school to home)…

Pretty brilliant, I told myself… was satisfied and excited to code.
I included classic <WiFi.h>, <WiFiClient.h> and so on, then WiFi.mode(WIFI_STA); WiFi.begin... hit the upload button and…

…and suddenly I was very sad, because of this:

Free heap before WiFi: 280020
..
IP address: 
192.168.2.82
Free heap after WiFi: 242024
UID: 0xc402829ef0c8
[Error]	(8.388, +8388)	 lv_mem_alloc: couldn't allocate memory (83548 bytes) 	(in lv_mem.c line #138)
[Error]	(8.388, +0)	 lv_mem_alloc: used:      0 (  0 %), frag:   0 %, biggest free:      0 	(in lv_mem.c line #143)
[Warn]	(8.397, +9)	 lv_gif_set_src: Could't load the source 	(in lv_gif.c line #78)
Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC      : 0x4012ad5b  PS      : 0x00060630  A0      : 0x8012af44  A1      : 0x3ffb3580  
A2      : 0x00000000  A3      : 0x3f40d66c  A4      : 0x0000004e  A5      : 0x3f40d6d0  
A6      : 0x3f40d654  A7      : 0x0000005a  A8      : 0x000000ff  A9      : 0x000000ff  
A10     : 0x00000019  A11     : 0x3ffb80c0  A12     : 0x0000004e  A13     : 0x00000014  
A14     : 0x00000009  A15     : 0x3f40d6d0  SAR     : 0x00000004  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x0000000c  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xfffffffe  

Backtrace: 0x4012ad5b:0x3ffb3580 0x4012af41:0x3ffb35a0 0x400d6c17:0x3ffb35c0 0x400d2b3e:0x3ffb35e0 0x400d2f67:0x3ffb3630 0x40138a43:0x3ffb3660 0x40089519:0x3ffb3680

Rebooting...

No matter what I do and where I call this new WiFi code, it crashes. The memory is surely not full, as you can see, I used Serial.println(ESP.getFreeHeap());, we have plenty. I’ve got IP, no problem here.

So… where is the problem and why it crashes?
I want WiFi on my ESP32! :slight_smile:

Hello!

The memory on the Chatter is pretty stretched as it is, and the WiFi stack also needs quite a bit of memory to operate, so it isn’t unreasonable to assume there simply isn’t enough free memory to have both WiFi and the rest of the firmware.

Sure, ESP.getFreeHeap() says that there is enough total free memory on the heap, however, that memory is fragmented across several blocks, and when allocating memory (LVGL is trying to allocate 83.5kB in this case), you need a contiguous block of memory for the allocation to succeed. You can use ESP.getMaxAllocHeap() to get the largest free contiguous block of memory on the heap, i.e. the maximum amount of memory you can allocate with a single malloc. Here’s a good illustrated explanation of what memory fragmentation is: Memory Fragmentation, your worst nightmare | Software Verify

Memory fragmentation, in general, is a pretty complex issue to tackle. From the trace you posted, it seems like you used the WiFi stack, connected to an AP, got an IP successfully, but now because of the WiFi stack, LVGL (the UI library) can’t allocate the memory it needs. I can’t pinpoint in which part of the code that particular allocation happens, but if it’s some less-used part of the software, maybe you could exclude that feature or optimize it so it needs less memory.

Let me know if I can help you further in any way.

Filip

Thank you for the answer. I made another attempt with correct function:

ESP.getMaxAllocHeap() before wifi.setup():
113792
...
IP address:
192.168.2.82

ESP.getMaxAllocHeap() after wifi.setup():
113792

So it seems that WiFi init takes some other free memory chunk and some 100k+ chunk is avail. Good. So where it is used? I pinpointed that:

FSLVGL::loadCache();

eats 30k memory, so only 72812 remains.

The last command executed is:

    auto intro = new IntroScreen([](){
		Shutdown.begin();
		Buzz.begin();
	});

which crashes like as:

[Error]	(13.580, +13580)	 lv_mem_alloc: couldn't allocate memory (83548 bytes) 	(in lv_mem.c line #138)

I really like to have that WiFi :frowning: What is in that cache? Do I have to use it? It’s 30k and we are approx 10k short, so can I save something here?

FSLVGL::loadCache(); loads the images used in the UI from the flash into the RAM. Since accessing data from the flash can be quite slow, especially for images and animations, loading them into RAM speeds up the whole UI by a lot.
If you wish, you can completely omit that line and everything will keep working as before, although you will see some stuttering in the UI.

Filip

You were right, it is working without that loadCache(). Thanks to additional available memory I have usable WiFi now. But as you mentioned, the experience in UI is really bad, almost no animation and A LOT OF stuttering. So now it’s about compromise. Although I appreciate nice animated GUI (which I expect is primarily targeted for kids to be attractive), I am practical person and do not need any fancy stuff, so maybe I will learn how to use LVGL and simplify things to bare minimum, which then will fit into the cache.

It’s pretty complex, though. Many screens and classes… I’d like to code in Platformio, but have some troubles with setting up the project and all dependencies. Is there any guide specific for Platformio and Chatter 2?

Hey @HANAX,

we are glad that you were able to make it work :slight_smile:

Regarding Platformio, we only have guides for Nibble, JayD, Spencer, and Ringo. Unfortunately, we won’t be making new PlatformIO packages in the near future.

Best,
Sandi