Re: in desperate need for RAM to run mqtt over TLS
Posted: Sun May 26, 2019 6:28 pm
If you get a detailed memory map from the build, you'll at least be able to see where your RAM is going, and then you can start looking at moving functions and data to flash. I found using -DUSE_OPTIMIZE_PRINTF made a big difference as it moved all printf strings into flash. I also found I had a couple of functions that I had left the ICACHE_FLASH_ATTR directive off - I now only have a couple of interrupt service routines that are in RAM.
The other thing I found was that there are several problems with the default esp_mqtt code, and one that may be affecting you is that it allocates a 2kB data array on the heap during publish routines, but this is only used when there is a publish buffer overrun to clear the oldest message from the buffer. I changed this to a malloc inside the error handling if statement instead. This made a big difference to the amount of free heap I had at crucial moments
https://github.com/tuanpmt/esp_mqtt/issues/160
I also found I could set the SSL size to 4096 for MQTT and still publish everything I needed to, however I needed 5120 for HTTPS calls to a different server, so it's highly dependent on where you're connecting - have a play reducing the value for your site and you may find you can get away with less.
The other thing I found was that there are several problems with the default esp_mqtt code, and one that may be affecting you is that it allocates a 2kB data array on the heap during publish routines, but this is only used when there is a publish buffer overrun to clear the oldest message from the buffer. I changed this to a malloc inside the error handling if statement instead. This made a big difference to the amount of free heap I had at crucial moments
https://github.com/tuanpmt/esp_mqtt/issues/160
I also found I could set the SSL size to 4096 for MQTT and still publish everything I needed to, however I needed 5120 for HTTPS calls to a different server, so it's highly dependent on where you're connecting - have a play reducing the value for your site and you may find you can get away with less.