-->
Page 1 of 2

memory optimization tips for esp8266

PostPosted: Fri May 19, 2017 2:19 am
by debojitk
Hi Everyone,
Being a c++ and embedded newbie I have put best of my efforts to develop the following codebase:
https://github.com/debojitk/WifiDoorLock
Details of the project are listed here:
https://hackaday.io/project/13478-smart ... -door-lock

Recently I started working on security implementation on the connections, so I tried to use Websockets library by Links2004, and used wss protocol. Now the problem is when I am using plain websocket (no ssl) the available memory is ~12kb. Whenever I change it to use wss the connection itself fails ad the esp restarts. The obvious reason being the wss implementation itself that uses WifiClientSecure api behind the scene. I tested with a sample sketch that every wss connection eats up up to 10-11kb memory. With 3 connections created I am left with 10kb memory.
So it is very obvious that using wss with my existing code will fail as is has got only 11 kb left.

Its a humble request to you all to have a look at my codebase ad suggest some fine tuning or design changes to optimize memory usage or any alternate way to implement security.

Thanks in advance.
Debojit

Re: memory optimization tips for esp8266

PostPosted: Fri May 19, 2017 4:09 am
by Pablo2048
Hi debojitk,
it seems like You have almost every character array (aka string) in RAM - try to use F(""), FPSTR macros and strcmp_P functions... IMHO You have to study how to use PROGMEM for storing constants...

Re: memory optimization tips for esp8266

PostPosted: Fri May 19, 2017 4:49 am
by debojitk
Pablo2048 wrote:Hi debojitk,
it seems like You have almost every character array (aka string) in RAM - try to use F(""), FPSTR macros and strcmp_P functions... IMHO You have to study how to use PROGMEM for storing constants...

Yes, that's true. But I am using debug switches, so, i will switch off those debug statements. that might save 4/5 kb. What are the other parts where I can save?
I have a commands array with the following definition:
Code: Select allchar commands[][30]={
      "UDP_PAIR_BROADCAST",
      "UDP_CONNECT_BC_REQUEST",
      "UDP_CONNECT_BC_RESPONSE",
      "UDP_CONNECT_BC_RETRY",
      "UDP_CONNECT_BC_STARTHB",
      "START_COMM",
      "STOP_COMM",
      "START_RECORD",
      "STOP_RECORD",
      "MIC_RECORD_START",
      "MIC_RECORD_STOP",
      "START_PLAY",
      "STOP_PLAY",
      "RESET",
      "NOTIFY",
      "HELLO",
      "OPEN_DOOR",
      "CLOSE_DOOR",
      "SAVE_CONFIG",
      "LOAD_CONFIG",
      "DELETE_FILE",
      "GET_MESSAGES",
      "FORMAT",
      "FREE_SPACE",
      "SD_WRITE_TEST",
      "TEST_NOTIFY",
      "RESTORE"
};


I think this can go to PROGMEM. In such case should I use strcmp_P functions?

Re: memory optimization tips for esp8266

PostPosted: Fri May 19, 2017 5:56 am
by Pablo2048
Your code is full with this unoptimized stuff like:
SD.remove("testsd.txt") -> SD.remove(F("testsd.txt"))
properties.put("mode","station") -> properties.put(F("mode"), F("station"))
...
For accessing strings in PROGMEM there are lot of articles / for example https://www.arduino.cc/en/Reference/PROGMEM